Blob


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