1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#define NOPLAN9DEFINES
#include <u.h>
#include <libc.h>
#include <stdlib.h> /* setenv etc. */
#include <time.h>
static int didtz;
static int tzdelta;
static char tzone[4];
static void
dotz(void)
{
time_t t;
struct tm *gtm;
struct tm tm;
if(didtz)
return;
didtz = 1;
t = time(0);
strftime(tzone, sizeof tzone, "%Z", localtime(&t));
tm = *localtime(&t); /* set local time zone field */
gtm = gmtime(&t);
tm.tm_sec = gtm->tm_sec;
tm.tm_min = gtm->tm_min;
tm.tm_hour = gtm->tm_hour;
tm.tm_mday = gtm->tm_mday;
tm.tm_mon = gtm->tm_mon;
tm.tm_year = gtm->tm_year;
tm.tm_wday = gtm->tm_wday;
tzdelta = t - mktime(&tm);
}
static void
tm2Tm(struct tm *tm, Tm *bigtm, int gmt)
{
memset(bigtm, 0, sizeof *bigtm);
bigtm->sec = tm->tm_sec;
bigtm->min = tm->tm_min;
bigtm->hour = tm->tm_hour;
bigtm->mday = tm->tm_mday;
bigtm->mon = tm->tm_mon;
bigtm->year = tm->tm_year;
bigtm->wday = tm->tm_wday;
if(gmt){
strcpy(bigtm->zone, "GMT");
bigtm->tzoff = 0;
}else{
dotz();
strcpy(bigtm->zone, tzone);
bigtm->tzoff = tzdelta;
}
}
static void
Tm2tm(Tm *bigtm, struct tm *tm)
{
/* initialize with current time to get local time zone! (tm_isdst) */
time_t t;
time(&t);
*tm = *localtime(&t);
tm->tm_sec = bigtm->sec;
tm->tm_min = bigtm->min;
tm->tm_hour = bigtm->hour;
tm->tm_mday = bigtm->mday;
tm->tm_mon = bigtm->mon;
tm->tm_year = bigtm->year;
tm->tm_wday = bigtm->wday;
}
Tm*
p9gmtime(long x)
{
time_t t;
struct tm tm;
static Tm bigtm;
t = (time_t)x;
tm = *gmtime(&t);
tm2Tm(&tm, &bigtm, 1);
return &bigtm;
}
Tm*
p9localtime(long x)
{
time_t t;
struct tm tm;
static Tm bigtm;
t = (time_t)x;
tm = *localtime(&t);
tm2Tm(&tm, &bigtm, 0);
return &bigtm;
}
long
p9tm2sec(Tm *bigtm)
{
time_t t;
struct tm tm;
Tm2tm(bigtm, &tm);
t = mktime(&tm);
if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0){
dotz();
t += tzdelta;
}
return t;
}
|