#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
 * Get International Atomic Time (TAI) in seconds.
 */
time_t time_tai(void) {
    char *orig_tzdir, *orig_tz;
    time_t t;

    orig_tzdir = getenv("TZDIR");
    orig_tz = getenv("TZ");
    {
        struct tm *tm;
        
        setenv("TZDIR", "/usr/share/zoneinfo/posix", 1);
        setenv("TZ", "", 1);
        tzset();
        setenv("TZ", "UTC", 1);
        tzset();

        t = time(NULL);
        tm = gmtime(&t);
        
        setenv("TZDIR", "/usr/share/zoneinfo/right", 1);
        setenv("TZ", "", 1);
        tzset();
        setenv("TZ", "UTC", 1);
        tzset();

        t = mktime(tm);
        
    }
    setenv("TZ", "", 1);
    tzset();
    if (orig_tzdir) {
        setenv("TZDIR", orig_tzdir, 1);
    } else {
        unsetenv("TZDIR");
    }
    if (orig_tz) {
        setenv("TZ", orig_tz, 1);
    } else {
        unsetenv("TZ");
    }
    tzset();
    return t + 10;
}

int main(int argc, char *argv[]) {
    printf("%lu\n", time_tai());
    return 0;
}
