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

Closed
MyChaOS87 opened this issue Feb 23, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@MyChaOS87
Copy link

commented Feb 23, 2018

Please answer these questions before submitting your issue. Thanks!

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

1.9.4

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/kasch/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/kasch/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
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 (
	"time"
)

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: https://github.com/eggert/tz/blob/6df98f4244eaa5ef9af50d71020faa9364b52f09/europe#L3738

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

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

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.

@MyChaOS87

This comment has been minimized.

Copy link
Author

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:
https://github.com/golang/go/blob/master/src/time/format.go#L1106-L1146

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.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Feb 23, 2018

OK, sure, let's try that.

@gopherbot

This comment has been minimized.

Copy link

commented Mar 2, 2018

Change https://golang.org/cl/98157 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.
You can’t perform that action at this time.