see sharper
see sharper

Reputation: 12055

Timezones failing to load in Go 1.13

This simple code:

tzloc, err := time.LoadLocation(service.Settings.TimezoneName)
if err != nil {
    panic(err)
}

works just fine in Go 1.12, but in 1.13, it fails with "unknown timezone Australia/Melbourne". I'm sure it works in some environments, but for whatever reason, it is broken in 1.13 for us. It is immediately fixed by reverting to 1.12. I am wondering if anyone knows of any issues or reasons in 1.13 why this would fail despite this assurance from the release notes: "As always, the release maintains the Go 1 promise of compatibility. We expect almost all Go programs to continue to compile and run as before."

Upvotes: 8

Views: 14485

Answers (6)

Xavier-Lam
Xavier-Lam

Reputation: 513

In our case, our QA uses Windows 7 to test our application, they don't install go environment in their machine. We added a build tag timetzdata when we build executable file for them to solve the problem.

go build -tags timetzdata [your other options]

https://github.com/golang/go/issues/38017

Upvotes: 0

Amid
Amid

Reputation: 631

Importing time/tzdata will cause timezone information to be embedded in the program. So if the time package cannot find tzdata files on the system, it will use this embedded information.

Keep in mind that based on the documentation:

Importing this package will increase the size of a program by about 450 KB.

A sample code could be like this:

package main

import (
    "fmt"
    "time"
    _ "time/tzdata"
)

func main() {
    loc, err := time.LoadLocation("Australia/Melbourne")
    if err != nil {
        panic(err)
    }
    fmt.Println(loc)
}

Upvotes: 8

Ankur Agarwal
Ankur Agarwal

Reputation: 837

For docker, the solution by adding a tzdata or equivalent package. This solve the issue when using go 1.13

http://pouzek.si/blog/go-loadlocation-docker/

FROM gliderlabs/alpine:3.3

RUN apk add --no-cache tzdata

COPY out/go-loadlocation-docker /go-loadlocation-docker

ENTRYPOINT ["/go-loadlocation-docker"]

Upvotes: 15

fussmonkey
fussmonkey

Reputation: 678

This happened to us, where we are bundling the zoneinfo.zip file with our program because we need to run this on Windows servers. We set the ZONEINFO environment variable to the path with the .zip file and then call time.LoadLocation("America/Chicago"). In version 1.12 this was fine. We upgraded to version 1.13 and had an issue of "The system cannot find the path specified." when calling LoadLocation.

In our case, updating the bundled zoneinfo.zip file to the latest version from the golang repo fixed the issue. The version we used previously was from commit af3c48096. Not sure if this was the same problem you ran into, since it's unclear if you were deploying that zip file like we are, but thought I'd chime in for anyone else running into the issue.

Note: this only affected situations where Go version 1.13 wasn't installed - when I ran it locally, things were fine because it was falling back to the version in the $GOROOT folder (which was newer from when 1.13 was installed). The error happened on a Windows machine without Go installed, and also locally if I removed the $GOROOT\lib\time\zoneinfo.zip file.

Upvotes: 4

zzn
zzn

Reputation: 2465

if you are passing exactly Australia/Melbourne to time.LoadLocation, then you should check the zoneinfo file.

https://golang.org/pkg/time/#LoadLocation

the name is taken to be a location name corresponding to a file in the IANA Time Zone database.

The time zone database needed by LoadLocation may not be present on all systems, especially non-Unix systems. LoadLocation looks in the directory or uncompressed zip file named by the ZONEINFO environment variable, if any, then looks in known installation locations on Unix systems, and finally looks in $GOROOT/lib/time/zoneinfo.zip.

Upvotes: 1

SouvikMaji
SouvikMaji

Reputation: 1145

Test with a hardcoded timezone string in time.LoadLocation. I suspect there might be whitespaces in your TimezoneName variable.

Otherwise LoadLocation is working fine as expected in Go1.13.

package main

import (
    "fmt"
    "time"
)

func main() {
    location, err := time.LoadLocation("Australia/Melbourne")
    if err != nil {
        panic(err)
    }

    timeInUTC := time.Date(2018, 8, 30, 12, 0, 0, 0, time.UTC)
    fmt.Println(timeInUTC.In(location))
}

The above code is giving 2018-08-30 22:00:00 +1000 AEST as output in Go Playground. Go Playground uses go 1.13.

But if I put a space around

location, err := time.LoadLocation("Australia/Melbourne ")
if err != nil {
     panic(err)
}

It gives the error you are having.

panic: unknown time zone Australia/Melbourne

Upvotes: 0

Related Questions