aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/zoneinfo.c
blob: f8ae442dd3348d6557aad37fe694a052c7aee08a (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <u.h>
#include <libc.h>

/*
 * Access local time entries of zoneinfo files.
 * Formats 0 and 2 are supported, and 4-byte timestamps
 *
 * Copyright © 2008 M. Teichgräber
 * Contributed under the terms of the Lucent Public License 1.02.
 */
#include "zoneinfo.h"

static
struct Zoneinfo
{
	int	timecnt;		/* # of transition times */
	int	typecnt;		/* # of local time types */
	int	charcnt;		/* # of characters of time zone abbreviation strings */

	uchar *ptime;
	uchar *ptype;
	uchar *ptt;
	uchar *pzone;
} z;

static uchar *tzdata;

static
uchar*
readtzfile(char *file)
{
	uchar *p;
	int fd;
	Dir *d;

	fd = open(file, OREAD);
	if (fd<0)
		return nil;
	d = dirfstat(fd);
	if (d==nil)
		return nil;
	p = malloc(d->length);
	if (p!=nil)
		readn(fd, p, d->length);
	free(d);
	close(fd);
	return p;
}
static char *zonefile;
void
tzfile(char *f)
{
	if (tzdata!=nil) {
		free(tzdata);
		tzdata = nil;
	}
	z.timecnt = 0;
	zonefile = f;
}

static
long
get4(uchar *p)
{
	return (p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
}

enum {
	TTinfosz	= 4+1+1,
};

static
int
parsehead(void)
{
	uchar *p;
	int	ver;

	ver = tzdata[4];
	if (ver!=0)
	if (ver!='2')
		return -1;

	p = tzdata + 4 + 1 + 15;

	z.timecnt = get4(p+3*4);
	z.typecnt = get4(p+4*4);
	if (z.typecnt==0)
		return -1;
	z.charcnt = get4(p+5*4);
	z.ptime = p+6*4;
	z.ptype = z.ptime + z.timecnt*4;
	z.ptt = z.ptype + z.timecnt;
	z.pzone = z.ptt + z.typecnt*TTinfosz;
	return 0;
}

static
void
ttinfo(Tinfo *ti, int tti)
{
	uchar *p;
	int	i;

	i = z.ptype[tti];
	assert(i<z.typecnt);
	p = z.ptt + i*TTinfosz;
	ti->tzoff = get4(p);
	ti->dlflag = p[4];
	assert(p[5]<z.charcnt);
	ti->zone = (char*)z.pzone + p[5];
}

static
void
readtimezone(void)
{
	char *tmp;

	z.timecnt = 0;
	if(zonefile==nil) {
		if ((tmp=getenv("timezone"))!=nil) {
			tzdata = readtzfile(tmp);
			free(tmp);
			goto havedata;
		}
		zonefile = "/etc/localtime";
	}
	tzdata = readtzfile(zonefile);
	if (tzdata==nil)
		return;

havedata:
	if (strncmp("TZif", (char*)tzdata, 4)!=0)
		goto errfree;

	if (parsehead()==-1) {
	errfree:
		free(tzdata);
		tzdata = nil;
		z.timecnt = 0;
		return;
	}
}

static
tlong
gett4(uchar *p)
{
	long l;

	l = get4(p);
	if (l<0)
		return 0;
	return l;
}
int
zonetinfo(Tinfo *ti, int i)
{
	if (tzdata==nil)
		readtimezone();
	if (i<0 || i>=z.timecnt)
		return -1;
	ti->t = gett4(z.ptime + 4*i);
	ttinfo(ti, i);
	return i;
}

int
zonelookuptinfo(Tinfo *ti, tlong t)
{
	uchar *p;
	int	i;
	tlong	oldtt, tt;

	if (tzdata==nil)
		readtimezone();
	oldtt = 0;
	p = z.ptime;
	for (i=0; i<z.timecnt; i++) {
		tt = gett4(p);
		if (t<tt)
			break;
		oldtt = tt;
		p += 4;
	}
	if (i>0) {
		ttinfo(ti, i-1);
		ti->t = oldtt;
//		fprint(2, "t:%ld off:%d dflag:%d %s\n", (long)ti->t, ti->tzoff, ti->dlflag, ti->zone);
		return i-1;
	}
	return -1;
}

void
zonedump(int fd)
{
	int	i;
	uchar *p;
	tlong t;
	Tinfo ti;

	if (tzdata==nil)
		readtimezone();
	p = z.ptime;
	for (i=0; i<z.timecnt; i++) {
		t = gett4(p);
		ttinfo(&ti, i);
		fprint(fd, "%ld\t%d\t%d\t%s\n", (long)t, ti.tzoff, ti.dlflag, ti.zone);
		p += 4;
	}
}