schibsted / jslt

JSON query and transformation language
Apache License 2.0
638 stars 120 forks source link

Format the given time from UTC/GMT to ET (including EST & EDT) #318

Open sabareeshmurthy opened 1 year ago

sabareeshmurthy commented 1 year ago

Need to transform UTC/GMT time to Eastern Time which should follow day light saving too. Tried using format-time function with time zone as ET/EDT but it shows as invalid time zone. EST works but it always gives GMT-5 even during day light saving days.

Do we have any library to convert given date time to Eastern Time including day light saving?

Excepted input -> output: [Daylight Saving Date] 2023-11-03T13:36:04:535Z -> 2023-11-03T09:36:04:535-04:00

[Non-Daylight Saving Date] 2023-11-10T10:33:37:929Z -> 2023-11-10T05:33:37:929-05:00

greyfairer commented 10 months ago

@sabareeshmurthy What did you use in your jslt file?

The built-in parse-time function currently uses Java SimpleDateFormat, not the newer JSR-310 DateTimeFormatter. See BuiltinFunctions.java

I'm considering creating a PR to switch this, because we also have a use-case for this, but maybe we should just add our own Custom Function for this?

greyfairer commented 10 months ago

Difference between SimpleDateFormat and DateTimeFormatter

SimpleDateFormat and DateTimeFormatter are two different classes in Java that serve a similar purpose of formatting and parsing dates and times. However, DateTimeFormatter was introduced in Java 8 as a replacement for SimpleDateFormat, and it provides several advantages over SimpleDateFormat. Here are some of the main differences between the two:

  1. Thread safety: DateTimeFormatter is thread-safe, meaning that multiple threads can use the same instance of the formatter without any issues. On the other hand, SimpleDateFormat is not thread-safe, so if you need to format or parse dates in a multi-threaded environment, you need to synchronize access to the formatter or use a separate instance for each thread.
  2. Immutable: DateTimeFormatter is immutable, meaning that once you create an instance of the formatter, you cannot modify its properties. This makes it easier to share the same formatter instance across multiple threads or methods without worrying about unintentional changes to its configuration. SimpleDateFormat, on the other hand, is mutable, and changing its properties can lead to unpredictable behavior.
  3. More flexible: DateTimeFormatter is more flexible than SimpleDateFormat in terms of the formatting options it provides. For example, it can handle a wider range of date and time formats, including ISO date-time formats, and it supports more options for formatting and parsing, such as localized date and time patterns.
  4. Better error handling: DateTimeFormatter provides better error handling than SimpleDateFormat. For example, it can handle invalid or ambiguous date and time input more gracefully and provide more detailed error messages.

Overall, DateTimeFormatter is a more modern and robust class than SimpleDateFormat, and it is recommended for all new Java development. However, if you are working with older Java versions or legacy code, you may still need to use SimpleDateFormat.

larsga commented 10 months ago

@greyfairer If you want to make a PR for it might be interested in that. The thread safety isn't an issue, because we are not caching and reusing the SimpleDateFormat instances. But better error messages is definitely valuable.

I guess a bigger issue might be that existing format strings would change behaviour. We'd have to look into that before switching over.