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 64bcfff3 2003-11-25 devnull #define _HAVETMTZOFF 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 64bcfff3 2003-11-25 devnull # undef _HAVETMTZOFF
14 fd04aace 2003-11-23 devnull
15 64bcfff3 2003-11-25 devnull #elif defined(__sun__)
16 64bcfff3 2003-11-25 devnull # undef _HAVETIMEGM
17 64bcfff3 2003-11-25 devnull # undef _HAVETMZONE
18 64bcfff3 2003-11-25 devnull # undef _HAVETMTZOFF
19 64bcfff3 2003-11-25 devnull
20 64bcfff3 2003-11-25 devnull #endif
21 64bcfff3 2003-11-25 devnull
22 fd04aace 2003-11-23 devnull static Tm bigtm;
23 fd04aace 2003-11-23 devnull
24 fd04aace 2003-11-23 devnull static void
25 fd04aace 2003-11-23 devnull tm2Tm(struct tm *tm, Tm *bigtm)
26 fd04aace 2003-11-23 devnull {
27 8ad51794 2004-03-25 devnull char *s;
28 8ad51794 2004-03-25 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 1c253ceb 2003-11-23 devnull #ifdef _HAVETMZONE
38 fd04aace 2003-11-23 devnull strecpy(bigtm->zone, bigtm->zone+4, tm->tm_zone);
39 1c253ceb 2003-11-23 devnull #endif
40 1c253ceb 2003-11-23 devnull #ifdef _HAVETZOFF
41 fd04aace 2003-11-23 devnull bigtm->tzoff = tm->tm_gmtoff;
42 1c253ceb 2003-11-23 devnull #endif
43 8ad51794 2004-03-25 devnull if(bigtm->zone[0] == 0){
44 8ad51794 2004-03-25 devnull s = getenv("TIMEZONE");
45 8ad51794 2004-03-25 devnull if(s){
46 be22ae2d 2004-03-26 devnull strecpy(bigtm->zone, bigtm->zone+4, s);
47 8ad51794 2004-03-25 devnull free(s);
48 8ad51794 2004-03-25 devnull }
49 8ad51794 2004-03-25 devnull }
50 fd04aace 2003-11-23 devnull }
51 fd04aace 2003-11-23 devnull
52 fd04aace 2003-11-23 devnull static void
53 fd04aace 2003-11-23 devnull Tm2tm(Tm *bigtm, struct tm *tm)
54 fd04aace 2003-11-23 devnull {
55 fd04aace 2003-11-23 devnull memset(tm, 0, sizeof *tm);
56 fd04aace 2003-11-23 devnull tm->tm_sec = bigtm->sec;
57 fd04aace 2003-11-23 devnull tm->tm_min = bigtm->min;
58 fd04aace 2003-11-23 devnull tm->tm_hour = bigtm->hour;
59 fd04aace 2003-11-23 devnull tm->tm_mday = bigtm->mday;
60 fd04aace 2003-11-23 devnull tm->tm_mon = bigtm->mon;
61 fd04aace 2003-11-23 devnull tm->tm_year = bigtm->year;
62 fd04aace 2003-11-23 devnull tm->tm_wday = bigtm->wday;
63 1c253ceb 2003-11-23 devnull #ifdef _HAVETMZONE
64 fd04aace 2003-11-23 devnull tm->tm_zone = bigtm->zone;
65 1c253ceb 2003-11-23 devnull #endif
66 1c253ceb 2003-11-23 devnull #ifdef _HAVETZOFF
67 fd04aace 2003-11-23 devnull tm->tm_gmtoff = bigtm->tzoff;
68 1c253ceb 2003-11-23 devnull #endif
69 fd04aace 2003-11-23 devnull }
70 fd04aace 2003-11-23 devnull
71 fd04aace 2003-11-23 devnull Tm*
72 fd04aace 2003-11-23 devnull p9gmtime(long t)
73 fd04aace 2003-11-23 devnull {
74 fd04aace 2003-11-23 devnull struct tm tm;
75 fd04aace 2003-11-23 devnull
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 fd04aace 2003-11-23 devnull p9localtime(long t)
83 fd04aace 2003-11-23 devnull {
84 fd04aace 2003-11-23 devnull struct tm tm;
85 fd04aace 2003-11-23 devnull
86 fd04aace 2003-11-23 devnull tm = *localtime(&t);
87 fd04aace 2003-11-23 devnull tm2Tm(&tm, &bigtm);
88 fd04aace 2003-11-23 devnull return &bigtm;
89 fd04aace 2003-11-23 devnull }
90 fd04aace 2003-11-23 devnull
91 d2c4ee9e 2003-11-24 devnull #if !defined(_HAVETIMEGM)
92 d2c4ee9e 2003-11-24 devnull static time_t
93 1c253ceb 2003-11-23 devnull timegm(struct tm *tm)
94 1c253ceb 2003-11-23 devnull {
95 d2c4ee9e 2003-11-24 devnull time_t ret;
96 d2c4ee9e 2003-11-24 devnull char *tz;
97 64bcfff3 2003-11-25 devnull char *s;
98 d2c4ee9e 2003-11-24 devnull
99 d2c4ee9e 2003-11-24 devnull tz = getenv("TZ");
100 64bcfff3 2003-11-25 devnull putenv("TZ=");
101 d2c4ee9e 2003-11-24 devnull tzset();
102 d2c4ee9e 2003-11-24 devnull ret = mktime(tm);
103 64bcfff3 2003-11-25 devnull if(tz){
104 64bcfff3 2003-11-25 devnull s = smprint("TZ=%s", tz);
105 64bcfff3 2003-11-25 devnull if(s)
106 64bcfff3 2003-11-25 devnull putenv(s);
107 64bcfff3 2003-11-25 devnull }
108 d2c4ee9e 2003-11-24 devnull return ret;
109 1c253ceb 2003-11-23 devnull }
110 1c253ceb 2003-11-23 devnull #endif
111 1c253ceb 2003-11-23 devnull
112 fd04aace 2003-11-23 devnull long
113 fd04aace 2003-11-23 devnull p9tm2sec(Tm *bigtm)
114 fd04aace 2003-11-23 devnull {
115 fd04aace 2003-11-23 devnull struct tm tm;
116 fd04aace 2003-11-23 devnull
117 fd04aace 2003-11-23 devnull Tm2tm(bigtm, &tm);
118 fd04aace 2003-11-23 devnull if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)
119 fd04aace 2003-11-23 devnull return timegm(&tm);
120 fd04aace 2003-11-23 devnull return mktime(&tm); /* local time zone */
121 fd04aace 2003-11-23 devnull }
122 fd04aace 2003-11-23 devnull