/* A recherché example o Modifying a field in struct tm after calling localtime() o Changing TZ before calling mktime(). Both yield the result I expect. Would these be facilitated or perhaps hindered by tm_gmtoff and tm_zone? */ #include #include #include int main(void) { char buf[256]; time_t t1, t2, t3; struct tm result; /* Use localtime_r() to load struct tm. */ #define Z1 "America/Los_Angeles" putenv( "TZ="Z1 ); time(&t1); localtime_r( &t1, &result ); strftime(buf, sizeof(buf), "%Y %s", &result ); printf( "%s %12ld\n", buf, mktime( &result ) ); /* The Grtegorian calendar cycle is 499 years, so add 400 years; take the difference in seconds and divide by seconds/day then by 400 to get average days in year. */ result.tm_year += 400; strftime(buf, sizeof(buf), "%Y %s", &result ); printf( "%s %12ld\n", buf, mktime( &result ) ); t2 = mktime( &result ); printf( "Average days in year =%10.5f\n", ( t2 - t1 ) / 86400 /400.0 ); /* time difference in seconds between two timezones; convert to hours. */ #define Z3 "America/New_York" putenv( "TZ="Z3 ); t3 = mktime( &result ); printf( "\n%s is ahead of %s by %5.2f hours.\n", Z3, Z1, ( t2 - t3 ) /3600.0 ); }