22

I know I need to use a time layout in Go (as shown here https://golang.org/src/time/format.go) but I can't find the layout for an ISO 8601 timestamp.

If it helps, I get the timestamp from the Facebook API. Here is an example timestamp: 2016-07-25T02:22:33+0000

Jonathan Allen Grant
  • 3,408
  • 6
  • 30
  • 53

4 Answers4

24

I found this layout to work: "2006-01-02T15:04:05-0700"

Jonathan Allen Grant
  • 3,408
  • 6
  • 30
  • 53
12

The problem here is that RFC3339 requires the zone offset to be given as "+00:00" (or "Z" in case of UTC) while ISO8601 allows it to be "+0000".

From RFC3339:

[...]

time-numoffset  = ("+" / "-") time-hour ":" time-minute
time-offset     = "Z" / time-numoffset

[...]

full-time       = partial-time time-offset
date-time       = full-date "T" full-time

So instead of the time.RFC3339 layout

"2006-01-02T15:04:05Z07:00"

you have to use:

"2006-01-02T15:04:05Z0700"
Community
  • 1
  • 1
AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • There is a problem with your examples. From your quote of the RFC, it's either "2006-01-02T15:04:05Z" or "2006-01-02T15:04:05+07:00". You can't have both "Z" and a timezone offset. – thibpat Jul 06 '18 at 12:58
  • 2
    @thibpat In a timestamp you can't, but in a *layout string* you can. The "Z" in that case [means](https://golang.org/pkg/time/#pkg-constants) that a "Z" should be printed **instead** of an offset when the time zone is UTC. – AndreKR Jul 06 '18 at 16:26
  • Thanks for the precision, that makes a lot more sense, sorry for not catching it right away. – thibpat Jul 09 '18 at 19:30
2

RFC3339 is equivalent to ISO 8601. Specifically, it has identical format, RFC3339 just has stricter requirements (example, it requires a complete date representation with 4-digit year).

What's the difference between ISO 8601 and RFC 3339 Date Formats?

So you can use the constant time.RFC3339 as your layout.

Community
  • 1
  • 1
Kaedys
  • 9,600
  • 1
  • 33
  • 40
  • @TravisSmith Take a look at this [issue](https://github.com/golang/go/issues/9346) – nouney Aug 15 '17 at 15:31
  • That issue is about not understanding what "Z07:00" means in the layout, but here the problem is about the colon. – AndreKR Sep 07 '17 at 11:58
  • 3
    RFC 3339 is NOT equivalent to ISO 8601; it is a subset of the allowed formats of ISO 8601. Notably, using time.RFC3339 fails to parse 20060102T150405Z correctly. – David Cuthbert Jan 08 '20 at 00:40
0

You have that to use the some layout of your date.

 data   := "2022-01-24T00:00:00.000-03:00"
 layout := "2006-01-02T15:04:05.000-03:00"

 dataTime, err :=  time.Parse(layout, data)