Skip to content

time: inconsistent handling timezone offset for a location #35152

@w0st

Description

@w0st

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

go version go1.13.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/w0st/.cache/go-build"
GOENV="/home/w0st/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/w0st/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/4668"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/4668/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/w0st/Projects/project/go.mod"
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-build558594662=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Hello, I want to convert local DateTime by parsing string (without having information about the timezone) and location string (like Europe/Paris) to a DateTime following ISO standard (with correct timezone information)

I'm trying this approach:

location, _ := time.LoadLocation("Europe/Paris")
parsedTime, _ := time.Parse("2006-01-02T15:04", "2019-10-25T13:45")

_, offset := time.Now().In(location).Zone()

result := parsedTime.Add(-time.Second * time.Duration(offset)).In(location).Format("2006-01-02T15:04:00-07:00")

fmt.Printf("Offset: %d\n", offset)
fmt.Printf("Result: %s\n", result)

I tested this on different versions of Go and the results aren't the same.

https://repl.it/repls/MediumpurpleObviousPriority
https://play.golang.org/p/WSG8Nb8R0TF

What am I'm doing wrong?

What did you expect to see?

Consistent outputs regardless Go version.

What did you see instead?

https://repl.it/repls/MediumpurpleObviousPriority
https://play.golang.org/p/WSG8Nb8R0TF

Offset: 7200
Result: 2019-10-25T13:45:00+02:00

or

Offset: 3600
Result: 2019-10-25T14:45:00+02:00

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions