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