summaryrefslogtreecommitdiff
path: root/lib/libc/time/tzset.c
blob: ca54c46ed6cc5fd8d15955aefb5c4db9f6fa8119 (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
#include "stddef.h" // for NULL

#include <ctype.h> // for isdigit
#include <time.h>  // for daylight, timezone, tzname, tzset

int daylight = 0;
long timezone = 0;
char *tzname[2] = { "UTC", "UTC" };

void tzset(void)
{
	const char *tz = NULL; // getenv("TZ");

	if (tz == NULL || *tz == '\0') {
		timezone = 0;
		daylight = 0;
		tzname[0] = tzname[1] = "UTC";
		return;
	}

	const char *p = tz;
	char sign = 0;
	int hours = 0, mins = 0;

	static char stdname[8];
	int i = 0;
	while (*p && !isdigit((unsigned char)*p) && *p != '+' && *p != '-' && i < 7) {
		stdname[i++] = *p++;
	}

	stdname[i] = '\0';
	if (stdname[0])
		tzname[0] = stdname;
	else
		tzname[0] = "LCL";

	if (*p == '+' || *p == '-')
		sign = *p++;

	while (isdigit((unsigned char)*p))
		hours = hours * 10 + (*p++ - '0');

	if (*p == ':') {
		p++;
		while (isdigit((unsigned char)*p))
			mins = mins * 10 + (*p++ - '0');
	}

	int total = hours * 3600 + mins * 60;
	if (sign == '+')
		timezone = -total;
	else if (sign == '-')
		timezone = total;
	else
		timezone = 0;

	daylight = 0;
	tzname[1] = tzname[0];
}