#include #include #include #include void print_date(const char *label, const struct tm *tm); int date_equals(const struct tm *a, const struct tm *b); int main(int argc, char **argv) { time_t t_in = 0, t_ago; struct tm local, utc, local2; if (argc == 2) { t_in = (time_t) strtol(argv[1], NULL, 0); } local = *localtime(&t_in); print_date("Local", &local); utc = *gmtime(&t_in); print_date("UTC", &utc); /* subtract an interval of 34 years */ local.tm_year -= 34; /* Normalize */ local.tm_isdst = -1; t_ago = mktime(&local); if (t_ago == -1 && local.tm_isdst == -1) { int n = (70 - local.tm_year + 27) / 28; local.tm_year += n * 28; t_ago = mktime(&local); if (t_ago == -1 && local.tm_isdst == -1) { printf("Warning, time may be wrong\n"); /* or call elog() */ } local.tm_year -= n * 28; t_ago -= (365*4+1)*7*24*60*60*n; /* assumes 1901 <= year <= 2099 */ } print_date("Local", &local); local2 = *localtime(&t_ago); /* this should be redundant */ if (!date_equals(&local, &local2)) { print_date("ERROR", &local2); } utc = *gmtime(&t_ago); print_date("UTC", &utc); return EXIT_SUCCESS; } void print_date(const char *label, const struct tm *tm) { char buffer[80]; strftime(buffer, sizeof buffer, "%a %b %d %H:%M:%S %Y %Z %z", tm); printf("%5s %s\n", label, buffer); printf(" %4d-%02d-%02dT%02d:%02d:%02d, wday=%d, yday=%d, isdst = %d\n", 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday, tm->tm_yday, tm->tm_isdst); } /* return 1 if all fields of a are equal to those of b, otherwise 0 */ int date_equals(const struct tm *a, const struct tm *b) { return a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour && a->tm_min == b->tm_min && a->tm_sec == b->tm_sec && a->tm_wday == b->tm_wday && a->tm_yday == b->tm_yday && a->tm_isdst == b->tm_isdst; }