Skip to content

Commit

Permalink
More stable date formatting #10239 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 5, 2025
1 parent 7a52a93 commit 832ba1d
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions core/src/test/java/hudson/scheduler/CronTabTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@

import antlr.ANTLRException;
import java.text.DateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.function.Function;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

Expand Down Expand Up @@ -343,20 +346,20 @@ public int next(int n) {

@Test public void floorCeilMinuteGranularity() throws Exception {
var tab = new CronTab("*/5 * * * *");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 15, 23), "Mar 4, 2025, 2:15:00 PM", "Mar 4, 2025, 2:20:00 PM");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 19, 23), "Mar 4, 2025, 2:15:00 PM", "Mar 4, 2025, 2:20:00 PM");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 16, 0), "Mar 4, 2025, 2:15:00 PM", "Mar 4, 2025, 2:20:00 PM");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 19, 0), "Mar 4, 2025, 2:15:00 PM", "Mar 4, 2025, 2:20:00 PM");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 15, 0), "Mar 4, 2025, 2:15:00 PM", "Mar 4, 2025, 2:15:00 PM");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 15, 23), "2025-03-04T14:15:00", "2025-03-04T14:20:00");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 19, 23), "2025-03-04T14:15:00", "2025-03-04T14:20:00");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 16, 0), "2025-03-04T14:15:00", "2025-03-04T14:20:00");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 19, 0), "2025-03-04T14:15:00", "2025-03-04T14:20:00");
assertFloorCeil(tab, new GregorianCalendar(2025, 2, 4, 14, 15, 0), "2025-03-04T14:15:00", "2025-03-04T14:15:00");
}

private static void assertFloorCeil(CronTab tab, Calendar now, String expectedFloor, String expectedCeil) {
var fmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
var nowFormatted = fmt.format(now.getTime());
Function<Calendar, String> fmt = c -> ZonedDateTime.ofInstant(c.toInstant(), c.getTimeZone().toZoneId()).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
var nowFormatted = fmt.apply(now);
var nowClone = (Calendar) now.clone();
assertThat("floor of " + nowFormatted, fmt.format(tab.floor(nowClone).getTime()), is(expectedFloor));
assertThat("floor of " + nowFormatted, fmt.apply(tab.floor(nowClone)), is(expectedFloor));
nowClone = (Calendar) now.clone();
assertThat("ceil of " + nowFormatted, fmt.format(tab.ceil(nowClone).getTime()), is(expectedCeil));
assertThat("ceil of " + nowFormatted, fmt.apply(tab.ceil(nowClone)), is(expectedCeil));
}

@Issue("SECURITY-790")
Expand Down

0 comments on commit 832ba1d

Please sign in to comment.