Skip to content

time: Location() reports UTC inconsistently #48493

@dallbee

Description

@dallbee

The Location() method on a Time struct reports UTC as "local" when acquired from /etc/localtime, but "UTC" when acquired from the TZ environment variable.

This seems related to:
#45960

However, this inconsistency is produced without any parse or marshal methods.

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

$ go version
go version go1.17.1 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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/dylan/.cache/go-build"
GOENV="/home/dylan/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/dylan/go/pkg/mod"
GOOS="linux"
GOPATH="/home/dylan/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.1"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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-build817213888=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Using the following program (playground link):

func main() {
    x := time.Now()
    fmt.Println(x.Location())
}
# with localtime set to UTC
$ doas ln -sf /usr/share/zoneinfo/UTC /etc/localtime

# load from /etc/localtime by unsetting TZ
$ env -u TZ go run main.go
Local

# load from TZ
$ env TZ="UTC" go run main.go
UTC
$ env TZ="" go run main.go
UTC

What did you expect to see?

Consistent location behavior between environment variable and /etc/localtime methods of setting time.

What did you see instead?

The output of Location() changes based on where it obtained the timezone information.

Additional Notes

The internal documentation on the Time struct's loc variable is incorrect:

// All UTC times are represented with loc==nil, never loc==&utcLoc.

After String(), the loc field becomes non-nil, and is set to UTC:
https://play.golang.org/p/rqgA876kFIW

These are just internal docs, so I'm not suggesting this itself is a bug. The bug report is about the Location behavior noted in prior sections.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions