DrP3pp3r
DrP3pp3r

Reputation: 857

Howard Hinnant's date library: Only load part of the IANA Time Zone Database

I'm working on an embedded system with limited RAM. Also, the used toolchain currently supports C++17 at the most.

I have to convert UTC timestamps to local time stamps for displaying data to the user. The time zone that is supposed to be used is retrieved from another system. This all works excellently with Howard Hinnant's date and tz libraries.

In his excellent talk at CppCon 2016 Howard Hinnant mentions that the loaded DB roughly takes up 800 KB of memory. He also mentions that there is a way to load only part of the data and that it used to be documented somewhere.

As I do not need the ability to look back far into the past, that would be a great opportunity to save some RAM.

I cannot find the information on how to only partially load the database. Neither on github nor in any of the resources that are linked there or via Google.

Upvotes: 2

Views: 236

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 218880

Caution: These tips have not been tested recently!

1. Temporal Window

src/tz.cpp has:

// These can be used to reduce the range of the database to save memory
CONSTDATA auto min_year = date::year::min();
CONSTDATA auto max_year = date::year::max();

The idea when I wrote this 5 years ago was that you could set min_year and max_year and the code would skip database entries outside of this temporal window. This was written prior to the implementation of the USE_OS_TZDB option (which reads the OS-supplied database as opposed to the IANA text database). So it has never been tested with USE_OS_TZDB=1.

2. Geographical Window

src/tz.cpp has:

CONSTDATA char*const files[] =
{
    "africa", "antarctica", "asia", "australasia", "backward", "etcetera", "europe",
    "pacificnew", "northamerica", "southamerica", "systemv", "leapseconds"
};

When reading the text version of the database (USE_OS_TZDB=0), this is the list of text database files it reads. Remove whichever files are not useful to your application.

Upvotes: 3

Related Questions