Detect if ZONEINFO fails in Go
Asked Answered
D

1

3

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.

Disputation answered 27/6, 2018 at 1:27 Comment(0)
T
2

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
Trice answered 27/6, 2018 at 5:5 Comment(4)
Thanks @chuckx. Let me layout my understanding of the proposed solution to see if I understand correctly. 1. Make a local version of loadTzinfoFromZip that I can use since this method is not exported 2. Use that method to get the []bytes of the timeinfo.zip file for the version I want to use 3. Use LoadLocationFromTZData exclusively throughout my code instead of LoadLocationDisputation
Sounds about right. Extracted the steps into the answer.Trice
Thanks. I'll try it out and report back.Disputation
It turns out that getting the correct bytes out of the zip file was a real pain so instead I went with a strategy of extracting the zip and then it becomes much easier to get the bytes out of the correct time zone file. I created a repo for this that I'll link to from my question.Disputation

© 2022 - 2024 — McMap. All rights reserved.