Advertisement
Guest User

bob

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