Search code examples
gotimezonezoneinfo

Detect if ZONEINFO fails in Go


In Go, you can specify a specific zoneinfo.zip file to be used by adding a ZONEINFO environment variable that points to the specific file you'd like to use for time zone information. This is great as it allows me to ensure that the versions of the IANA time zone database that I'm using on my front-end and my back-end are the same.

However, there does not appear to be any way to detect if use of the specified zone info file has failed. Looking at the source code (https://golang.org/src/time/zoneinfo.go), it looks like any errors using the specified file will fail quietly and then go will proceed to check for default OS locations or the default $GOROOT location to pull time zone information from there. This is not the behavior that I would prefer as I would like to know with certainty that I am using my specified version of zone info.

I've thought of the following in terms of solutions, but I'm not happy with any of them.

  1. I can check that the environment variable is set myself, but this is at best a partial solution as it doesn't tell me if the file is actually usable by LoadLocation.
  2. I can ensure none of the backup locations for zone info exist. This seems a bit extreme and means that I have to be extremely careful about the environment that the code is running in in both dev and production settings.

Does anyone know of a more elegant way to ensure that I am using the zoneinfo.zip file specified by my ZONEINFO environment variable?

Update: To address this problem I too inspiration from @chuckx's answer below and put together a Go package that takes the guess work out of which time zone database is being used. Included in the readme are instructions on how to get the correct version of the time zone database using a Go installation.


Solution

  • Maybe consider not relying on the environment variable?

    If you're not averse to distributing the unzipped fileset, you can easily use LoadLocationFromTZData(name string, data []byte). The second argument is the contents of an individual timezone file.

    For reference, the functionality for processing a zip file is found in the unexported function loadTzinfoFromZip().

    Step-by-step approach extracted from @Slotherooo's comment:

    1. Make a local version of time.loadTzinfoFromZip(zipfile, name string) ([]byte, error)
    2. Use that method to extract the []byte for the desired location from a timeinfo.zip file
    3. Use time.LoadLocationFromTZData() exclusively instead of time.LoadLocation