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