test structure too
[tridge/junkcode.git] / tprog.c
1 #include <time.h>
2 #include <stdio.h>
3
4 #define TM_YEAR_BASE 1900
5
6 /*******************************************************************
7 yield the difference between *A and *B, in seconds, ignoring leap seconds
8 ********************************************************************/
9 static int tm_diff(struct tm *a, struct tm *b)
10 {
11         int ay = a->tm_year + (TM_YEAR_BASE - 1);
12         int by = b->tm_year + (TM_YEAR_BASE - 1);
13         int intervening_leap_days =
14                 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
15         int years = ay - by;
16         int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
17         int hours = 24*days + (a->tm_hour - b->tm_hour);
18         int minutes = 60*hours + (a->tm_min - b->tm_min);
19         int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
20
21         return seconds;
22 }
23
24 /*******************************************************************
25   return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
26   ******************************************************************/
27 static int TimeZone(time_t t)
28 {
29         struct tm *tm = gmtime(&t);
30         struct tm tm_utc;
31         if (!tm)
32                 return 0;
33         tm_utc = *tm;
34         tm = localtime(&t);
35         if (!tm)
36                 return 0;
37         return tm_diff(&tm_utc,tm);
38 }
39
40 time_t timegm2(struct tm *tm) 
41 {
42         struct tm tm2, tm3;
43         time_t t;
44
45         tm2 = *tm;
46
47         t = mktime(&tm2);
48         tm3 = *localtime(&t);
49         tm2 = *tm;
50         tm2.tm_isdst = tm3.tm_isdst;
51         t = mktime(&tm2);
52         t -= TimeZone(t);
53
54         return t;
55 }
56
57 main()
58 {
59         struct tm tm;
60         time_t tnow, t;
61
62         tnow = time(NULL);
63
64         for (t=tnow; t < tnow + (400 * 24 * 60 * 60); t += 600) {
65                 tm = *localtime(&t);
66
67                 if (timegm2(&tm) != timegm(&tm)) {
68                         printf("diff=%d at %s", 
69                                timegm2(&tm) - timegm(&tm), 
70                                asctime(&tm));
71                 }
72         }
73         return 0;
74 }