Retrieving DST Parameters from IANA TZif2 Files in C99
The IANA Time Zone database (often stored in /etc/localtime) uses the TZif2 file format to store time zone information, including Daylight Saving Time (DST) rules. Here's a C99 program that reads and parses this file to extract DST parameters:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
#include <endian.h>
#define TZ_MAGIC "TZif"
#define TZ_MAGIC_LEN 4
#define TZ_VERSION_2 2
// Structure to hold DST transition information
typedef struct {
time_t transition_time;
int is_dst;
int gmtoff;
char abbr[5];
} DSTTransition;
// Function to read a 32-bit integer from file with proper byte order
static uint32_t read_uint32(FILE *f, int is_big_endian) {
uint32_t value;
if (fread(&value, sizeof(value), 1, f) != 1) {
perror("Failed to read uint32");
exit(EXIT_FAILURE);
}
return is_big_endian ? be32toh(value) : le32toh(value);
}
// Function to read a 64-bit integer from file with proper byte order
static uint64_t read_uint64(FILE *f, int is_big_endian) {
uint64_t value;
if (fread(&value, sizeof(value), 1, f) != 1) {
perror("Failed to read uint64");
exit(EXIT_FAILURE);
}
return is_big_endian ? be64toh(value) : le64toh(value);
}
// Function to read a time_t value (which may be 32 or 64 bits)
static time_t read_time_t(FILE *f, int is_64bit, int is_big_endian) {
if (is_64bit) {
return (time_t)read_uint64(f, is_big_endian);
} else {
return (time_t)read_uint32(f, is_big_endian);
}
}
// Function to parse the TZif file and extract DST information
void parse_tzif_file(const char *filename) {
FILE *f = fopen(filename, "rb");
if (!f) {
perror("Failed to open timezone file");
exit(EXIT_FAILURE);
}
// Read and verify the magic header
char magic[TZ_MAGIC_LEN + 1];
if (fread(magic, TZ_MAGIC_LEN, 1, f) != 1) {
perror("Failed to read magic header");
fclose(f);
exit(EXIT_FAILURE);
}
magic[TZ_MAGIC_LEN] = '\0';
if (strcmp(magic, TZ_MAGIC) != 0) {
fprintf(stderr, "Not a valid TZif file (magic header mismatch)\n");
fclose(f);
exit(EXIT_FAILURE);
}
// Read version (we only handle version 2)
char version = fgetc(f);
if (version != TZ_VERSION_2) {
fprintf(stderr, "Unsupported TZif version: %d\n", version);
fclose(f);
exit(EXIT_FAILURE);
}
// Skip 15 bytes of padding
if (fseek(f, 15, SEEK_CUR) != 0) {
perror("Failed to seek past padding");
fclose(f);
exit(EXIT_FAILURE);
}
// Read the counts and types
uint32_t tzh_ttisgmtcnt = read_uint32(f, 1);
uint32_t tzh_ttisstdcnt = read_uint32(f, 1);
uint32_t tzh_leapcnt = read_uint32(f, 1);
uint32_t tzh_timecnt = read_uint32(f, 1);
uint32_t tzh_typecnt = read_uint32(f, 1);
uint32_t tzh_charcnt = read_uint32(f, 1);
// Determine if we're dealing with 32-bit or 64-bit time_t
struct stat st;
if (fstat(fileno(f), &st) != 0) {
perror("Failed to stat file");
fclose(f);
exit(EXIT_FAILURE);
}
int is_64bit = (st.st_size > 512); // Simple heuristic for TZif2 format
// Read transition times
time_t *transitions = malloc(tzh_timecnt * sizeof(time_t));
if (!transitions) {
perror("Failed to allocate memory for transitions");
fclose(f);
exit(EXIT_FAILURE);
}
for (uint32_t i = 0; i < tzh_timecnt; i++) {
transitions[i] = read_time_t(f, is_64bit, 1);
}
// Read transition types
uint8_t *types = malloc(tzh_timecnt * sizeof(uint8_t));
if (!types) {
perror("Failed to allocate memory for types");
free(transitions);
fclose(f);
exit(EXIT_FAILURE);
}
if (fread(types, sizeof(uint8_t), tzh_timecnt, f) != tzh_timecnt) {
perror("Failed to read transition types");
free(transitions);
free(types);
fclose(f);
exit(EXIT_FAILURE);
}
// Read type information
DSTTransition *type_info = malloc(tzh_typecnt * sizeof(DSTTransition));
if (!type_info) {
perror("Failed to allocate memory for type info");
free(transitions);
free(types);
fclose(f);
exit(EXIT_FAILURE);
}
for (uint32_t i = 0; i < tzh_typecnt; i++) {
type_info[i].gmtoff = (int)read_uint32(f, 1);
type_info[i].is_dst = fgetc(f);
type_info[i].abbr[0] = fgetc(f);
type_info[i].abbr[1] = fgetc(f);
type_info[i].abbr[2] = fgetc(f);
type_info[i].abbr[3] = fgetc(f);
type_info[i].abbr[4] = '\0';
}
// Read timezone strings (we'll skip these for simplicity)
if (fseek(f, tzh_charcnt, SEEK_CUR) != 0) {
perror("Failed to skip timezone strings");
}
// Print DST information
printf("Time Zone Information for %s:\n", filename);
printf("Number of transitions: %u\n", tzh_timecnt);
printf("Number of types: %u\n", tzh_typecnt);
printf("\nDST Transition Information:\n");
for (uint32_t i = 0; i < tzh_timecnt; i++) {
if (types[i] >= tzh_typecnt) {
fprintf(stderr, "Warning: Invalid type index %u (max %u)\n", types[i], tzh_typecnt - 1);
continue;
}
DSTTransition *t = &type_info[types[i]];
char time_str[26];
ctime_r(&transitions[i], time_str);
time_str[strlen(time_str) - 1] = '\0'; // Remove newline
printf("Transition %u: %s\n", i + 1, time_str);
printf(" GMT Offset: %d seconds\n", t->gmtoff);
printf(" Is DST: %s\n", t->is_dst ? "Yes" : "No");
printf(" Abbreviation: %s\n", t->abbr);
printf("\n");
}
// Clean up
free(transitions);
free(types);
free(type_info);
fclose(f);
}
int main(int argc, char *argv[]) {
const char *tzfile = "/etc/localtime";
if (argc > 1) {
tzfile = argv[1];
}
printf("Parsing timezone file: %s\n", tzfile);
parse_tzif_file(tzfile);
return 0;
}
How This Code Works:
-
File Structure: The code reads the IANA TZif2 file format, which typically contains:
- A magic header ("TZif")
- Version information
- Counts of various data structures
- Transition times (when DST changes occur)
- Type information (including DST flags and GMT offsets)
- Timezone abbreviations
-
Key Functions:
read_uint32 and read_uint64: Handle byte order conversion (TZif files are big-endian)
read_time_t: Reads time values which can be either 32 or 64 bits
parse_tzif_file: The main function that parses the file and extracts DST information
-
Output: The program prints:
- The number of transitions (DST changes)
- For each transition: the date/time, GMT offset, whether it's DST, and the timezone abbreviation
Compilation and Usage:
gcc -std=c99 -o tz_parser tz_parser.c
./tz_parser /etc/localtime
Notes:
- The code handles both 32-bit and 64-bit time_t values in the TZif file.
- It properly converts between host byte order and big-endian format used in TZif files.
- The program provides detailed information about each DST transition point.
- Error handling is included for file operations and memory allocation.
This implementation gives you a complete view of all DST transitions in the timezone file, which is typically more useful than just getting the current DST status.