Skip to content

Serializing UTCTime #1

hvr opened this Issue Feb 1, 2011 · 6 comments

2 participants

hvr commented Feb 1, 2011

The default ToJson instance for serializing UTCTimes doesn't use the ISO-8601 subset described in ECMA-262:

> t <- getCurrentTime
t :: UTCTime
> encode t
Chunk "\"/Date(1296603856)/\"" Empty
it :: Data.ByteString.Lazy.Internal.ByteString
bos commented Feb 16, 2011

Fixed in 5cd9d76 - thanks for the report.

hvr commented Feb 16, 2011

almost... :-)

according to the formatTime description, the %Q represents "decimal point and up to 12 second decimals, without trailing zeros. For a whole number of seconds, %Q produces the empty string."

But EcmaScript specifies an optional fixed-width milliseconds field:

So either leave out the subseconds, e.g. by

formatTime defaultTimeLocale  "%FT%XZ" t

or use a hack like (maybe there's a better way):

let lhs = formatTime defaultTimeLocale "%FT%X" t
    rhs = take 4 $ formatTime defaultTimeLocale ".%q" t
in lhs ++ rhs ++ "Z"

To quote ECMA-262: Date Time String Format

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ


sss is the number of complete milliseconds since the start of the second as three decimal digits.

Both the "." and the milliseconds field may be omitted.

Z is the time zone offset specified as "Z" (for UTC) or either "+" or "-" followed by a time expression hh:mm

hvr commented Feb 16, 2011

Maybe %T should be preferred over %X

%T same as %H:%M:%S

%X as timeFmt locale (e.g. %H:%M:%S)

bos commented Mar 10, 2011

I think I have a better fix in 055a7f7 - thanks!

hvr commented Mar 10, 2011

silly me for not seeing the simpler obvious solution :-)

@tibbe tibbe pushed a commit that referenced this issue Nov 30, 2012
@bos A better fix for issue #1 055a7f7
@tolysz tolysz pushed a commit to tolysz/aeson that referenced this issue May 18, 2015
@bos A better fix for issue #1 09faa3f
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.