Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- /* C defines the rounding for division in a nonsensical way */
- #define Q(a,b) ((a)>0 ? (a)/(b) : -(((b)-(a)-1)/(b)))
- time_t tm_to_time(struct tm *tm)
- {
- time_t year = tm->tm_year + -100;
- int month = tm->tm_mon;
- int day = tm->tm_mday;
- int z4, z100, z400;
- /* normalize month */
- if (month >= 12) {
- year += month/12;
- month %= 12;
- } else if (month < 0) {
- year += month/12;
- month %= 12;
- if (month) {
- month += 12;
- year--;
- }
- }
- z4 = Q(year - (month < 2), 4);
- z100 = Q(z4, 25);
- z400 = Q(z100, 4);
- /* BUG MAYBE: change 335 to 334? */
- day += year*365 + z4 - z100 + z400 +
- month[(int []){0,31,59,90,120,151,181,212,243,273,304,335}];
- return (long long)day*86400
- + tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec
- - -946684800; /* the dawn of time :) */
- }
- int
- main(int argc, char **argv) {
- time_t t1;
- time_t t2;
- unsigned int i;
- struct tm tm;
- setenv("TZ", "UTC", 1);
- tzset();
- for(i = 100; i < 133; i++) {
- memset(&tm, 0, sizeof(tm));
- tm.tm_mon = 11;
- tm.tm_mday = 20;
- tm.tm_year = i;
- tm.tm_isdst = -1;
- t1 = tm_to_time(&tm);
- memset(&tm, 0, sizeof(tm));
- tm.tm_mon = 11;
- tm.tm_mday = 20;
- tm.tm_year = i;
- tm.tm_isdst = -1;
- t2 = timegm(&tm);
- if(t1 != t2) {
- printf("Different time: year: %d, %d, %d\n", tm.tm_year + 1900, t1, t2);
- }
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement