Skip to content

Commit cce683a

Browse files
janheisetodvora
andauthored
Adding a reference date to NaturalDateParserTests (#22503)
* adding a reference date to all tests to guard against time weirdness on CI * Using fixed clock in NaturalDateParser --------- Co-authored-by: Tomas Dvorak <[email protected]>
1 parent d517932 commit cce683a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

graylog2-server/src/main/java/org/graylog2/plugin/utilities/date/NaturalDateParser.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.joda.time.DateTime;
2424
import org.joda.time.DateTimeZone;
2525

26+
import java.time.Clock;
27+
import java.time.Instant;
2628
import java.time.ZoneId;
2729
import java.time.temporal.WeekFields;
2830
import java.util.Arrays;
@@ -38,20 +40,21 @@ public class NaturalDateParser {
3840
private final ZoneId zoneId;
3941
private final DateTimeZone dateTimeZone;
4042
private final Locale locale;
43+
private final Clock clock;
4144

4245
public NaturalDateParser() {
43-
this("Etc/UTC", Locale.getDefault());
46+
this("Etc/UTC", Locale.getDefault(), Clock.systemDefaultZone());
4447
}
4548

4649
public NaturalDateParser(final Locale locale) {
47-
this("Etc/UTC", locale);
50+
this("Etc/UTC", locale, Clock.systemDefaultZone());
4851
}
4952

5053
public NaturalDateParser(final String timeZone) throws IllegalArgumentException {
51-
this(timeZone, Locale.getDefault());
54+
this(timeZone, Locale.getDefault(), Clock.systemDefaultZone());
5255
}
5356

54-
public NaturalDateParser(final String timeZone, final Locale locale) throws IllegalArgumentException {
57+
public NaturalDateParser(final String timeZone, final Locale locale, Clock clock) throws IllegalArgumentException {
5558
if (!isValidTimeZone(timeZone)) {
5659
throw new IllegalArgumentException("Invalid timeZone: " + timeZone);
5760
}
@@ -60,6 +63,7 @@ public NaturalDateParser(final String timeZone, final Locale locale) throws Ille
6063
this.timeZone = TimeZone.getTimeZone(timeZone);
6164
this.zoneId = ZoneId.of(timeZone);
6265
this.dateTimeZone = DateTimeZone.forTimeZone(this.timeZone);
66+
this.clock = clock;
6367
}
6468

6569
boolean isValidTimeZone(final String timeZone) {
@@ -127,7 +131,9 @@ Date alignToEndOfDay(Date dateToConvert) {
127131
}
128132

129133
public Result parse(final String string) throws DateNotParsableException {
130-
return this.parse(string, new Date());
134+
Instant instant = clock.instant(); // Get the current instant from the clock
135+
Date date = Date.from(instant);
136+
return this.parse(string, date);
131137
}
132138

133139
Result parse(final String string, final Date referenceDate) throws DateNotParsableException {

graylog2-server/src/test/java/org/graylog2/plugin/utilities/date/NaturalDateParserTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import org.junit.jupiter.api.Disabled;
2525
import org.junit.jupiter.api.Test;
2626

27+
import java.time.Clock;
28+
import java.time.Instant;
2729
import java.util.Locale;
2830

31+
import static java.time.ZoneOffset.UTC;
2932
import static org.assertj.core.api.Assertions.assertThat;
3033
import static org.assertj.jodatime.api.Assertions.assertThat;
3134
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -51,6 +54,10 @@
5154
*
5255
*/
5356
public class NaturalDateParserTest {
57+
58+
private static final Instant fixedInstant = Instant.parse("2025-05-05T09:45:23Z");
59+
private static final Clock fixedClock = Clock.fixed(fixedInstant, UTC);
60+
5461
private NaturalDateParser naturalDateParser;
5562
private NaturalDateParser naturalDateParserAntarctica;
5663
private NaturalDateParser naturalDateParserUtc;
@@ -75,9 +82,9 @@ public class NaturalDateParserTest {
7582

7683
@BeforeEach
7784
public void setUp() {
78-
naturalDateParser = new NaturalDateParser();
79-
naturalDateParserUtc = new NaturalDateParser("Etc/UTC");
80-
naturalDateParserAntarctica = new NaturalDateParser("Antarctica/Palmer");
85+
naturalDateParser = new NaturalDateParser("Etc/UTC", Locale.getDefault(), fixedClock);
86+
naturalDateParserUtc = new NaturalDateParser("Etc/UTC", Locale.getDefault(), fixedClock);
87+
naturalDateParserAntarctica = new NaturalDateParser("Antarctica/Palmer", Locale.getDefault(), fixedClock);
8188
}
8289

8390
@Test

0 commit comments

Comments
 (0)