Blame


1 64bcfff3 2003-11-25 devnull #define NOPLAN9DEFINES
2 5db07ba9 2006-03-03 devnull #include <u.h>
3 fd04aace 2003-11-23 devnull #include <libc.h>
4 5db07ba9 2006-03-03 devnull #include <stdlib.h> /* setenv etc. */
5 64bcfff3 2003-11-25 devnull #include <time.h>
6 fd04aace 2003-11-23 devnull
7 5db07ba9 2006-03-03 devnull static int didtz;
8 5db07ba9 2006-03-03 devnull static int tzdelta;
9 33433b3f 2007-04-17 devnull static char tzone[32];
10 fd04aace 2003-11-23 devnull
11 5db07ba9 2006-03-03 devnull static void
12 5db07ba9 2006-03-03 devnull dotz(void)
13 5db07ba9 2006-03-03 devnull {
14 5db07ba9 2006-03-03 devnull time_t t;
15 7f420fb3 2006-05-20 devnull struct tm *gtm;
16 7f420fb3 2006-05-20 devnull struct tm tm;
17 fd04aace 2003-11-23 devnull
18 5db07ba9 2006-03-03 devnull if(didtz)
19 5db07ba9 2006-03-03 devnull return;
20 6bd2526b 2006-05-30 devnull didtz = 1;
21 5db07ba9 2006-03-03 devnull t = time(0);
22 5db07ba9 2006-03-03 devnull strftime(tzone, sizeof tzone, "%Z", localtime(&t));
23 7f420fb3 2006-05-20 devnull tm = *localtime(&t); /* set local time zone field */
24 7f420fb3 2006-05-20 devnull gtm = gmtime(&t);
25 7f420fb3 2006-05-20 devnull tm.tm_sec = gtm->tm_sec;
26 7f420fb3 2006-05-20 devnull tm.tm_min = gtm->tm_min;
27 7f420fb3 2006-05-20 devnull tm.tm_hour = gtm->tm_hour;
28 7f420fb3 2006-05-20 devnull tm.tm_mday = gtm->tm_mday;
29 7f420fb3 2006-05-20 devnull tm.tm_mon = gtm->tm_mon;
30 7f420fb3 2006-05-20 devnull tm.tm_year = gtm->tm_year;
31 7f420fb3 2006-05-20 devnull tm.tm_wday = gtm->tm_wday;
32 7f420fb3 2006-05-20 devnull tzdelta = t - mktime(&tm);
33 5db07ba9 2006-03-03 devnull }
34 64bcfff3 2003-11-25 devnull
35 fd04aace 2003-11-23 devnull static void
36 5db07ba9 2006-03-03 devnull tm2Tm(struct tm *tm, Tm *bigtm, int gmt)
37 fd04aace 2003-11-23 devnull {
38 fd04aace 2003-11-23 devnull memset(bigtm, 0, sizeof *bigtm);
39 fd04aace 2003-11-23 devnull bigtm->sec = tm->tm_sec;
40 fd04aace 2003-11-23 devnull bigtm->min = tm->tm_min;
41 fd04aace 2003-11-23 devnull bigtm->hour = tm->tm_hour;
42 fd04aace 2003-11-23 devnull bigtm->mday = tm->tm_mday;
43 fd04aace 2003-11-23 devnull bigtm->mon = tm->tm_mon;
44 fd04aace 2003-11-23 devnull bigtm->year = tm->tm_year;
45 fd04aace 2003-11-23 devnull bigtm->wday = tm->tm_wday;
46 5db07ba9 2006-03-03 devnull if(gmt){
47 5db07ba9 2006-03-03 devnull strcpy(bigtm->zone, "GMT");
48 5db07ba9 2006-03-03 devnull bigtm->tzoff = 0;
49 5db07ba9 2006-03-03 devnull }else{
50 5db07ba9 2006-03-03 devnull dotz();
51 33433b3f 2007-04-17 devnull strncpy(bigtm->zone, tzone, 3);
52 33433b3f 2007-04-17 devnull bigtm->zone[3] = 0;
53 5db07ba9 2006-03-03 devnull bigtm->tzoff = tzdelta;
54 8ad51794 2004-03-25 devnull }
55 fd04aace 2003-11-23 devnull }
56 fd04aace 2003-11-23 devnull
57 fd04aace 2003-11-23 devnull static void
58 fd04aace 2003-11-23 devnull Tm2tm(Tm *bigtm, struct tm *tm)
59 fd04aace 2003-11-23 devnull {
60 6452f95b 2006-05-18 devnull /* initialize with current time to get local time zone! (tm_isdst) */
61 6452f95b 2006-05-18 devnull time_t t;
62 6452f95b 2006-05-18 devnull time(&t);
63 6452f95b 2006-05-18 devnull *tm = *localtime(&t);
64 6452f95b 2006-05-18 devnull
65 fd04aace 2003-11-23 devnull tm->tm_sec = bigtm->sec;
66 fd04aace 2003-11-23 devnull tm->tm_min = bigtm->min;
67 fd04aace 2003-11-23 devnull tm->tm_hour = bigtm->hour;
68 fd04aace 2003-11-23 devnull tm->tm_mday = bigtm->mday;
69 fd04aace 2003-11-23 devnull tm->tm_mon = bigtm->mon;
70 fd04aace 2003-11-23 devnull tm->tm_year = bigtm->year;
71 fd04aace 2003-11-23 devnull tm->tm_wday = bigtm->wday;
72 fd04aace 2003-11-23 devnull }
73 fd04aace 2003-11-23 devnull
74 fd04aace 2003-11-23 devnull Tm*
75 829e8223 2004-12-29 devnull p9gmtime(long x)
76 fd04aace 2003-11-23 devnull {
77 829e8223 2004-12-29 devnull time_t t;
78 fd04aace 2003-11-23 devnull struct tm tm;
79 5db07ba9 2006-03-03 devnull static Tm bigtm;
80 5db07ba9 2006-03-03 devnull
81 829e8223 2004-12-29 devnull t = (time_t)x;
82 fd04aace 2003-11-23 devnull tm = *gmtime(&t);
83 5db07ba9 2006-03-03 devnull tm2Tm(&tm, &bigtm, 1);
84 fd04aace 2003-11-23 devnull return &bigtm;
85 fd04aace 2003-11-23 devnull }
86 fd04aace 2003-11-23 devnull
87 fd04aace 2003-11-23 devnull Tm*
88 829e8223 2004-12-29 devnull p9localtime(long x)
89 fd04aace 2003-11-23 devnull {
90 829e8223 2004-12-29 devnull time_t t;
91 fd04aace 2003-11-23 devnull struct tm tm;
92 5db07ba9 2006-03-03 devnull static Tm bigtm;
93 fd04aace 2003-11-23 devnull
94 829e8223 2004-12-29 devnull t = (time_t)x;
95 fd04aace 2003-11-23 devnull tm = *localtime(&t);
96 5db07ba9 2006-03-03 devnull tm2Tm(&tm, &bigtm, 0);
97 fd04aace 2003-11-23 devnull return &bigtm;
98 fd04aace 2003-11-23 devnull }
99 fd04aace 2003-11-23 devnull
100 fd04aace 2003-11-23 devnull long
101 fd04aace 2003-11-23 devnull p9tm2sec(Tm *bigtm)
102 fd04aace 2003-11-23 devnull {
103 5db07ba9 2006-03-03 devnull time_t t;
104 fd04aace 2003-11-23 devnull struct tm tm;
105 fd04aace 2003-11-23 devnull
106 fd04aace 2003-11-23 devnull Tm2tm(bigtm, &tm);
107 5db07ba9 2006-03-03 devnull t = mktime(&tm);
108 5db07ba9 2006-03-03 devnull if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0){
109 5db07ba9 2006-03-03 devnull dotz();
110 5db07ba9 2006-03-03 devnull t += tzdelta;
111 5db07ba9 2006-03-03 devnull }
112 5db07ba9 2006-03-03 devnull return t;
113 fd04aace 2003-11-23 devnull }
114 fd04aace 2003-11-23 devnull