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: parseTimeZone() doesn't work for for many Zones formatted by Time.Format() #24071

MyChaOS87 opened this issue Feb 23, 2018 · 4 comments


Copy link

@MyChaOS87 MyChaOS87 commented Feb 23, 2018

Please answer these questions before submitting your issue. Thanks!

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


Does this issue reproduce with the latest release?


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

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build708812266=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (

func main() {
	loc, err := time.LoadLocation("Europe/Istanbul")
	if err != nil {
		panic("Cannot load Timezone: " + err.Error())

	timeStamp := time.Date(2018, 02, 23, 0, 0, 0, 0, loc)

	layout := "2006-01-02 15:04:05 MST"
	_, err = time.Parse(layout, timeStamp.Format(layout))

	if err != nil {
		panic("Cannot Parse Time: " + err.Error())

Example in Go Playground
some Timezones from IANA database do get parsed correctly (eg. Europe/Istanbul)
see tz database +03 is used as timezone:

there are more Timezones using times Like this one

What did you expect to see?

no panic, as I assume that the time package should be able to read every timestamp it produces with the same layout string

What did you see instead?

panic: Cannot Parse Time: parsing time "2018-02-23 00:00:00 +03" as "2006-01-02 15:04:05 MST": cannot parse "+03" as "MST"

@ianlancetaylor ianlancetaylor changed the title time.parseTimeZone() doesn't work for for many Zones formatted by Time.Format() time: parseTimeZone() doesn't work for for many Zones formatted by Time.Format() Feb 23, 2018
Copy link

@ianlancetaylor ianlancetaylor commented Feb 23, 2018

The timezone handling by the time package is entirely dependent on the files supplied by your operating system. As you have shown, due to changes in the way that Turkey handles time zones, the files on your system print some dates in the Europe/Istanbul timezone using +03, but there is no timezone with that abbreviation. As the docs for time.Parse say, using MST in the parse string only matches defined timezones, and there is no timezone named +03 on your system.

It is not a goal that time.Time.Format and time.Parse be exact reverses of each other. In general, that is impossible, as is demonstrated by this case.

Use time.ParseInLocation instead.

Closing because there is nothing we can do.

Copy link

@MyChaOS87 MyChaOS87 commented Feb 23, 2018

Actually that is not completely true, as it can be matched on the system and the error is generated before trying to match it.

time.Parse calls time.ParseTimeZone() which then rejects this as it does not fit into the scheme having at least three upper case letters and a T in the end, see:

adding a third special case like:
if value[:1] == "+" || value[:1] == "-" { return 3, true }

and packing my example into a test in that package shows that this is easily solvable.

I am also willing to fix that using a bit more sophisticated special case then shown above, if this bug gets accepted.

Copy link

@ianlancetaylor ianlancetaylor commented Feb 23, 2018

OK, sure, let's try that.

Copy link

@gopherbot gopherbot commented Mar 2, 2018

Change mentions this issue: time: add support for parsing timezones denoted by sign and offset

@andybons andybons added the NeedsFix label Mar 7, 2018
@andybons andybons added this to the Go1.11 milestone Mar 7, 2018
@gopherbot gopherbot closed this in 9f2c611 Mar 8, 2018
@golang golang locked and limited conversation to collaborators Mar 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.