#include #include #include #include #define BUFSIZE 256 void settz(const char* tz) { static char *tzvalbuf; static size_t tzvalbufsize; if( tzvalbufsize <= 3 + strlen(tz)) { tzvalbuf = malloc(tzvalbufsize = BUFSIZE); snprintf( tzvalbuf, BUFSIZE, "TZ=%s", tz ); putenv( tzvalbuf ); puts( "Updated environment" ); } else { puts( "Didn't update environment" ); snprintf( tzvalbuf, BUFSIZE, "TZ=%s", tz ); } tzset(); } int main() { struct tm t = { .tm_sec = 44, .tm_min = 42, .tm_hour = 6, .tm_mday = 15, .tm_mon = 2 - 1, .tm_year = 2015 - 1900, .tm_isdst = -1, }; time_t val; puts( "Staying at localtime zone" ); val = mktime( &t ); printf( "Time (in seconds): %ld\n", val ); settz( "TZ=XXX-0:00:00" ); puts( "Switching to UTC" ); val = mktime( &t ); printf( "Time (in seconds): %ld\n", val ); settz( "TZ=XXX-1:00:00" ); puts( "Switching to UTC+1" ); val = mktime( &t ); printf( "Time (in seconds): %ld\n", val ); settz( "TZ=XXX-3:00:00" ); puts( "Switching to UTC+3" ); val = mktime( &t ); printf( "Time (in seconds): %ld\n", val ); return 0; }