Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: doesn't parse RFC3339 properly #9346

Closed
aniketawati opened this issue Dec 16, 2014 · 5 comments
Closed

time: doesn't parse RFC3339 properly #9346

aniketawati opened this issue Dec 16, 2014 · 5 comments

Comments

@aniketawati
Copy link

Time can't parse the layout constant for RFC3339 in the code itself.

For eg. Go playground link - http://play.golang.org/p/EJH53M__AH

    // Time value taken from go source https://github.com/golang/go/blob/master/src/time/format.go#L51
    layout := "2006-01-02T15:04:05Z07:00" // RFC3339
    tm, err := time.Parse(layout, layout) 
    if err != nil {
        fmt.Println("error", err)
        return
    }
    fmt.Println("Parsed time", tm)

Currently if there is any offset in time (non UTC time), time formats it in RFC3339 as - +07:00 instead of Z07:00

If Z07:00 is given, it gives an error

error parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00

@nickcarenza
Copy link

@aniketawati How did you resolve this?

@benbariteau
Copy link

For posterity: the time package docs describe this layout conversion with the Z (near the bottom of the linked section), which is that it shows "Z" instead of a time offset if the timezone is UTC, and +hh:mm or -hh:mm otherwise. Since this is one of the few examples of the reference time format not being an actually valid time, I can see the confusion.

@mikioh mikioh changed the title time doesn't parse RFC3339 properly time: doesn't parse RFC3339 properly Aug 29, 2015
@mdwhatcott
Copy link

Maybe the docs should be updated to reflect the insight shared by @firba1 ??

@ianlancetaylor
Copy link
Contributor

@mdwhatcott firba1 is citing the docs, so I'm not sure what to change. I would encourage you to send in a change if you see how to make the docs better (https://golang.org/doc/contribute.html).

@mdwhatcott
Copy link

Thanks for the encouragement @ianlancetaylor. I received a good answer on stackoverflow just today that was more helpful for me than the docs, probably because it used complete real examples and was specific to the particular layout string I was using:

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.

@golang golang locked and limited conversation to collaborators Sep 22, 2016
gopherbot pushed a commit that referenced this issue Dec 6, 2017
For #9346 #22135 explicitly state under layout constants
that they are not valid time values for Parse. Also add
examples of parsing valid RFC3339 values and the layout
to the example for time.Parse.

Fix capitalisation of time.Parse and Time.Format.

For #20869 include RFC3339 in the list of layouts that do
not accept all the time formats allowed by RFCs (lowercase z).
This does not fully address #20869.

Fixes #9346
Fixes #22135

Change-Id: Ia4c13e5745de583db5ef7d5b1688d7768bc42c1b
Reviewed-on: https://go-review.googlesource.com/74231
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants