Brutezone is a C library for computing the time in IANA timezones. It uses a lookup table to get the UTC offset of the timezone. The code for the lookup table generator (written in C#) is included in the project; you can shrink the lookup table if desired.
The UTC offsets are stored in a lookup table. This library is only valid for UTC times between 1 January 1970 and 19 January 2038.
If your application is memory-constrained (like in some embedded systems), you can limit the time-range of the database by defining the minimum and/or the maximum times you care about as UTC time_t for BRUTEZONE_MIN_TIME or BRUTEZONE_MAX_TIME during compilation.
If you are running this library on an embedded device, ensure you have clock synchronization implemented.
time_t timezone_local_time- Translates the UTC timegmtinto the time in the timezone namedtimezonetime_t timezone_current_local_time- Returns the current local time in the timezone namedtimezonetime_t timezone_gmt_time- Convertslocal_timefrom the timezone namedtimezoneto UTC time
int secs_to_tm- Converts a tick counter (such astime_t) to a time structurelong long tm_to_secs- Converts a time structure to a tick counter
void add_day- Adds a dayvoid subtract_day- Subtracts a day
time_t timezone_secs_until- Returns the number of seconds until the next occurance ofhour:minute:secondin the timezone namedtimezonetime_t timezone_secs_since- Returns the number of seconds since the last occurance ofhour:minute:secondin the timezone namedtimezone
time_t timezone_secs_until_dow- Returns the number of seconds until the next occurance ofhour:minute:secondonwday(day of the week) in the timezone namedtimezonetime_t timezone_secs_since_dow- Returns the number of seconds since the last occurance ofhour:minute:secondonwday(day of the week) in the timezone namedtimezone
iso_time- Populates thestrinput with the ISO 8601 timestamp of thedatetimeargument. Example output:1970-01-29T10:03:17for 29 January 1970 at 10:03:17 AM.from_iso_time- Returns the time structure from the given timestamp.
See the test folder for examples of all of the functions.
The library can be compiled through the standard build process:
mkdir build
cd build
cmake ../
makeOnce the library is built, you can test it by running
make test
If you have Visual Studio and/or MonoDevelop installed, you can build the generator by running
make generator
Once you have build the generator, you can rebuild the timezone table by running
make database
We needed a simple-to-use, compact library for an alarm clock on an embedded system. Brutezone was the result. It's called Brutezone because the UTC offsets are calculated for each timezone using brute force (and NodaTime)
The functions used to translate between time_t and struct tm were borrowed from musl
The UTC offset lookup table was derived using the NodaTime library.