6

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."

3
  • 1
    What is the value of service.Settings.TimezoneName? Add fmt.Printf("%q\n", service.Settings.TimezoneName) before this tzloc, err := time.LoadLocation(service.Settings.TimezoneName) to see the content of service.Settings.TimezoneName
    – wasmup
    Nov 26, 2019 at 5:53
  • What does log.Printf("tz = %q\n", service.Settings.TimezoneName) output? It will catch any rogue bytes in your input.
    – colm.anseo
    Nov 26, 2019 at 5:55
  • I found this answer solved my problem by adding tzdata package to alpine and setting the local timezone. stackoverflow.com/questions/68996420/…
    – Nick T
    May 24, 2022 at 17:48

5 Answers 5

16

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"]
3
  • Thanks. Solved issue for go:1.14 with alpine image, so it looks like no matter what go version it is
    – iamolegga
    Apr 13, 2021 at 8:27
  • This solved the issue for me too, go.1.17. Thanks!
    – popstr
    Apr 5, 2022 at 7:39
  • tested on golang:1.18-alpine Aug 22, 2022 at 14:59
4

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.

1

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.

3
  • Yeah I know. But the thing is it works or fails depending on whether I compile with 1.12 or 1.13. Nov 27, 2019 at 3:32
  • zoneinfo file will not compile into the binary, check the server you run the program @seesharper
    – zzn
    Nov 27, 2019 at 3:37
  • Yes it's there. What I meant was that it works in 1.12, so the zoneinfo file must be there. Nov 27, 2019 at 5:48
1

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)
}
0

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

1
  • Thanks, I'll check this once I get a chance and mark as answer if you're right. Nov 26, 2019 at 22:42

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.