json.Unmarshal uses RFC3339 to decode datetime but why mine is not working?

4,226 views
Skip to first unread message

Qiang Han

unread,
Feb 3, 2014, 3:48:18 AM2/3/14
to golan...@googlegroups.com
package main

import "fmt"
import "time"
import "encoding/json"

type Foo struct {
Bar time.Time
}

func main() {
msg := "{\"bar\":\"2012-01-02T16:10:22Z07:00\"}"
var f Foo
err := json.Unmarshal([]byte(msg), &f)
if err != nil {
fmt.Printf("%+v", err)
} else {
fmt.Println(f)
}
}

output: parsing time ""2006-01-02T15:04:05Z07:00"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "07:00"" as """


Not sure what string are allowed after "Z"

Konstantin Khomoutov

unread,
Feb 3, 2014, 4:03:31 AM2/3/14
to Qiang Han, golan...@googlegroups.com
JSON does not define any representation for encoding date/time
information.

It *appears* that people tend to use ISO-8601 for them [1].
May be that's what Go does.
Note that the documentation for the encoding/json package does not
mention dates and times at all.

1. http://en.wikipedia.org/wiki/ISO_8601

Vasiliy

unread,
Feb 3, 2014, 5:09:04 AM2/3/14
to golan...@googlegroups.com
Because your string is invalid. Timezone must be either in format
`±hh:mm` or be a single letter `Z` which means UTC. Everything works
fine if you remove `07:00` after `Z` or replace `Z` with `+` or `-`.

On Mon, 3 Feb 2014 00:48:18 -0800 (PST)
Qiang Han <qjavasw...@gmail.com> wrote:

Qiang Han

unread,
Feb 3, 2014, 12:46:20 PM2/3/14
to golan...@googlegroups.com
Make sense.

http://golang.org/pkg/time/#pkg-constants and the miss-leading error message: parsing time ""2006-01-02T15:04:05Z07:00"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "07:00"" as """ make me think that it is parsed as RFC3339 format.

Matt Harden

unread,
Feb 9, 2014, 2:09:30 PM2/9/14
to Qiang Han, golang-nuts


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

tianzhaixi...@gmail.com

unread,
Jul 6, 2017, 8:11:07 AM7/6/17
to golang-nuts
thanks very much.

在 2014年2月3日星期一 UTC+8下午4:48:18,Qiang Han写道:
Reply all
Reply to author
Forward
0 new messages