Blame


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