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

Timezone support #5

Merged
merged 9 commits into from
Feb 26, 2024
Merged

Timezone support #5

merged 9 commits into from
Feb 26, 2024

Conversation

stheppi
Copy link
Collaborator

@stheppi stheppi commented Feb 23, 2024

There have been requests from users to support date/time partition in the S3/GCP/Azure sink with a specific timezone. The code changes brings the timezone configuration as an optional setting. When epoch configuration is involved, changing the timezone to non-UTC yields an error.

Removes the check plugin since the jar dependency is built with a newer JVM version and fails the build.

There have been requests from users to support date/time partition in the S3/GCP/Azure sink with a specific timezone. The code changes brings the timezone configuration as an optional setting. When epoch configuration is involved, changing the timezone to non-UTC yields an error.

Removes the check plugin since the jar dependency is built with a newer JVM version and fails the build.
@@ -81,7 +92,9 @@ public R apply(R r) {
return null;
}

final String value = valueExtractorF.apply(instantF.get());
Instant now = instantF.get();

Choose a reason for hiding this comment

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

how about nowSupplier or nowFunction instead of instantF?

result.set(Calendar.SECOND, 0);
result.set(Calendar.MILLISECOND, 0);
return result.getTime();
ZonedDateTime truncated =

Choose a reason for hiding this comment

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

I may be missing a point here but why not just:

ZonedDateTime.ofInstant(orig.toInstant(), config.targetTimeZoneId);

or ZoneOffset.UTC.normalized() when you need UTC? Cause here you pass two zones - one from config and second is UTC.

But it's still much better than what was there before the change ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The translator is responsible for only taking the date part of the incoming Date.

So:

  • orig.toInstant().atZone(config.targetTimeZoneId).toLocalDate(): extracts the date based on the configured timezone
  • LocalTime.MIDNIGHT: it's all about the 0 hour, 0 minute, 0 second, etc
  • UTC: at this point we don't want another timezone translation because on 01-01 2024 0:0:0 Chicago time would be still in 2023

Choose a reason for hiding this comment

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

I see the point now, thanks! But IMO it's still too much of a hassle and could be more readable. My proposition:

    ZonedDateTime truncated = ZonedDateTime.ofInstant(orig.toInstant(), config.targetTimeZoneId).truncatedTo(ChronoUnit.DAYS);
    return Date.from(truncated.toInstant());

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The option you shared yields incorrect results. in this case it will return the data as of 7am 01/01/1970 - see test testSchemalessTimestampToDateOnNonUTC

result.set(Calendar.MILLISECOND, origCalendar.get(Calendar.MILLISECOND));
return result.getTime();
ZonedDateTime zonedDateTime = orig.toInstant().atZone(config.targetTimeZoneId);
return Date.from(

Choose a reason for hiding this comment

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

same as above :)

ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(orig.toInstant(), config.targetTimeZoneId);
return Date.from(zonedDateTime.toInstant());

Map<String, String> configs = new HashMap<>();
configs.put("header.name", "wallclock");
configs.put("date.time.part", "year");
configs.put("timezone", "Asia/Kolkata");
Copy link

@GoMati-MU GoMati-MU Feb 23, 2024

Choose a reason for hiding this comment

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

MINOR: it's nice to pass comment with time difference for better understanding e.g.

String kolkataTimezone = "Asia/Kolkata"; // Kolkata is GMT+5:30 which makes it 2021 there.
configs.put("timezone", kolkataTimezone);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wouldn't the code assertion infer:

Kolkata is GMT+5:30 which makes it 2021 there.

Copy link

@GoMati-MU GoMati-MU Feb 23, 2024

Choose a reason for hiding this comment

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

I corrected my code :) Comment is one way to make developer know that TimeZone difference will cause Year to flip. Another is to add message to assertion e.g.

    assertEquals("2021", transformed.headers().lastWithName("wallclock").value(), "Year should flip as we calculate it for Kolkata Timezone");

@stheppi stheppi merged commit 93dbeb6 into main Feb 26, 2024
2 checks passed
@stheppi stheppi deleted the feat/timezone_conversion branch February 26, 2024 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants