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 inappropriately sets Location() to Local in some circumstances #19750
Comments
Just to be clear, my timezone is Europe/London, aka. BST, aka. +0100 |
time.Parse
inappropriately sets Location() to Local in some circumstances
I can reproduce on Linux or Mac, with:
|
Here's the relevant code: if zoneOffset != -1 {
t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
t.addSec(-int64(zoneOffset))
// Look for local zone with the given offset.
// If that zone was in effect at the given time, use it.
name, offset, _, _, _ := local.lookup(t.unixSec())
if offset == zoneOffset && (zoneName == "" || name == zoneName) {
t.setLoc(local)
return t, nil
}
// Otherwise create fake zone to record offset.
t.setLoc(FixedZone(zoneName, zoneOffset))
return t, nil
} It's trying to map the timezone to your local timezone if your local timezone was applicable for that time. In this case, it's looking to see whether Unix time Looking at a fix. |
More particularly, Unix time -62167219200 in Europe/London was GMT, but somewhere before Unix time 0 (sometime in the following 1970 years), daylight savings time was invented and it was now British Summer Time, with offset 3600 seconds forward, which didn't match your +0000, so your local time zone didn't apply then. I'm tempted to ignore this rule before December 1, 1847-ish, when time zones first started being used. Or I'll see what the earliest recorded zone in the Olson database is. Oh, that's just from 1970. Easy enough. |
CL https://golang.org/cl/44032 mentions this issue. |
Moving discussion from the CL back to the issue. I said:
@bradfitz replied:
To which I say: Yes, it is weird, but an arbitrary cutoff at a point in historical time is also weird, whether the cutoff is 1970 (start of Unix time) or 1847 (invention of timezones). I think the behavior of But the current behavior does seem to match the docs, so if we change this, we should change the docs. And if we approach it from that perspective, what do we want the docs to say? |
Yeah, the more I think about it, the more I dislike an arbitrary cutoff and more docs too. The current behavior is documented and there's a workaround for people who really care: I'm happy closing this. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.8 darwin/amd64
I can also see this on go1.6.2 darwin/amd64 and go.7.4 linux/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
Here's an example on the playground: https://play.golang.org/p/ND8CEYOKcT
The correct behaviour is actually seen when this example is run on playground, so I've included the output I see on my own machine.
What did you expect to see?
time.Parse("-0700", "+0000")
should return a time.Time with a Location() with an offset of +0000 in all casesWhat did you see instead?
time.Local was used instead, resulting in the timezone changing from +0000 to +0100 on my local machine.
Adding the year to the parse string is enough to get the desired behaviour
The text was updated successfully, but these errors were encountered: