multi-line grep
[tridge/junkcode.git] / tz.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <time.h>
4
5 #define TM_YEAR_BASE 1900
6
7 /*******************************************************************
8 yield the difference between *A and *B, in seconds, ignoring leap seconds
9 ********************************************************************/
10 static int tm_diff(struct tm *a, struct tm *b)
11 {
12   int ay = a->tm_year + (TM_YEAR_BASE - 1);
13   int by = b->tm_year + (TM_YEAR_BASE - 1);
14   int intervening_leap_days =
15     (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
16   int years = ay - by;
17   int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
18   int hours = 24*days + (a->tm_hour - b->tm_hour);
19   int minutes = 60*hours + (a->tm_min - b->tm_min);
20   int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
21
22   printf("tm_diff ay=%d by=%d ild=%d y=%d d=%d h=%d m=%d s=%d am=%d bm=%d asec=%d bsec=%d\n",
23            ay, by, intervening_leap_days, years, days, hours, minutes, seconds,
24            a->tm_min, b->tm_min,
25            a->tm_sec, b->tm_sec
26            );
27
28   if (a->tm_sec != b->tm_sec)
29           printf("Your timezone file is broken\n");
30   return seconds;
31 }
32
33 /*******************************************************************
34   return the UTC offset in seconds west of UTC
35   ******************************************************************/
36 static int TimeZone(time_t t)
37 {
38   struct tm tm_utc = *(gmtime(&t));
39   return tm_diff(&tm_utc,localtime(&t));
40 }
41
42 int main(int argc, char *argv[])
43 {
44         TimeZone(time(NULL));
45         return 0;
46 }