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: Parse() does not recognize time zone MST #19694

Closed
crufter opened this issue Mar 24, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@crufter
Copy link

commented Mar 24, 2017

What version of Go are you using (go version)?

Playground one, go version go1.8 darwin/amd64, go version go1.7.3 darwin/amd64, it's all wrong on these.

What operating system and processor architecture are you using (go env)?

The go env correspoding to the go version go1.7.3 darwin/amd64 version:

GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/crufter/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nm/gx94478n3dq9478jm5ndvpqh0000gn/T/go-build541649718=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

I put the example code from the time.Parse docs on playground and I see a different output than the output in the doc. https://play.golang.org/p/tuBDbz66I0

What did you expect to see?

2013-02-03 19:54:00 -0800 PST

(Same as in documentation here: https://golang.org/pkg/time/#Parse)

What did you see instead?

2013-02-03 19:54:00 +0000 PST

@ALTree

This comment has been minimized.

Copy link
Member

commented Mar 24, 2017

The Parse documentation says:

When parsing a time with a zone abbreviation like MST, if the zone abbreviation has a defined offset in the current location, then that offset is used.

and

If the zone abbreviation is unknown, Parse records the time as being in a fabricated location with the given zone abbreviation and a zero offset

and

To avoid such problems, prefer time layouts that use a numeric zone offset, or use ParseInLocation

So you can either use the numeric offset, or call ParseInLocation:

loc, _ := time.LoadLocation("US/Pacific")
const longForm = "Jan 2, 2006 at 15:04 (MST)"
t, _ := time.ParseInLocation(longForm, "Feb 3, 2013 at 19:30 (PST)", loc)
fmt.Println(t)

2013-02-03 19:30:00 -0800 PST

I guess in the documentation it recognizes PST as -08 because it's the godoc tz?

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 24, 2017

Which timezones are recognized depends on the timezone database on your computer.

Timezone names are inherently ambiguous and should be avoided when possible for portability.

Closing because I don't think there is anything to do. Please comment if you disagree.

@crufter

This comment has been minimized.

Copy link
Author

commented Mar 29, 2017

@ianlancetaylor @ALTree

That explanation makes sense. I did not realise the timezone database is on my computer, hence I found the disparity between my setup and play.golang.org weird.

Thank you

@golang golang locked and limited conversation to collaborators Mar 29, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.