Blob


1 #include <stdlib.h> /* setenv etc. */
3 #include <u.h>
4 #define NOPLAN9DEFINES
5 #include <libc.h>
6 #include <time.h>
8 #define _HAVETIMEGM 1
9 #define _HAVETMZONE 1
10 #define _HAVETMTZOFF 1
12 #if defined(__linux__)
13 # undef _HAVETMZONE
14 # undef _HAVETMTZOFF
16 #elif defined(__sun__)
17 # undef _HAVETIMEGM
18 # undef _HAVETMZONE
19 # undef _HAVETMTZOFF
21 #endif
23 static Tm bigtm;
25 static void
26 tm2Tm(struct tm *tm, Tm *bigtm)
27 {
28 memset(bigtm, 0, sizeof *bigtm);
29 bigtm->sec = tm->tm_sec;
30 bigtm->min = tm->tm_min;
31 bigtm->hour = tm->tm_hour;
32 bigtm->mday = tm->tm_mday;
33 bigtm->mon = tm->tm_mon;
34 bigtm->year = tm->tm_year;
35 bigtm->wday = tm->tm_wday;
36 #ifdef _HAVETMZONE
37 strecpy(bigtm->zone, bigtm->zone+4, tm->tm_zone);
38 #endif
39 #ifdef _HAVETZOFF
40 bigtm->tzoff = tm->tm_gmtoff;
41 #endif
42 }
44 static void
45 Tm2tm(Tm *bigtm, struct tm *tm)
46 {
47 memset(tm, 0, sizeof *tm);
48 tm->tm_sec = bigtm->sec;
49 tm->tm_min = bigtm->min;
50 tm->tm_hour = bigtm->hour;
51 tm->tm_mday = bigtm->mday;
52 tm->tm_mon = bigtm->mon;
53 tm->tm_year = bigtm->year;
54 tm->tm_wday = bigtm->wday;
55 #ifdef _HAVETMZONE
56 tm->tm_zone = bigtm->zone;
57 #endif
58 #ifdef _HAVETZOFF
59 tm->tm_gmtoff = bigtm->tzoff;
60 #endif
61 }
63 Tm*
64 p9gmtime(long t)
65 {
66 struct tm tm;
68 tm = *gmtime(&t);
69 tm2Tm(&tm, &bigtm);
70 return &bigtm;
71 }
73 Tm*
74 p9localtime(long t)
75 {
76 struct tm tm;
78 tm = *localtime(&t);
79 tm2Tm(&tm, &bigtm);
80 return &bigtm;
81 }
83 #if !defined(_HAVETIMEGM)
84 static time_t
85 timegm(struct tm *tm)
86 {
87 time_t ret;
88 char *tz;
89 char *s;
91 tz = getenv("TZ");
92 putenv("TZ=");
93 tzset();
94 ret = mktime(tm);
95 if(tz){
96 s = smprint("TZ=%s", tz);
97 if(s)
98 putenv(s);
99 }
100 return ret;
102 #endif
104 long
105 p9tm2sec(Tm *bigtm)
107 struct tm tm;
109 Tm2tm(bigtm, &tm);
110 if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)
111 return timegm(&tm);
112 return mktime(&tm); /* local time zone */