xref: /original-bsd/lib/libc/gen/timezone.c (revision 264c46cb)
1 /* @(#)timezone.c	4.2 (Berkeley) 06/10/83 */
2 /*
3  * The arguments are the number of minutes of time
4  * you are westward from Greenwich and whether DST is in effect.
5  * It returns a string
6  * giving the name of the local timezone.
7  *
8  * Sorry, I don't know all the names.
9  */
10 
11 static struct zone {
12 	int	offset;
13 	char	*stdzone;
14 	char	*dlzone;
15 } zonetab[] = {
16 	4*60, "AST", "ADT",		/* Atlantic */
17 	5*60, "EST", "EDT",		/* Eastern */
18 	6*60, "CST", "CDT",		/* Central */
19 	7*60, "MST", "MDT",		/* Mountain */
20 	8*60, "PST", "PDT",		/* Pacific */
21 	0, "GMT", 0,			/* Greenwich */
22 	-10*60, "EST", "EST",		/* Aust: Eastern */
23 	-10*60+30, "CST", "CST",	/* Aust: Central */
24 	-8*60, "WST", 0,		/* Aust: Western */
25 	-1
26 };
27 
28 char *timezone(zone, dst)
29 {
30 	register struct zone *zp;
31 	static char czone[10];
32 	char *sign;
33 	register char *p, *q;
34 	char *getenv(), *index();
35 
36 	if (p = getenv("TZNAME")) {
37 		if (q = index(p, ',')) {
38 			if (dst)
39 				return(++q);
40 			else {
41 				*q = '\0';
42 				strncpy(czone, p, sizeof(czone)-1);
43 				czone[sizeof(czone)-1] = '\0';
44 				*q = ',';
45 				return (czone);
46 			}
47 		}
48 		return(p);
49 	}
50 	for (zp=zonetab; zp->offset!=-1; zp++)
51 		if (zp->offset==zone) {
52 			if (dst && zp->dlzone)
53 				return(zp->dlzone);
54 			if (!dst && zp->stdzone)
55 				return(zp->stdzone);
56 		}
57 	if (zone<0) {
58 		zone = -zone;
59 		sign = "+";
60 	} else
61 		sign = "-";
62 	sprintf(czone, "GMT%s%d:%02d", sign, zone/60, zone%60);
63 	return(czone);
64 }
65