Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- char* printtm(struct tm tm)
- {
- static char buf[100];
- sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d (off=%ld, dst=%d)",
- tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
- tm.tm_hour, tm.tm_min, tm.tm_sec,
- tm.tm_gmtoff, tm.tm_isdst);
- return buf;
- }
- void test(int y, int m, int d, int hh, int mm, int ss, int isdst)
- {
- // Prepare tm structs
- struct tm tm, tm2;
- memset(&tm, 0, sizeof(tm));
- memset(&tm2, 0, sizeof(tm));
- tm.tm_year = y - 1900;
- tm.tm_mon = m - 1;
- tm.tm_mday = d;
- tm.tm_hour = hh;
- tm.tm_min = mm;
- tm.tm_sec = ss;
- tm.tm_isdst = isdst;
- // Convert tm -> t -> tm and print
- printf("%s -> ", printtm(tm));
- time_t t = mktime(&tm);
- printf("%s -> ", printtm(tm));
- printf("%12ld -> ", t);
- localtime_r(&t, &tm2);
- printf("%s\n", printtm(tm));
- }
- int main()
- {
- setenv("TZ", ":America/Los_Angeles", 1);
- tzset();
- test(2013,07,01, 12,0,0, 1);
- test(2013,01,01, 12,0,0, 1);
- test(1927,01,01, 12,0,0, 1);
- test(1929,01,01, 12,0,0, 1);
- test(1932,01,01, 12,0,0, 1);
- test(1934,01,01, 12,0,0, 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement