Blame


1 fd04aace 2003-11-23 devnull #include <u.h>
2 8ad51794 2004-03-25 devnull #include <stdlib.h> /* setenv etc. */
3 64bcfff3 2003-11-25 devnull #define NOPLAN9DEFINES
4 fd04aace 2003-11-23 devnull #include <libc.h>
5 64bcfff3 2003-11-25 devnull #include <time.h>
6 fd04aace 2003-11-23 devnull
7 64bcfff3 2003-11-25 devnull #define _HAVETIMEGM 1
8 64bcfff3 2003-11-25 devnull #define _HAVETMZONE 1
9 9cfbc441 2006-02-03 devnull #define _HAVETMGMTOFF 1
10 fd04aace 2003-11-23 devnull
11 64bcfff3 2003-11-25 devnull #if defined(__linux__)
12 64bcfff3 2003-11-25 devnull # undef _HAVETMZONE
13 fd04aace 2003-11-23 devnull
14 64bcfff3 2003-11-25 devnull #elif defined(__sun__)
15 64bcfff3 2003-11-25 devnull # undef _HAVETIMEGM
16 64bcfff3 2003-11-25 devnull # undef _HAVETMZONE
17 9cfbc441 2006-02-03 devnull # undef _HAVETMGMTOFF
18 64bcfff3 2003-11-25 devnull
19 64bcfff3 2003-11-25 devnull #endif
20 64bcfff3 2003-11-25 devnull
21 fd04aace 2003-11-23 devnull static Tm bigtm;
22 fd04aace 2003-11-23 devnull
23 fd04aace 2003-11-23 devnull static void
24 fd04aace 2003-11-23 devnull tm2Tm(struct tm *tm, Tm *bigtm)
25 fd04aace 2003-11-23 devnull {
26 8ad51794 2004-03-25 devnull char *s;
27 8ad51794 2004-03-25 devnull
28 fd04aace 2003-11-23 devnull memset(bigtm, 0, sizeof *bigtm);
29 fd04aace 2003-11-23 devnull bigtm->sec = tm->tm_sec;
30 fd04aace 2003-11-23 devnull bigtm->min = tm->tm_min;
31 fd04aace 2003-11-23 devnull bigtm->hour = tm->tm_hour;
32 fd04aace 2003-11-23 devnull bigtm->mday = tm->tm_mday;
33 fd04aace 2003-11-23 devnull bigtm->mon = tm->tm_mon;
34 fd04aace 2003-11-23 devnull bigtm->year = tm->tm_year;
35 fd04aace 2003-11-23 devnull bigtm->wday = tm->tm_wday;
36 dddc2ed1 2004-04-20 devnull strftime(bigtm->zone, sizeof bigtm->zone, "%Z", tm);
37 9cfbc441 2006-02-03 devnull #ifdef _HAVETMGMTOFF
38 fd04aace 2003-11-23 devnull bigtm->tzoff = tm->tm_gmtoff;
39 1c253ceb 2003-11-23 devnull #endif
40 dddc2ed1 2004-04-20 devnull
41 8ad51794 2004-03-25 devnull if(bigtm->zone[0] == 0){
42 8ad51794 2004-03-25 devnull s = getenv("TIMEZONE");
43 8ad51794 2004-03-25 devnull if(s){
44 be22ae2d 2004-03-26 devnull strecpy(bigtm->zone, bigtm->zone+4, s);
45 8ad51794 2004-03-25 devnull free(s);
46 8ad51794 2004-03-25 devnull }
47 8ad51794 2004-03-25 devnull }
48 fd04aace 2003-11-23 devnull }
49 fd04aace 2003-11-23 devnull
50 fd04aace 2003-11-23 devnull static void
51 fd04aace 2003-11-23 devnull Tm2tm(Tm *bigtm, struct tm *tm)
52 fd04aace 2003-11-23 devnull {
53 fd04aace 2003-11-23 devnull memset(tm, 0, sizeof *tm);
54 fd04aace 2003-11-23 devnull tm->tm_sec = bigtm->sec;
55 fd04aace 2003-11-23 devnull tm->tm_min = bigtm->min;
56 fd04aace 2003-11-23 devnull tm->tm_hour = bigtm->hour;
57 fd04aace 2003-11-23 devnull tm->tm_mday = bigtm->mday;
58 fd04aace 2003-11-23 devnull tm->tm_mon = bigtm->mon;
59 fd04aace 2003-11-23 devnull tm->tm_year = bigtm->year;
60 fd04aace 2003-11-23 devnull tm->tm_wday = bigtm->wday;
61 1c253ceb 2003-11-23 devnull #ifdef _HAVETMZONE
62 fd04aace 2003-11-23 devnull tm->tm_zone = bigtm->zone;
63 1c253ceb 2003-11-23 devnull #endif
64 9cfbc441 2006-02-03 devnull #ifdef _HAVETMGMTOFF
65 fd04aace 2003-11-23 devnull tm->tm_gmtoff = bigtm->tzoff;
66 1c253ceb 2003-11-23 devnull #endif
67 fd04aace 2003-11-23 devnull }
68 fd04aace 2003-11-23 devnull
69 fd04aace 2003-11-23 devnull Tm*
70 829e8223 2004-12-29 devnull p9gmtime(long x)
71 fd04aace 2003-11-23 devnull {
72 829e8223 2004-12-29 devnull time_t t;
73 fd04aace 2003-11-23 devnull struct tm tm;
74 fd04aace 2003-11-23 devnull
75 829e8223 2004-12-29 devnull t = (time_t)x;
76 fd04aace 2003-11-23 devnull tm = *gmtime(&t);
77 fd04aace 2003-11-23 devnull tm2Tm(&tm, &bigtm);
78 fd04aace 2003-11-23 devnull return &bigtm;
79 fd04aace 2003-11-23 devnull }
80 fd04aace 2003-11-23 devnull
81 fd04aace 2003-11-23 devnull Tm*
82 829e8223 2004-12-29 devnull p9localtime(long x)
83 fd04aace 2003-11-23 devnull {
84 829e8223 2004-12-29 devnull time_t t;
85 fd04aace 2003-11-23 devnull struct tm tm;
86 fd04aace 2003-11-23 devnull
87 829e8223 2004-12-29 devnull t = (time_t)x;
88 fd04aace 2003-11-23 devnull tm = *localtime(&t);
89 fd04aace 2003-11-23 devnull tm2Tm(&tm, &bigtm);
90 fd04aace 2003-11-23 devnull return &bigtm;
91 fd04aace 2003-11-23 devnull }
92 fd04aace 2003-11-23 devnull
93 d2c4ee9e 2003-11-24 devnull #if !defined(_HAVETIMEGM)
94 d2c4ee9e 2003-11-24 devnull static time_t
95 1c253ceb 2003-11-23 devnull timegm(struct tm *tm)
96 1c253ceb 2003-11-23 devnull {
97 d2c4ee9e 2003-11-24 devnull time_t ret;
98 d2c4ee9e 2003-11-24 devnull char *tz;
99 64bcfff3 2003-11-25 devnull char *s;
100 d2c4ee9e 2003-11-24 devnull
101 d2c4ee9e 2003-11-24 devnull tz = getenv("TZ");
102 64bcfff3 2003-11-25 devnull putenv("TZ=");
103 d2c4ee9e 2003-11-24 devnull tzset();
104 d2c4ee9e 2003-11-24 devnull ret = mktime(tm);
105 64bcfff3 2003-11-25 devnull if(tz){
106 64bcfff3 2003-11-25 devnull s = smprint("TZ=%s", tz);
107 64bcfff3 2003-11-25 devnull if(s)
108 64bcfff3 2003-11-25 devnull putenv(s);
109 64bcfff3 2003-11-25 devnull }
110 d2c4ee9e 2003-11-24 devnull return ret;
111 1c253ceb 2003-11-23 devnull }
112 1c253ceb 2003-11-23 devnull #endif
113 1c253ceb 2003-11-23 devnull
114 fd04aace 2003-11-23 devnull long
115 fd04aace 2003-11-23 devnull p9tm2sec(Tm *bigtm)
116 fd04aace 2003-11-23 devnull {
117 fd04aace 2003-11-23 devnull struct tm tm;
118 fd04aace 2003-11-23 devnull
119 fd04aace 2003-11-23 devnull Tm2tm(bigtm, &tm);
120 fd04aace 2003-11-23 devnull if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)
121 fd04aace 2003-11-23 devnull return timegm(&tm);
122 fd04aace 2003-11-23 devnull return mktime(&tm); /* local time zone */
123 fd04aace 2003-11-23 devnull }
124 fd04aace 2003-11-23 devnull