json time format discrepancy

115 views
Skip to first unread message

Andrey Tcherepanov

unread,
Oct 8, 2019, 1:27:34 PM10/8/19
to golang-nuts
Fellas,

Would someone kind to point me to a reasoning behind "func (t Time) MarshalJSON() ([]byte, error)" use "time.RFC3339Nano" formatting to spit out time in JSON, and 
"func (t *Time) UnmarshalJSON(data []byte) error " uses "time.RFC3339" to parse the same timestamp? I do expect serialization/deserialization roundtrip to return exactly the same 
data as it was at the beginning. In this case it loses subsecond precision, and it just bit me (doctor says I will live).

Is it a bug or some historically-important reasons that I am missing are in play there?

Thank you in advance!

Andrey


Jakob Borg

unread,
Oct 9, 2019, 1:29:29 AM10/9/19
to Andrey Tcherepanov, golang-nuts
On 8 Oct 2019, at 19:27, Andrey Tcherepanov <xnow4f...@sneakemail.com> wrote:

In this case it loses subsecond precision, and it just bit me (doctor says I will live).

Is it a bug or some historically-important reasons that I am missing are in play there?

I don't see this discrepancy: https://play.golang.org/p/uwpnmpcc_Bw

Can you show your case?

//jb

Anderson Queiroz

unread,
Oct 9, 2019, 10:25:05 AM10/9/19
to golang-nuts
Indeed looks odd, but as Jakob showed, it looks fine. I was playing directly with the marshal/unmarshall and I didn't fine any problem either.
My playground: https://play.golang.org/p/LLmQqxLKfME

Andrey Tcherepanov

unread,
Oct 9, 2019, 11:28:40 AM10/9/19
to golang-nuts
Thanks Jakob. I am shamefully have to concur that I indeed messed up. Now I need to find where did I do it though :)

Still curious - how RFC3339 works with parsing nanoseconds past decimal point?

Andrey

Andrey Tcherepanov

unread,
Oct 9, 2019, 12:16:06 PM10/9/19
to golang-nuts
Thanks Anderson, I think I got a classical PEBKAC problem here, so all is good now.
Reply all
Reply to author
Forward
0 new messages