Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't go parse the time represented by the provided formats?

Tags:

time

parsing

go

Consider this example:

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Parse(time.RFC3339, time.RFC3339))
}

The output is:

0001-01-01 00:00:00 +0000 UTC parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00

Why can't time.Parse() handle a layout as a value? What's missing here?


UPDATE: Cutting off the time zone value (but not the 'Z' delimiting the time from the zone) fixes it:

fmt.Println(time.Parse(time.RFC3339, "2015-09-15T11:50:00Z"))

Why can't time.Parse() handle time zone info when using time.RFC3339 as the layout string?

http://play.golang.org/p/p3fHfJNHVK


UPDATE: JimB's answer led me to read from RFC3339 and I found these examples that clarify further:

Here are some examples of Internet date/time format.

1985-04-12T23:20:50.52Z

This represents 20 minutes and 50.52 seconds after the 23rd hour of April 12th, 1985 in UTC.

1996-12-19T16:39:57-08:00

This represents 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC (Pacific Standard Time). Note that this is equivalent to 1996-12-20T00:39:57Z in UTC.

like image 367
mdwhatcott Avatar asked Sep 15 '15 17:09

mdwhatcott


People also ask

How to format datetime in Golang?

Golang Time Format YYYY-MM-DD.

How do you store dates in go?

The month comes first at 01 , followed by the day of the month at 02 , then the hour at 03 , the minute at 04 , the second at 05 , the year at 06 (or 2006 ) and, finally, the time zone at 07 . Remembering this order will make it easier to create date and time formats in the future.


1 Answers

The time.RFC3339 format is a case where the format string itself isn't a valid time. You can't have a Z and an offset in the time string, but the format string has both because the spec can contain either type of timezone specification.

Both of these are valid RFC3339 times:

"2015-09-15T14:00:12-00:00"
"2015-09-15T14:00:13Z"

And the time package needs to be able to parse them both using the same RFC3339 format string.

like image 122
JimB Avatar answered Sep 29 '22 18:09

JimB