xref: /csrg-svn/lib/libc/gen/timezone.c (revision 30820)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)timezone.c	5.6 (Berkeley) 04/06/87";
9 #endif LIBC_SCCS and not lint
10 
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <stdio.h>
14 #include <tzfile.h>
15 
16 /*
17  * timezone --
18  *	The arguments are the number of minutes of time you are westward
19  *	from Greenwich and whether DST is in effect.  It returns a string
20  *	giving the name of the local timezone.  Should be replaced, in the
21  *	application code, by a call to localtime.
22  */
23 
24 static char	czone[TZ_MAX_CHARS];		/* space for zone name */
25 
26 char *
27 timezone(zone, dst)
28 	int	zone,
29 		dst;
30 {
31 	register char	*beg,
32 			*end;
33 	char	*getenv(), *index(), *strncpy(), *_tztab();
34 
35 	if (beg = getenv("TZNAME")) {		/* set in environment */
36 		if (end = index(beg, ',')) {	/* "PST,PDT" */
37 			if (dst)
38 				return(++end);
39 			*end = '\0';
40 			(void)strncpy(czone,beg,sizeof(czone) - 1);
41 			czone[sizeof(czone) - 1] = '\0';
42 			*end = ',';
43 			return(czone);
44 		}
45 		return(beg);
46 	}
47 	return(_tztab(zone,dst));	/* default: table or created zone */
48 }
49 
50 static struct zone {
51 	int	offset;
52 	char	*stdzone;
53 	char	*dlzone;
54 } zonetab[] = {
55 	-1*60,	"MET",	"MET DST",	/* Middle European */
56 	-2*60,	"EET",	"EET DST",	/* Eastern European */
57 	4*60,	"AST",	"ADT",		/* Atlantic */
58 	5*60,	"EST",	"EDT",		/* Eastern */
59 	6*60,	"CST",	"CDT",		/* Central */
60 	7*60,	"MST",	"MDT",		/* Mountain */
61 	8*60,	"PST",	"PDT",		/* Pacific */
62 #ifdef notdef
63 	/* there's no way to distinguish this from WET */
64 	0,	"GMT",	0,		/* Greenwich */
65 #endif
66 	0*60,	"WET",	"WET DST",	/* Western European */
67 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
68      -10*60+30,	"CST",	"CST",		/* Aust: Central */
69 	-8*60,	"WST",	0,		/* Aust: Western */
70 	-1
71 };
72 
73 /*
74  * _tztab --
75  *	check static tables or create a new zone name; broken out so that
76  *	we can make a guess as to what the zone is if the standard tables
77  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
78  *	STANDARD LIBRARY.
79  */
80 char *
81 _tztab(zone,dst)
82 	register int	zone;
83 	int	dst;
84 {
85 	register struct zone	*zp;
86 	register char	sign;
87 
88 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
89 		if (zp->offset == zone) {
90 			if (dst && zp->dlzone)
91 				return(zp->dlzone);
92 			if (!dst && zp->stdzone)
93 				return(zp->stdzone);
94 		}
95 
96 	if (zone < 0) {					/* create one */
97 		zone = -zone;
98 		sign = '+';
99 	}
100 	else
101 		sign = '-';
102 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
103 	return(czone);
104 }
105