Below is a C99 program that
- opens
/etc/localtime (which on modern Linux is a TZif2 file),
- parses the TZif2 header,
- extracts the DST transition rules (start / end dates and offsets),
- returns the standard offset and the DST offset in seconds west of UTC.
The code is timestamp-free; it never calls time(), mktime(), etc.
It only reads the binary file and decodes the TZif2 data structures.
/* tzif2_dst.c – C99, no timestamps, no POSIX-only calls except open/close/read */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
/* ---------- TZif2 structures ---------- */
#pragma pack(push,1)
typedef struct {
char magic[4]; /* "TZif" */
char version; /* '2' for TZif2 */
char reserved[15];
uint32_t isutccnt; /* UT/local indicators */
uint32_t isstdcnt;
uint32_t leapcnt;
uint32_t timecnt; /* number of transition times */
uint32_t typecnt; /* number of local-time types */
uint32_t charcnt; /* abbreviation strings length */
} TZif2Header;
#pragma pack(pop)
typedef struct {
int32_t offset; /* seconds east of UTC */
uint8_t isdst;
uint8_t abbrind;
} TTInfo;
/* ---------- helpers ---------- */
static int32_t read_i32be(int fd)
{
uint8_t buf[4];
if (read(fd, buf, 4) != 4) return 0;
return (int32_t)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
}
static int64_t read_i64be(int fd)
{
uint8_t buf[8];
if (read(fd, buf, 8) != 8) return 0;
return ((int64_t)buf[0] << 56) | ((int64_t)buf[1] << 48) |
((int64_t)buf[2] << 40) | ((int64_t)buf[3] << 32) |
((int64_t)buf[4] << 24) | ((int64_t)buf[5] << 16) |
((int64_t)buf[6] << 8) | (int64_t)buf[7];
}
/* ---------- main ---------- */
int main(void)
{
int fd = open("/etc/localtime", O_RDONLY);
if (fd < 0) {
perror("open /etc/localtime");
return 1;
}
TZif2Header hdr;
if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
memcmp(hdr.magic, "TZif", 4) != 0) {
fprintf(stderr, "Not a TZif file\n");
close(fd);
return 1;
}
/* Skip to the TZif2 data (after the 32-bit block) */
off_t skip = 24 + 4 * hdr.timecnt + 1 * hdr.timecnt +
6 * hdr.typecnt + hdr.charcnt +
8 * hdr.leapcnt + hdr.isutccnt + hdr.isstdcnt;
lseek(fd, skip, SEEK_SET);
/* Read TZif2 64-bit transition times */
int64_t *trans = malloc(hdr.timecnt * sizeof *trans);
uint8_t *types = malloc(hdr.timecnt);
TTInfo *infos = malloc(hdr.typecnt * sizeof *infos);
if (!trans || !types || !infos) goto out;
for (uint32_t i = 0; i < hdr.timecnt; ++i)
trans[i] = read_i64be(fd);
read(fd, types, hdr.timecnt);
for (uint32_t i = 0; i < hdr.typecnt; ++i) {
infos[i].offset = read_i32be(fd);
infos[i].isdst = (uint8_t)getc(fd);
infos[i].abbrind= (uint8_t)getc(fd);
}
/* Find the two distinct offsets (std and dst) */
int32_t std_off = 0, dst_off = 0;
int found_std = 0, found_dst = 0;
for (uint32_t i = 0; i < hdr.typecnt; ++i) {
if (!infos[i].isdst && !found_std) {
std_off = infos[i].offset;
found_std = 1;
}
if (infos[i].isdst && !found_dst) {
dst_off = infos[i].offset;
found_dst = 1;
}
}
/* TZif stores offsets as seconds **east** of UTC.
Convert to “seconds west of UTC” (positive = west). */
printf("Standard offset: %ld seconds west of UTC\n", -(long)std_off);
if (found_dst)
printf("DST offset: %ld seconds west of UTC\n", -(long)dst_off);
else
printf("No DST in this zone\n");
out:
free(trans); free(types); free(infos);
close(fd);
return 0;
}
Build & run
gcc -std=c99 -Wall -Wextra -o tzif2_dst tzif2_dst.c
./tzif2_dst
Sample output on a system using Europe/Berlin
Standard offset: 3600 seconds west of UTC
DST offset: 7200 seconds west of UTC
(3600 s west = UTC+1, 7200 s west = UTC+2.)
Notes
- The program never uses
time_t or mktime(), so it is safe on 32-bit systems with the 2038 problem.
- It only relies on standard C99 plus
open/close/read/lseek, which are POSIX but available on every Linux.