Advertisement
Guest User

mktime bug?

a guest
Dec 5th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. char* printtm(struct tm tm)
  7. {
  8.   static char buf[100];
  9.   sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d (off=%ld, dst=%d)",
  10.     tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  11.     tm.tm_hour, tm.tm_min, tm.tm_sec,
  12.     tm.tm_gmtoff, tm.tm_isdst);
  13.   return buf;
  14. }
  15.  
  16. void test(int y, int m, int d, int hh, int mm, int ss, int isdst)
  17. {
  18.   // Prepare tm structs
  19.   struct tm tm, tm2;
  20.   memset(&tm, 0, sizeof(tm));
  21.   memset(&tm2, 0, sizeof(tm));
  22.   tm.tm_year = y - 1900;
  23.   tm.tm_mon = m - 1;
  24.   tm.tm_mday = d;
  25.   tm.tm_hour = hh;
  26.   tm.tm_min = mm;
  27.   tm.tm_sec = ss;
  28.   tm.tm_isdst = isdst;
  29.   // Convert tm -> t -> tm and print
  30.   printf("%s -> ", printtm(tm));
  31.   time_t t = mktime(&tm);
  32.   printf("%s -> ", printtm(tm));
  33.   printf("%12ld -> ", t);
  34.   localtime_r(&t, &tm2);
  35.   printf("%s\n", printtm(tm));
  36. }
  37.  
  38. int main()
  39. {
  40.   setenv("TZ", ":America/Los_Angeles", 1);
  41.   tzset();
  42.  
  43.   test(2013,07,01, 12,0,0, 1);
  44.   test(2013,01,01, 12,0,0, 1);
  45.   test(1927,01,01, 12,0,0, 1);
  46.   test(1929,01,01, 12,0,0, 1);
  47.   test(1932,01,01, 12,0,0, 1);
  48.   test(1934,01,01, 12,0,0, 1);
  49.   return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement