Skip to content

time: ParseInLocation may return time with a location that yields an empty string #71347

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

Closed
aksdb opened this issue Jan 20, 2025 · 2 comments
Closed

Comments

@aksdb
Copy link

aksdb commented Jan 20, 2025

Go version

1.23

Output of go env in your module/workspace:

[current playground]

What did you do?

I parse a time string using a known location name.
The resulting time ends up with the location name being empty, though.

	location, err := time.LoadLocation("Europe/Helsinki")
	if err != nil {
		panic(err)
	}
	t, err := time.ParseInLocation(time.RFC3339, "2022-09-29T13:00:00+02:00", location)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Expected: %s, Got: %s\n", location.String(), t.Location().String())

https://go.dev/play/p/WVdVLmBjkuL

What did you see happen?

The given example prints: Expected: Europe/Helsinki, Got:

What did you expect to see?

The given example prints: Expected: Europe/Helsinki, Got: Europe/Helsinki

@seankhliao seankhliao changed the title time.ParseInLocation may return time with a location that yields an empty string time: ParseInLocation may return time with a location that yields an empty string Jan 20, 2025
@seankhliao
Copy link
Member

I believe that's correct, it's an unnamed location with a fixed time offset of 2 hours (7200 secs).
It doesn't match Europe/Helsinki because at that time, Helsinki observes DST with an offset of +0300.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 20, 2025
@aksdb
Copy link
Author

aksdb commented Jan 21, 2025

Ah that makes sense; indeed when adjusting the parsed time to match the expected offset, it keeps the location info.
So I guess the solution to ensure the location would be to use t = t.In(location) afterwards to keep consistency.

Thanks for clarifying and sorry for the apparently unnecessary noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants