tdammers / ginger

A Haskell implementation of the Jinja template language.
MIT License
77 stars 13 forks source link

Fraction of second in date #55

Closed mulderr closed 4 years ago

mulderr commented 4 years ago

Hi,

dateformat only supports whole seconds but JSON date strings may include fractions ex. 2020-08-21T19:35:42.994Z.

Use case:

  1. fetch some data from db
  2. convert to aeson Value using toJSON
  3. pass as context to easyRender

Expected behavior: Dates can be formatted using dateformat.

Actual behavior: In case the date has an exact number of seconds everything works fine. However, when fractions of seconds are present ex. 2020-08-21T19:35:42.994Z then dateformat produces no output.

I think the only change needed would be to add %Q to the first 4 formats here. This handles both cases:

λ> parseTimeOrError False defaultTimeLocale "%Y-%m-%dT%H:%M:%S%Q%Z" "2020-08-21T19:35:42.994Z" :: UTCTime
2020-08-21 19:35:42.994 UTC
it :: UTCTime
λ> parseTimeOrError False defaultTimeLocale "%Y-%m-%dT%H:%M:%S%Q%Z" "2020-08-21T19:35:42Z" :: UTCTime
2020-08-21 19:35:42 UTC
it :: UTCTime