-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
KAFKA-18585 Fix fail test ValuesTest#shouldConvertDateValues #18611
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -887,7 +887,9 @@ public void shouldConvertTimeValues() { | |
|
||
@Test | ||
public void shouldConvertDateValues() { | ||
java.util.Date current = new java.util.Date(); | ||
LocalDateTime localTime = LocalDateTime.now(); | ||
ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(localTime); | ||
java.util.Date current = new java.util.Date(localTime.toEpochSecond(zoneOffset) * 1000); | ||
long currentMillis = current.getTime() % MILLIS_PER_DAY; | ||
long days = current.getTime() / MILLIS_PER_DAY; | ||
|
||
|
@@ -901,8 +903,10 @@ public void shouldConvertDateValues() { | |
assertEquals(currentDate, d2); | ||
|
||
// ISO8601 strings - accept a string matching pattern "yyyy-MM-dd" | ||
LocalDateTime localTimeTruncated = localTime.truncatedTo(ChronoUnit.DAYS); | ||
java.util.Date d3 = Values.convertToDate(Date.SCHEMA, LocalDate.ofEpochDay(days).format(DateTimeFormatter.ISO_LOCAL_DATE)); | ||
assertEquals(currentDate, d3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @divijvaidya is correct that I suggest replacing the first line of the method with:
And then the check becomes:
I tried this with various timezones locally and it seems to work properly. Essentially, the time information is discarded in a way that a person in the timezone would expect looking at a clock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can observe that when https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Date.java#L1210 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @m1a2st Are you happy that this works now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, using |
||
LocalDateTime date3 = LocalDateTime.ofInstant(Instant.ofEpochMilli(d3.getTime()), ZoneId.systemDefault()); | ||
assertEquals(localTimeTruncated, date3); | ||
|
||
// Days as string | ||
java.util.Date d4 = Values.convertToDate(Date.SCHEMA, Long.toString(days)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must be missing something here so please help me understand the following:
Java's util.Date is timezone agnostic (it's always epochs in UTC timezone) until you want to print it (where you can format it). Hence, the currentDate here should already be in UTC timezone. And the actual value from
LocalDate.ofEpochDay(days).format(DateTimeFormatter.ISO_LOCAL_DATE)
should also be in UTC. So, where is the disconnect here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'm not sure either. But I can believe it's possible to write a test that is timezone-dependent.