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: parsing a time with UnixDate layout ignores time zone #11815

Closed
mmb opened this issue Jul 22, 2015 · 4 comments

Comments

Projects
None yet
4 participants
@mmb
Copy link

commented Jul 22, 2015

Create two times that differ only by their time zone. They both return the same value for Unix timestamp even though they are not the same time.

Example:

package main

import "fmt"
import "time"

func main() {
    time1, _ := time.Parse(time.UnixDate, "Mon Jan 2 15:04:05 MST 2006")
    time2, _ := time.Parse(time.UnixDate, "Mon Jan 2 15:04:05 PST 2006")

    fmt.Println(time1.Unix())
    fmt.Println(time2.Unix())
}

Output:

1136214245
1136214245

@ianlancetaylor ianlancetaylor changed the title parsing a time with UnixDate layout ignores time zone time: parsing a time with UnixDate layout ignores time zone Jul 22, 2015

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 22, 2015

Time zone names like MST and PST are parsed based on your local timezone information. On my Ubuntu GNU/Linux system, I get different values because the files /usr/share/zoneinfo/MST and /usr/share/zoneinfo/PST8PDT both exist. If files like those do not exist on your system, then the results are expected.

I'm closing this as expected, but please feel free to reopen if you disagree.

@mmb

This comment has been minimized.

Copy link
Author

commented Jul 22, 2015

I get a different value on OSX, but on the Go Playground it gives the same value.

I was expecting a parse error if I gave time.Parse a time zone that the system did not know about. It seems like you can choose any three letters and it will not error and use the local time zone, which is probably not what the user intended.

@cespare

This comment has been minimized.

Copy link
Contributor

commented Jul 22, 2015

From the time docs:

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. The zone abbreviation "UTC" is recognized as UTC regardless of location. 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. This choice means that such a time can be parsed and reformatted with the same layout losslessly, but the exact instant used in the representation will differ by the actual zone offset. To avoid such problems, prefer time layouts that use a numeric zone offset, or use ParseInLocation.

@mmb

This comment has been minimized.

Copy link
Author

commented Jul 22, 2015

I missed this in the doc. Thanks for pointing it out.

@golang golang locked and limited conversation to collaborators Aug 5, 2016

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.