Below is a self-contained C99 program that
- opens /etc/localtime (which on most glibc-based systems is a copy of, or a symlink to, a binary IANA Time-Zone Information Format v2 file – TZif2),
- parses the TZif2 header and data blocks,
- walks the leap-second-corrected transition times,
- prints every transition that changes the DST bit (tt_isdst),
- and shows the UTC offset and the wall-time offset that applies right after the transition.
Compile with
gcc -std=c99 -Wall -O2 tzif2-dst.c -o tzif2-dst
Run as
./tzif2-dst
The code is deliberately minimal and uses only the C99 standard library plus POSIX open/read/close. It does not rely on <time.h> or the zoneinfo parser that libc provides; instead it interprets the raw TZif2 file itself.
/*
* tzif2-dst.c – extract DST transition times from /etc/localtime (TZif2 format)
* C99, POSIX
*
* References:
* https://datatracker.ietf.org/doc/html/rfc8536
*/
#define _POSIX_C_SOURCE 200809L
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
/* ---------- endian-independent 32/64-bit readers ------------------------ */
static uint32_t read_be32(const unsigned char *p)
{
return ((uint32_t)p[0] << 24) |
((uint32_t)p[1] << 16) |
((uint32_t)p[2] << 8) |
((uint32_t)p[3] << 0);
}
static uint64_t read_be64(const unsigned char *p)
{
return ((uint64_t)read_be32(p) << 32) | read_be32(p + 4);
}
/* ---------- TZif structures --------------------------------------------- */
#define TZ_MAGIC "TZif"
struct tzhead {
char magic[4]; /* "TZif" */
char version[1]; /* '2' for TZif2 */
char reserved[15];
uint32_t isutccnt; /* UT/local indicators count */
uint32_t isstdcnt; /* standard/wall indicators */
uint32_t leapcnt; /* leap-second records */
uint32_t timecnt; /* transition times */
uint32_t typecnt; /* local time type records */
uint32_t charcnt; /* zone abbreviation chars */
};
/* ---------- main --------------------------------------------------------- */
int main(void)
{
const char *path = "/etc/localtime";
int fd = open(path, O_RDONLY);
if (fd < 0) {
perror(path);
return EXIT_FAILURE;
}
/* read header -------------------------------------------------------- */
struct tzhead hdr;
if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
memcmp(hdr.magic, TZ_MAGIC, 4) != 0) {
fprintf(stderr, "%s: not a TZif file\n", path);
close(fd);
return EXIT_FAILURE;
}
if (hdr.version[0] != '2' && hdr.version[0] != '3') {
fprintf(stderr, "%s: unsupported TZif version '%c'\n",
path, hdr.version[0]);
close(fd);
return EXIT_FAILURE;
}
/* skip the first (32-bit) data block; TZif2/3 have a second 64-bit one */
size_t skip = 4 * hdr.timecnt /* transition times 32-bit */
+ 1 * hdr.timecnt /* type indices */
+ 6 * hdr.typecnt /* ttinfo entries (6 bytes each) */
+ 1 * hdr.charcnt /* abbrev chars */
+ 8 * hdr.leapcnt /* leap-second records */
+ 1 * hdr.isstdcnt
+ 1 * hdr.isutccnt;
if (lseek(fd, skip, SEEK_CUR) == (off_t)-1) {
perror("lseek");
close(fd);
return EXIT_FAILURE;
}
/* read 64-bit second header (identical counts) ----------------------- */
struct tzhead hdr64;
if (read(fd, &hdr64, sizeof hdr64) != sizeof hdr64) {
perror("read 64-bit header");
close(fd);
return EXIT_FAILURE;
}
uint32_t timecnt = read_be32((unsigned char *)&hdr64.timecnt);
uint32_t typecnt = read_be32((unsigned char *)&hdr64.typecnt);
/* allocate and read 64-bit transition times -------------------------- */
uint64_t *transition = malloc(timecnt * sizeof *transition);
unsigned char *typeidx = malloc(timecnt);
unsigned char *ttinfo = malloc(typecnt * 6); /* 6 bytes per record */
if (!transition || !typeidx || !ttinfo) {
perror("malloc");
close(fd);
return EXIT_FAILURE;
}
/* read data blocks */
if (read(fd, transition, 8 * timecnt) != 8 * (ssize_t)timecnt ||
read(fd, typeidx, 1 * timecnt) != 1 * (ssize_t)timecnt ||
read(fd, ttinfo, 6 * typecnt) != 6 * (ssize_t)typecnt) {
perror("read data");
close(fd);
return EXIT_FAILURE;
}
/* convert from big-endian */
for (uint32_t i = 0; i < timecnt; ++i)
transition[i] = read_be64((unsigned char *)&transition[i]);
/* print DST transitions ---------------------------------------------- */
puts("DST transitions in /etc/localtime (TZif2/3 64-bit block)");
puts("UTC-time offset isdst");
for (uint32_t i = 0; i < timecnt; ++i) {
uint8_t idx = typeidx[i];
if (idx >= typecnt) {
fprintf(stderr, "invalid type index %u\n", idx);
continue;
}
int32_t offset = (int32_t)read_be32(ttinfo + idx * 6);
uint8_t isdst = ttinfo[idx * 6 + 4];
/* show only transitions that flip the DST flag */
if (i == 0 || isdst != (ttinfo[typeidx[i-1] * 6 + 4])) {
time_t t = (time_t)transition[i];
char buf[32];
struct tm tm;
gmtime_r(&t, &tm);
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", &tm);
printf("%s %+ld %d\n", buf, (long)offset, isdst);
}
}
free(transition);
free(typeidx);
free(ttinfo);
close(fd);
return 0;
}
What the program does
- Reads the TZif2 header twice (the second copy contains 64-bit transition times).
- Walks the transition array and prints every UTC instant at which the DST flag changes.
- Prints the UTC offset (in seconds east of UTC) and the new
isdst value (0 or 1).