LCA2011 version
[tridge/junkcode.git] / timegm.c
1 #include <sys/time.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <stdio.h>
6
7  time_t timegm(struct tm *tm) 
8 {
9         time_t t = mktime(tm);
10         t -= mktime(gmtime(&t)) - (int)mktime(localtime(&t));
11         return t;
12 }
13
14 int get_time_zone(time_t t)
15 {
16         struct tm *tm = gmtime(&t);
17         struct tm tm_utc;
18         if (!tm)
19                 return 0;
20         tm_utc = *tm;
21         tm = localtime(&t);
22         if (!tm)
23                 return 0;
24         return mktime(&tm_utc) - mktime(tm);
25 }
26
27 int main(void)
28 {
29         time_t t = time(NULL);
30         struct tm *tm_local = localtime(&t);
31         struct tm *tm_gmt   = gmtime(&t);
32         int diff;
33
34         diff = timegm(tm_local) - (int)mktime(tm_local);
35
36         printf("gmt1=%d\n", t);
37         printf("loc1=%d\n", t + 36000);
38
39         printf("loc2=%d\n", mktime(tm_local));
40         printf("gmt2=%d\n", timegm(tm_local));
41
42         printf("diff=%d\n", diff);
43
44         printf("tz=%d\n", get_time_zone(t));
45         return 0;
46 }