-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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 doesn't seem to parse the abbreviated timezone as expected #56528
Comments
Totally agree! |
Same result in a European timezone, but I got the right result in a Pacific timezone
In Pacific timezone:
|
After parsing the time string, you will get a Quoting the documentation for
Closing because this is working as documented. |
In my opinion this behaviour is unusual to say the least. It effectively means that the correctness of the parsing depends on your current location.
Anyway, can you please help me how to work around this limitation? Say you have a list of timestamps in this format: "Jan 2, 2006 at 3:04pm (MST)". How can you parse them correctly? |
The document quoted by ianlancetaylor@ has pointed out that you can use Please note that a zone abbreviation alone is not enough to determine the time zone offset sometimes. For example, package main
import (
"fmt"
"time"
)
func main() {
parseIn("Asia/Shanghai")
parseIn("America/Chicago")
parseIn("America/Havana")
}
func parseIn(locationName string) {
loc, err := time.LoadLocation(locationName)
if err != nil {
panic(err)
}
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, err := time.ParseInLocation(longForm, "Feb 3, 2013 at 7:54pm (CST)", loc)
if err != nil {
panic(err)
}
fmt.Printf("%15s: %v\n", loc, t)
} The output:
If you need to parse a time layout with arbitrary zone abbreviation, I think the solution is to maintain a mapping from zone abbreviation to location. Then for each input, read the zone abbreviation first, load the location for that zone abbreviation, and finally parse the input in that location. |
@ZekeLu Thank you for the example. I didn't know that the zone abbreviation alone is not enough to determine the time zone. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I ran the first example in the example under https://pkg.go.dev/time#Parse both in the Go Playground and on my local machine:
What did you expect to see?
What did you see instead?
https://github.com/golang/go/blob/master/src/time/example_test.go#L375 also seems to fail with this:
The text was updated successfully, but these errors were encountered: