Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-issues#1648] Fix BusinessCalendarImpl time calculation when currentCalHour < startHour #3795

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ public long calculateBusinessTimeAsDuration(String timeExpression) {

Date calculatedDate = calculateBusinessTimeAsDate(timeExpression);

return (calculatedDate.getTime() - getCurrentTime());
long calculatedDurationInMs = (calculatedDate.getTime() - getCurrentTime());
logger.debug("calculateBusinessTimeAsDuration for expression {} returns {} seconds", timeExpression, (calculatedDurationInMs / 1000));
return calculatedDurationInMs;
}

public Date calculateBusinessTimeAsDate(String timeExpression) {
Expand Down Expand Up @@ -251,7 +253,9 @@ public Date calculateBusinessTimeAsDate(String timeExpression) {
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
} else if (currentCalHour < startHour) {
c.add(Calendar.HOUR_OF_DAY, startHour);
c.add(Calendar.HOUR_OF_DAY, startHour - currentCalHour);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
}

// calculate remaining hours
Expand All @@ -267,7 +271,9 @@ public Date calculateBusinessTimeAsDate(String timeExpression) {
c.set(Calendar.HOUR_OF_DAY, startHour);
c.add(Calendar.HOUR_OF_DAY, currentCalHour - endHour);
} else if (currentCalHour < startHour) {
c.add(Calendar.HOUR_OF_DAY, startHour);
c.add(Calendar.HOUR_OF_DAY, startHour - currentCalHour);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
}

// calculate minutes
Expand All @@ -293,7 +299,9 @@ public Date calculateBusinessTimeAsDate(String timeExpression) {
c.set(Calendar.HOUR_OF_DAY, startHour);
c.add(Calendar.HOUR_OF_DAY, currentCalHour - endHour);
} else if (currentCalHour < startHour) {
c.add(Calendar.HOUR_OF_DAY, startHour);
c.add(Calendar.HOUR_OF_DAY, startHour - currentCalHour);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we change construction of HOUR_OF_DAY, maybe it would be worth to add also *hour* tests?

During my brief check, I didn't find such tests. Also, I didn't check if tring duration = "2h"; is a correct duration value. I wrote the code below just in github comment.

If you think, such tests are not needed, feel free to proceed without adding such tests.

    @Test
    public void testCalculateHoursBeforeStartHour() {
        Properties config = new Properties();
        config.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "4");
        config.setProperty(BusinessCalendarImpl.START_HOUR, "14");
        config.setProperty(BusinessCalendarImpl.END_HOUR, "18");
        String currentDate = "2024-11-28 10:48:33.000";
        String duration = "2h";
        String expectedDate = "2024-11-28 16:00:00";

        SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTimeAndMillis(currentDate).getTime());
        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock);

        Date result = businessCal.calculateBusinessTimeAsDate(duration);

        assertThat(formatDate("yyyy-MM-dd HH:mm:ss", result)).isEqualTo(expectedDate);
    }


    @Test
    public void testCalculateHoursBeforeEndHour() {
        Properties config = new Properties();
        config.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "4");
        config.setProperty(BusinessCalendarImpl.START_HOUR, "14");
        config.setProperty(BusinessCalendarImpl.END_HOUR, "18");
        String currentDate = "2024-11-28 17:58:33.000";
        String duration = "2h";
        String expectedDate = "2024-11-29 15:58:33";

        SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTimeAndMillis(currentDate).getTime());
        BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock);

        Date result = businessCal.calculateBusinessTimeAsDate(duration);

        assertThat(formatDate("yyyy-MM-dd HH:mm:ss", result)).isEqualTo(expectedDate);
    }

c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
}
// take under consideration weekend
handleWeekend(c, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,78 @@ public void testCalculateMinutesPassingWeekend() {
assertThat(formatDate("yyyy-MM-dd HH:mm:ss", result)).isEqualTo(expectedDate);
}

@Test
public void testCalculateMinutesBeforeStartHour() {
Properties config = new Properties();
config.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "4");
config.setProperty(BusinessCalendarImpl.START_HOUR, "14");
config.setProperty(BusinessCalendarImpl.END_HOUR, "18");
String currentDate = "2024-11-28 10:48:33.000";
String duration = "10m";
String expectedDate = "2024-11-28 14:10:00";

SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTimeAndMillis(currentDate).getTime());
BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock);

Date result = businessCal.calculateBusinessTimeAsDate(duration);

assertThat(formatDate("yyyy-MM-dd HH:mm:ss", result)).isEqualTo(expectedDate);
}

@Test
public void testCalculateSecondsBeforeStartHour() {
Properties config = new Properties();
config.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "4");
config.setProperty(BusinessCalendarImpl.START_HOUR, "14");
config.setProperty(BusinessCalendarImpl.END_HOUR, "18");
String currentDate = "2024-11-28 10:48:33.000";
String duration = "10s";
String expectedDate = "2024-11-28 14:00:10";

SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTimeAndMillis(currentDate).getTime());
BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock);

Date result = businessCal.calculateBusinessTimeAsDate(duration);

assertThat(formatDate("yyyy-MM-dd HH:mm:ss", result)).isEqualTo(expectedDate);
}

@Test
public void testCalculateMinutesBeforeEndHour() {
Properties config = new Properties();
config.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "4");
config.setProperty(BusinessCalendarImpl.START_HOUR, "14");
config.setProperty(BusinessCalendarImpl.END_HOUR, "18");
String currentDate = "2024-11-28 17:58:33.000";
String duration = "10m";
String expectedDate = "2024-11-29 14:08:33";

SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTimeAndMillis(currentDate).getTime());
BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock);

Date result = businessCal.calculateBusinessTimeAsDate(duration);

assertThat(formatDate("yyyy-MM-dd HH:mm:ss", result)).isEqualTo(expectedDate);
}

@Test
public void testCalculateSecondsBeforeEndHour() {
Properties config = new Properties();
config.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "4");
config.setProperty(BusinessCalendarImpl.START_HOUR, "14");
config.setProperty(BusinessCalendarImpl.END_HOUR, "18");
String currentDate = "2024-11-28 17:59:33.000";
String duration = "50s";
String expectedDate = "2024-11-29 14:00:23";

SessionPseudoClock clock = new StaticPseudoClock(parseToDateWithTimeAndMillis(currentDate).getTime());
BusinessCalendarImpl businessCal = new BusinessCalendarImpl(config, clock);

Date result = businessCal.calculateBusinessTimeAsDate(duration);

assertThat(formatDate("yyyy-MM-dd HH:mm:ss", result)).isEqualTo(expectedDate);
}

private Date parseToDate(String dateString) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Expand Down
Loading