How to get current IANA Timezone Database version in Golang?

810 Views Asked by At

I need to store datetime records with IANA database current version used (2022g for example). How could I get it with Go?

I tried to search this on standard "time" package, but it seems that there isn't any functionality for this. I am expecting that there is some function that can return IANA tzdb version as a string.

UPDATE 1 according to comments below I need to clarify the problem:

The main problem is I want to store some FUTURE events. The event object has several fields:

  1. Local dateTime
  2. Timezone
  3. UTC datetime

To keep my data up to date with IANA database (timezone, daylight saving time may change) I need to store current version of tzdb version. That will help me to write correct data migration of my events when new version of tzdb was released. So I need to add one more field with version of current tzdb that had been used to populate the time.

And I am trying to figure out how can I get the current version of my tzdb that Go application is using right now to store that version.

Also I am opened to alternative solutions of storing time records with extra accuracy of long-lived future events.

Update 2: This events are bounded to exact location.

1

There are 1 best solutions below

1
On

The discussion thread in the comment is pretty long, but I'll attempt to answer and address some of the concerns. (I won't address the question in the title, as I believe that is not straightforward in Go.)

  • Indeed, future scheduling of events should be in terms of the time zone where the event takes place - which is usually not UTC.

  • Time zone identifiers will never be removed or renamed (with rare exception anyway). Once introduced, the identifier will either maintained as a Zone or as a Link in the TZDB indefinitely. Thus, you don't need to check that the time zone still exists. (Windows time zone IDs are also like this.)

  • DST is only one aspect of picking the correct offset. The standard time may have changed as well. However, all of that is encapsulated in the tzdb itself. You shouldn't need to concern yourself about which version of the tzdb was in effect when you created the event.

The general approach to this issue in most cases is:

  • Store the scheduled local date, time, and time zone ID of the event (local with regard to the time zone of the event).

    Example: 2030-12-31T00:00:00[America/New_York]

  • At the time you create the event, also calculate a UTC value (or equivalent DateTimeOffset value) and store that in a separate field - so you know exactly when to fire the event:

    Example: 2030-12-31T05:00:00Z (or 2030-12-31T00:00:00-05:00)

  • Periodically check that your UTC equivalent is correct. This can be in a daily maintenance task, or on application startup, or just before the event (perhaps also an hour before the event), or all of these.

    The offset will only be different than projected if the time zone data changed on the device to give it a new offset. For example, let's hypothetically say the lawmakers in the USA succeed at making DST permanent sometime before this event takes place. Then the equivalent UTC time for the same event would now be 2030-12-31T04:00:00Z (or 2030-12-31T00:00:00-04:00).

    In such cases, update the UTC time of the event if it has changed, but the original local time of the event usually should not be modified. Human beings tend to schedule things in terms of local time, not in terms of their UTC equivalents.

The only advantage knowing the TZDB version would give you, is you could do that last step less often - only when knowing the data has changed. I see that as an optimization though - it's not usually required.

  • Without such legal changes to time zone definitions, the mere start/stop of DST as scheduled is not a reason to worry about this. That is already accounted for by using the TZDB in the first place.

    • If the event is recurring (say a 10:00 AM daily meeting), each occurrence might have a different offset, but the local time will be consistent and the TZDB doesn't need to be updated to calculate it.