Search code examples
c++timezonec++-chrono

what is the use of tzdb_list


I have seen the reference of std::chrono::tzdb_list that

tzdb_list is a singleton list of std::chrono::tzdbs, each of which represents a copy of the IANA time zone database.

Why do we need a list like this.

When I try to use this list, it only have one item and I can not modify it. I already use tzdb for time zone.

The list seems to be useless


Solution

  • You are almost correct. std::chrono::tzdb_list is very nearly useless! :-)

    But when you need it, you really, really need it.

    A very few applications need to run for months, maybe even years at a time. And these applications need the very latest time zone database from IANA. And these applications are often highly multithreaded.

    Think airline reservations systems.

    Such an applications might have multiple threads accessing the time zone database simultaneously and need to upgrade from one version of the time zone database to the next, without stopping and restarting the application.

    This is where std::chrono::tzdb_list can save the day. On some OS's, the function std::chrono::reload_tzdb() will push a new database onto the front of the atomic, singly linked list that is std::chrono::tzdb_list if a newer version exists. This allows an application to migrate from the older version of the time zone database to a newer version, at its own pace, however it can manage that. For example the application might have its own system of atomic flags and mutexes for managing such a transition.

    Here is a brief article that describes exactly how an application might periodically check for the existence of a new database, and add one if found. The article was written for the open-source precursor to C++20, but should easily translate to C++20.

    Here is the specification for the functions that access the time zone databases.