public void start() { DateTokenConverter dtc = tbrp.fileNamePattern.getPrimaryDateTokenConverter(); if (dtc == null) { throw new IllegalStateException("FileNamePattern [" + tbrp.fileNamePattern.getPattern() + "] does not contain a valid DateToken"); } rc = new RollingCalendar(); rc.init(dtc.getDatePattern()); addInfo("The date pattern is '" + dtc.getDatePattern() + "' from file name pattern '" + tbrp.fileNamePattern.getPattern() + "'."); rc.printPeriodicity(this); setDateInCurrentPeriod(new Date(getCurrentTime())); if (tbrp.getParentsRawFileProperty() != null) { File currentFile = new File(tbrp.getParentsRawFileProperty()); if (currentFile.exists() && currentFile.canRead()) { setDateInCurrentPeriod(new Date(currentFile.lastModified())); } } addInfo("Setting initial period to " + dateInCurrentPeriod); computeNextCheck(); }
public long getNextTriggeringMillis(Date now) { return getNextTriggeringDate(now).getTime(); }
public void init(String datePattern) { periodicityType = computePeriodicityType(datePattern); }
public Date getRelativeDate(Date now, int periods) { this.setTime(now); this.add(Calendar.MILLISECOND, periods); break; this.set(Calendar.MILLISECOND, 0); this.add(Calendar.SECOND, periods); break; this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.MINUTE, periods); break; this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.HOUR_OF_DAY, periods); break; this.set(Calendar.HOUR_OF_DAY, 0); this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.DATE, periods); break; this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek()); this.set(Calendar.HOUR_OF_DAY, 0);
public PeriodicityType computePeriodicityType(String datePattern) { RollingCalendar rollingCalendar = new RollingCalendar(GMT_TIMEZONE, Locale .getDefault()); // set sate to 1970-01-01 00:00:00 GMT Date epoch = new Date(0); if (datePattern != null) { for (PeriodicityType i : PeriodicityType.VALID_ORDERED_LIST) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern); simpleDateFormat.setTimeZone(GMT_TIMEZONE); // all date formatting done // in GMT String r0 = simpleDateFormat.format(epoch); rollingCalendar.setPeriodicityType(i); Date next = new Date(rollingCalendar.getNextTriggeringMillis(epoch)); String r1 = simpleDateFormat.format(next); // System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1); if ((r0 != null) && (r1 != null) && !r0.equals(r1)) { return i; } } } // we failed return PeriodicityType.ERRONEOUS; }
public void start() { DateTokenConverter<Object> dtc = tbrp.fileNamePattern.getPrimaryDateTokenConverter(); if (dtc == null) { throw new IllegalStateException("FileNamePattern [" + tbrp.fileNamePattern.getPattern() + "] does not contain a valid DateToken"); } if (dtc.getTimeZone() != null) { rc = new RollingCalendar(dtc.getDatePattern(), dtc.getTimeZone(), Locale.getDefault()); } else { rc = new RollingCalendar(dtc.getDatePattern()); } addInfo("The date pattern is '" + dtc.getDatePattern() + "' from file name pattern '" + tbrp.fileNamePattern.getPattern() + "'."); rc.printPeriodicity(this); if (!rc.isCollisionFree()) { addError("The date format in FileNamePattern will result in collisions in the names of archived log files."); addError(CoreConstants.MORE_INFO_PREFIX + COLLIDING_DATE_FORMAT_URL); withErrors(); return; } setDateInCurrentPeriod(new Date(getCurrentTime())); if (tbrp.getParentsRawFileProperty() != null) { File currentFile = new File(tbrp.getParentsRawFileProperty()); if (currentFile.exists() && currentFile.canRead()) { setDateInCurrentPeriod(new Date(currentFile.lastModified())); } } addInfo("Setting initial period to " + dateInCurrentPeriod); computeNextCheck(); }
public long periodBarriersCrossed(long start, long end) { if (start > end) throw new IllegalArgumentException("Start cannot come before end"); long startFloored = getStartOfCurrentPeriodWithGMTOffsetCorrection(start, getTimeZone()); long endFloored = getStartOfCurrentPeriodWithGMTOffsetCorrection(end, getTimeZone()); long diff = endFloored - startFloored; switch (periodicityType) { case TOP_OF_MILLISECOND: return diff; case TOP_OF_SECOND: return diff / MILLIS_IN_ONE_SECOND; case TOP_OF_MINUTE: return diff / MILLIS_IN_ONE_MINUTE; case TOP_OF_HOUR: return (int) diff / MILLIS_IN_ONE_HOUR; case TOP_OF_DAY: return diff / MILLIS_IN_ONE_DAY; case TOP_OF_WEEK: return diff / MILLIS_IN_ONE_WEEK; case TOP_OF_MONTH: return diffInMonths(start, end); default: throw new IllegalStateException("Unknown periodicity type."); } }
private static void checkDateFormat(DateTokenConverter<Object> token, String pattern, String docRef) { RollingCalendar rollingCalendar; if (token.getTimeZone() != null) { rollingCalendar = new RollingCalendar(token.getDatePattern(), token.getTimeZone(), Locale.getDefault()); } else { rollingCalendar = new RollingCalendar(token.getDatePattern()); } if (!rollingCalendar.isCollisionFree()) { throw new IllegalStateException(String.format("%s %s", String.format(INCORRECT_DATE_FORMAT, pattern), docRef)); } } }
private Calendar getEndOfNextNthPeriod(String dateFormat, Date date, int n) { RollingCalendar rc = new RollingCalendar(dateFormat); Date nextDate = rc.getEndOfNextNthPeriod(date, n); Calendar cal = Calendar.getInstance(GMT_TIMEZONE, Locale.US); cal.setTime(nextDate); return cal; }
@Test public void testVaryingNumberOfDailyPeriods() { RollingCalendar rc = new RollingCalendar("yyyy-MM-dd"); final long MILLIS_IN_DAY = 24 * 3600 * 1000; for (int p = 20; p > -100; p--) { long now = 1223325293589L; // Mon Oct 06 22:34:53 CEST 2008 Date nowDate = new Date(now); Date result = rc.getEndOfNextNthPeriod(nowDate, p); long offset = rc.getTimeZone().getRawOffset() + rc.getTimeZone().getDSTSavings(); long origin = now - ((now + offset) % (MILLIS_IN_DAY)); long expected = origin + p * MILLIS_IN_DAY; assertEquals("p=" + p, expected, result.getTime()); } }
public PeriodicityType computePeriodicity(String datePattern) { RollingCalendar rollingCalendar = new RollingCalendar(GMT_TIMEZONE, Locale .getDefault()); // set sate to 1970-01-01 00:00:00 GMT Date epoch = new Date(0); if (datePattern != null) { for (PeriodicityType i : PeriodicityType.VALID_ORDERED_LIST) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern); simpleDateFormat.setTimeZone(GMT_TIMEZONE); // all date formatting done // in GMT String r0 = simpleDateFormat.format(epoch); rollingCalendar.setType(i); Date next = new Date(rollingCalendar.getNextTriggeringMillis(epoch)); String r1 = simpleDateFormat.format(next); // System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1); if ((r0 != null) && (r1 != null) && !r0.equals(r1)) { return i; } } } // we failed return PeriodicityType.ERRONEOUS; }
public Date getNextTriggeringDate(Date now) { return getEndOfNextNthPeriod(now, 1); }
@Test public void testMillisecondPeriodicity() { // The length of the 'S' pattern letter matters on different platforms, // and can render different results on different versions of Android. // This test verifies that the periodicity is correct for different // pattern lengths. { RollingCalendar rc = new RollingCalendar("yyyy-MM-dd-S"); assertEquals(PeriodicityType.TOP_OF_MILLISECOND, rc.getPeriodicityType()); } { RollingCalendar rc = new RollingCalendar("yyyy-MM-dd-SS"); assertEquals(PeriodicityType.TOP_OF_MILLISECOND, rc.getPeriodicityType()); } { RollingCalendar rc = new RollingCalendar("yyyy-MM-dd-SSS"); assertEquals(PeriodicityType.TOP_OF_MILLISECOND, rc.getPeriodicityType()); } }
public long periodsElapsed(long start, long end) { if (start > end) throw new IllegalArgumentException("Start cannot come before end"); long diff = end - start; switch (periodicityType) { case TOP_OF_MILLISECOND: return diff; case TOP_OF_SECOND: return diff / CoreConstants.MILLIS_IN_ONE_SECOND; case TOP_OF_MINUTE: return diff / CoreConstants.MILLIS_IN_ONE_MINUTE; case TOP_OF_HOUR: return (int) diff / CoreConstants.MILLIS_IN_ONE_HOUR; case TOP_OF_DAY: return diff / CoreConstants.MILLIS_IN_ONE_DAY; case TOP_OF_WEEK: return diff / CoreConstants.MILLIS_IN_ONE_WEEK; case TOP_OF_MONTH: return diffInMonths(start, end); default: throw new IllegalStateException("Unknown periodicity type."); } }
private TimeBasedArchiveRemover mockArchiveRemover(String filenamePattern, FileProvider fileProvider) { LoggerContext context = new LoggerContext(); RollingCalendar rollingCalendar = new RollingCalendar(DATE_FORMAT); FileNamePattern filePattern = new FileNamePattern(filenamePattern, context); TimeBasedArchiveRemover archiveRemover = new TimeBasedArchiveRemover(filePattern, rollingCalendar, fileProvider); archiveRemover.setContext(context); return spy(archiveRemover); }
public void init(String datePattern, int rollingInterval) { super.init(datePattern); if(rollingInterval > 1) { this.rollingInterval = rollingInterval; } }
@Override public boolean accept(File dir, String path) { Date fileDate = dateParser.parseFilename(path); fileDate = rc.normalizeDate(fileDate); Date refDate = rc.getEndOfNextNthPeriod(baseDate, -maxHistory); refDate = rc.normalizeDate(refDate); return fileDate.compareTo(refDate) < 0; } };
public Date getRelativeDate(Date now, int periods) { this.setTime(now); this.add(Calendar.MILLISECOND, periods); break; this.set(Calendar.MILLISECOND, 0); this.add(Calendar.SECOND, periods); break; this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.MINUTE, periods); break; this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.HOUR_OF_DAY, periods); break; this.set(Calendar.HOUR_OF_DAY, 0); this.set(Calendar.MINUTE, 0); this.set(Calendar.SECOND, 0); this.set(Calendar.MILLISECOND, 0); this.add(Calendar.DATE, periods); break; this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek()); this.set(Calendar.HOUR_OF_DAY, 0);
public void start() { DateTokenConverter<Object> dtc = tbrp.fileNamePattern.getPrimaryDateTokenConverter(); if (dtc == null) { throw new IllegalStateException("FileNamePattern [" + tbrp.fileNamePattern.getPattern() + "] does not contain a valid DateToken"); } if (dtc.getTimeZone() != null) { rc = new RollingCalendar(dtc.getDatePattern(), dtc.getTimeZone(), Locale.getDefault()); } else { rc = new RollingCalendar(dtc.getDatePattern()); } addInfo("The date pattern is '" + dtc.getDatePattern() + "' from file name pattern '" + tbrp.fileNamePattern.getPattern() + "'."); rc.printPeriodicity(this); if (!rc.isCollisionFree()) { addError("The date format in FileNamePattern will result in collisions in the names of archived log files."); addError(CoreConstants.MORE_INFO_PREFIX + COLLIDING_DATE_FORMAT_URL); withErrors(); return; } setDateInCurrentPeriod(new Date(getCurrentTime())); if (tbrp.getParentsRawFileProperty() != null) { File currentFile = new File(tbrp.getParentsRawFileProperty()); if (currentFile.exists() && currentFile.canRead()) { setDateInCurrentPeriod(new Date(currentFile.lastModified())); } } addInfo("Setting initial period to " + dateInCurrentPeriod); computeNextCheck(); }
public PeriodicityType computePeriodicityType(String datePattern) { RollingCalendar rollingCalendar = new RollingCalendar(GMT_TIMEZONE, Locale .getDefault()); // set sate to 1970-01-01 00:00:00 GMT Date epoch = new Date(0); if (datePattern != null) { for (PeriodicityType i : PeriodicityType.VALID_ORDERED_LIST) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern); simpleDateFormat.setTimeZone(GMT_TIMEZONE); // all date formatting done // in GMT String r0 = simpleDateFormat.format(epoch); rollingCalendar.setPeriodicityType(i); Date next = new Date(rollingCalendar.getNextTriggeringMillis(epoch)); String r1 = simpleDateFormat.format(next); // System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1); if ((r0 != null) && (r1 != null) && !r0.equals(r1)) { return i; } } } // we failed return PeriodicityType.ERRONEOUS; }