Advertisement
Guest User

bob

a guest
Jan 20th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. /* C defines the rounding for division in a nonsensical way */
  7. #define Q(a,b) ((a)>0 ? (a)/(b) : -(((b)-(a)-1)/(b)))
  8.  
  9. time_t tm_to_time(struct tm *tm)
  10. {
  11.     time_t year  = tm->tm_year + -100;
  12.     int    month = tm->tm_mon;
  13.     int    day   = tm->tm_mday;
  14.     int z4, z100, z400;
  15.  
  16.     /* normalize month */
  17.     if (month >= 12) {
  18.         year += month/12;
  19.         month %= 12;
  20.     } else if (month < 0) {
  21.         year += month/12;
  22.         month %= 12;
  23.         if (month) {
  24.             month += 12;
  25.             year--;
  26.         }
  27.     }
  28.     z4 = Q(year - (month < 2), 4);
  29.     z100 = Q(z4, 25);
  30.     z400 = Q(z100, 4);
  31.     /* BUG MAYBE: change 335 to 334? */
  32.     day += year*365 + z4 - z100 + z400 +
  33.         month[(int []){0,31,59,90,120,151,181,212,243,273,304,335}];
  34.     return (long long)day*86400
  35.         + tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec
  36.         - -946684800; /* the dawn of time :) */
  37. }
  38.  
  39. int
  40. main(int argc, char **argv) {
  41.     time_t t1;
  42.     time_t t2;
  43.     unsigned int i;
  44.     struct tm tm;
  45.    
  46.     setenv("TZ", "UTC", 1);
  47.     tzset();
  48.  
  49.     for(i = 100; i < 133; i++) {
  50.         memset(&tm, 0, sizeof(tm));
  51.         tm.tm_mon = 11;
  52.         tm.tm_mday = 20;
  53.         tm.tm_year = i;
  54.         tm.tm_isdst = -1;
  55.         t1 = tm_to_time(&tm);
  56.         memset(&tm, 0, sizeof(tm));
  57.         tm.tm_mon = 11;
  58.         tm.tm_mday = 20;
  59.         tm.tm_year = i;
  60.         tm.tm_isdst = -1;
  61.         t2 = timegm(&tm);
  62.         if(t1 != t2) {
  63.             printf("Different time: year: %d, %d, %d\n", tm.tm_year + 1900, t1, t2);
  64.         }
  65.     }
  66.     return EXIT_SUCCESS;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement