public long calculateBusinessTimeAsDuration(String timeExpression) { timeExpression = adoptISOFormat(timeExpression); Date calculatedDate = calculateBusinessTimeAsDate(timeExpression); return (calculatedDate.getTime() - getCurrentTime()); }
@Test(expected = IllegalArgumentException.class) public void testMissingConfigurationSingleArgConstructor() { BusinessCalendarImpl businessCal = new BusinessCalendarImpl(null); }
protected void init() { if (this.businessCalendarConfiguration == null) { throw new IllegalArgumentException("BusinessCalendar configuration was not provided."); } daysPerWeek = getPropertyAsInt(DAYS_PER_WEEK, "5"); hoursInDay = getPropertyAsInt(HOURS_PER_DAY, "8"); startHour = getPropertyAsInt(START_HOUR, "9"); endHour = getPropertyAsInt(END_HOUR, "17"); holidays = parseHolidays(); parseWeekendDays(); this.timezone = businessCalendarConfiguration.getProperty(TIMEZONE); }
public Date calculateBusinessTimeAsDate(String timeExpression) { timeExpression = adoptISOFormat(timeExpression); c.add(Calendar.WEEK_OF_YEAR, numberOfWeeks); handleWeekend(c, hours > 0 || min > 0); hours += (days - (numberOfWeeks * daysPerWeek)) * hoursInDay; for (int i = 0; i < numberOfDays; i++) { c.add(Calendar.DAY_OF_YEAR, 1); handleWeekend(c, false); handleHoliday(c, hours > 0 || min > 0); handleWeekend(c, true); handleHoliday(c, hours > 0 || min > 0); handleWeekend(c, false); handleHoliday(c, false);
@Test public void testCalculateDaysPassingOverHolidayAtYearEndWithWildcards() { Properties config = new Properties(); config.setProperty(BusinessCalendarImpl.HOLIDAYS, "*-12-31:*-01-01"); String expectedDate = "2013-01-02 09:15"; SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTime("2012-12-28 16:45").getTime()); BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock); Date result = businessCal.calculateBusinessTimeAsDate("2d30m"); assertEquals(expectedDate, formatDate("yyyy-MM-dd HH:mm", result)); }
public BusinessCalendarImpl(Properties configuration, SessionClock clock) { this.businessCalendarConfiguration = configuration; this.clock = clock; init(); }
protected void handleWeekend(Calendar c, boolean resetTime) { int dayOfTheWeek = c.get(Calendar.DAY_OF_WEEK); while (!isWorkingDay(dayOfTheWeek)) { c.add(Calendar.DAY_OF_YEAR, 1); if (resetTime) { c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); } dayOfTheWeek = c.get(Calendar.DAY_OF_WEEK); } } }
protected void handleHoliday(Calendar c, boolean resetTime) { if (!holidays.isEmpty()) { Date current = c.getTime(); for (TimePeriod holiday : holidays) { // check each holiday if it overlaps current date and break after first match if (current.after(holiday.getFrom()) && current.before(holiday.getTo())) { Calendar tmp = new GregorianCalendar(); tmp.setTime(holiday.getTo()); Calendar tmp2 = new GregorianCalendar(); tmp2.setTime(current); tmp2.set(Calendar.HOUR_OF_DAY, 0); tmp2.set(Calendar.MINUTE, 0); tmp2.set(Calendar.SECOND, 0); tmp2.set(Calendar.MILLISECOND, 0); long difference = tmp.getTimeInMillis() - tmp2.getTimeInMillis(); c.add(Calendar.HOUR_OF_DAY, (int) (difference/HOUR_IN_MILLIS)); handleWeekend(c, resetTime); break; } } } }
@Test public void testCalculateDaysCustomWorkingDays() { Properties config = new Properties(); config.setProperty(BusinessCalendarImpl.DAYS_PER_WEEK, "4"); config.setProperty(BusinessCalendarImpl.WEEKEND_DAYS, Calendar.FRIDAY + ","+Calendar.SATURDAY + "," +Calendar.SUNDAY); String expectedDate = "2012-05-15 14:30"; SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTime("2012-05-03 14:30").getTime()); BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock); Date result = businessCal.calculateBusinessTimeAsDate("6d"); assertEquals(expectedDate, formatDate("yyyy-MM-dd HH:mm", result)); }
public Date calculateBusinessTimeAsDate(String timeExpression) { timeExpression = adoptISOFormat(timeExpression); c.add(Calendar.WEEK_OF_YEAR, numberOfWeeks); handleWeekend(c, hours > 0 || min > 0); hours += (days - (numberOfWeeks * daysPerWeek)) * hoursInDay; for (int i = 0; i < numberOfDays; i++) { c.add(Calendar.DAY_OF_YEAR, 1); handleWeekend(c, false); handleHoliday(c, hours > 0 || min > 0); handleWeekend(c, true); handleHoliday(c, hours > 0 || min > 0); handleWeekend(c, false); handleHoliday(c, false);
public BusinessCalendarImpl(Properties configuration) { this.businessCalendarConfiguration = configuration; init(); }
protected void handleWeekend(Calendar c, boolean resetTime) { int dayOfTheWeek = c.get(Calendar.DAY_OF_WEEK); while (!isWorkingDay(dayOfTheWeek)) { c.add(Calendar.DAY_OF_YEAR, 1); if (resetTime) { c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); } dayOfTheWeek = c.get(Calendar.DAY_OF_WEEK); } } }
protected void handleHoliday(Calendar c, boolean resetTime) { if (!holidays.isEmpty()) { Date current = c.getTime(); for (TimePeriod holiday : holidays) { // check each holiday if it overlaps current date and break after first match if (current.after(holiday.getFrom()) && current.before(holiday.getTo())) { Calendar tmp = new GregorianCalendar(); tmp.setTime(holiday.getTo()); Calendar tmp2 = new GregorianCalendar(); tmp2.setTime(current); tmp2.set(Calendar.HOUR_OF_DAY, 0); tmp2.set(Calendar.MINUTE, 0); tmp2.set(Calendar.SECOND, 0); tmp2.set(Calendar.MILLISECOND, 0); long difference = tmp.getTimeInMillis() - tmp2.getTimeInMillis(); c.add(Calendar.HOUR_OF_DAY, (int) (difference/HOUR_IN_MILLIS)); handleWeekend(c, resetTime); break; } } } }
@Test public void testCalculateTimeDaysHoursMinutesSingleDayHolidaysInMiddleOfWeek() { Properties config = new Properties(); config.setProperty(BusinessCalendarImpl.HOLIDAYS, "2012-05-09"); String expectedDate = "2012-05-10 15:30"; SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTime("2012-05-08 11:10").getTime()); BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock); Date result = businessCal.calculateBusinessTimeAsDate("1d4h20m"); assertEquals(expectedDate, formatDate("yyyy-MM-dd HH:mm", result)); }
public long calculateBusinessTimeAsDuration(String timeExpression) { timeExpression = adoptISOFormat(timeExpression); Date calculatedDate = calculateBusinessTimeAsDate(timeExpression); return (calculatedDate.getTime() - getCurrentTime()); }
protected void init() { if (this.businessCalendarConfiguration == null) { throw new IllegalArgumentException("BusinessCalendar configuration was not provided."); } daysPerWeek = getPropertyAsInt(DAYS_PER_WEEK, "5"); hoursInDay = getPropertyAsInt(HOURS_PER_DAY, "8"); startHour = getPropertyAsInt(START_HOUR, "9"); endHour = getPropertyAsInt(END_HOUR, "17"); holidays = parseHolidays(); parseWeekendDays(); this.timezone = businessCalendarConfiguration.getProperty(TIMEZONE); }
public BusinessCalendarImpl() { String propertiesLocation = System.getProperty("jbpm.business.calendar.properties"); if (propertiesLocation == null) { propertiesLocation = DEFAULT_PROPERTIES_NAME; } businessCalendarConfiguration = new Properties(); InputStream in = this.getClass().getResourceAsStream(propertiesLocation); if (in != null) { try { businessCalendarConfiguration.load(in); } catch (IOException e) { logger.error("Error while loading properties for business calendar", e); } } init(); }
@Test(expected = IllegalArgumentException.class) public void testMissingConfigurationDualArgConstructor() { SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTime("2012-05-04 13:45").getTime()); BusinessCalendarImpl businessCal = new BusinessCalendarImpl(null, clock); }
@Test public void testSingleHolidayWithinGivenTime() { final Properties props = new Properties(); props.put(BusinessCalendarImpl.HOLIDAYS, "2015-01-13"); String expectedDate = "2015-01-15 11:38"; SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTimeAndMillis("2015-01-08 11:38:30.198").getTime()); BusinessCalendarImpl businessCalendarImpl = new BusinessCalendarImpl(props, clock); Date result = businessCalendarImpl.calculateBusinessTimeAsDate("4d"); assertEquals(expectedDate, formatDate("yyyy-MM-dd HH:mm", result)); }
public BusinessCalendarImpl(Properties configuration, SessionClock clock) { this.businessCalendarConfiguration = configuration; this.clock = clock; init(); }