xref: /csrg-svn/lib/libc/gen/timezone.c (revision 30682)
11993Swnj /*
2*30682Sbostic  * Copyright (c) 1987 Regents of the University of California.
321365Sdist  * All rights reserved.  The Berkeley software License Agreement
421365Sdist  * specifies the terms and conditions for redistribution.
521365Sdist  */
621365Sdist 
7*30682Sbostic #ifndef lint
8*30682Sbostic static char sccsid[] = "@(#)timezone.c	5.3 (Berkeley) 03/28/87";
9*30682Sbostic #endif
1021365Sdist 
11*30682Sbostic #include <sys/types.h>
12*30682Sbostic #include <sys/time.h>
13*30682Sbostic #include <stdio.h>
14*30682Sbostic #include <tzfile.h>
15*30682Sbostic 
1621365Sdist /*
17*30682Sbostic  * timezone --
18*30682Sbostic  *	The arguments are the number of minutes of time you are westward
19*30682Sbostic  *	from Greenwich and whether DST is in effect.  It returns a string
20*30682Sbostic  *	giving the name of the local timezone.  Should be replaced, in the
21*30682Sbostic  *	application code, by a call to localtime.
221993Swnj  */
231993Swnj 
24*30682Sbostic static char	czone[TZ_MAX_CHARS];		/* space for zone name */
25*30682Sbostic 
26*30682Sbostic char *
27*30682Sbostic timezone(zone, dst)
28*30682Sbostic 	int	zone,
29*30682Sbostic 		dst;
30*30682Sbostic {
31*30682Sbostic 	register char	*beg,
32*30682Sbostic 			*end;
33*30682Sbostic 	char	*getenv(), *index(), *strncpy(), *tztab();
34*30682Sbostic 
35*30682Sbostic 	if (beg = getenv("TZNAME")) {		/* set in environment */
36*30682Sbostic 		if (end = index(beg, ',')) {	/* "PST,PDT" */
37*30682Sbostic 			if (dst)
38*30682Sbostic 				return(++end);
39*30682Sbostic 			*end = '\0';
40*30682Sbostic 			(void)strncpy(czone,beg,sizeof(czone) - 1);
41*30682Sbostic 			czone[sizeof(czone) - 1] = '\0';
42*30682Sbostic 			*end = ',';
43*30682Sbostic 			return(czone);
44*30682Sbostic 		}
45*30682Sbostic 		return(beg);
46*30682Sbostic 	}
47*30682Sbostic 	return(tztab(zone,dst));	/* default: table or created zone */
48*30682Sbostic }
49*30682Sbostic 
501993Swnj static struct zone {
511993Swnj 	int	offset;
521993Swnj 	char	*stdzone;
531993Swnj 	char	*dlzone;
541993Swnj } zonetab[] = {
55*30682Sbostic 	-1*60,	"MET",	"MET DST",	/* Middle European */
56*30682Sbostic 	-2*60,	"EET",	"EET DST",	/* Eastern European */
57*30682Sbostic 	4*60,	"AST",	"ADT",		/* Atlantic */
58*30682Sbostic 	5*60,	"EST",	"EDT",		/* Eastern */
59*30682Sbostic 	6*60,	"CST",	"CDT",		/* Central */
60*30682Sbostic 	7*60,	"MST",	"MDT",		/* Mountain */
61*30682Sbostic 	8*60,	"PST",	"PDT",		/* Pacific */
6213877Ssam #ifdef notdef
6313877Ssam 	/* there's no way to distinguish this from WET */
64*30682Sbostic 	0,	"GMT",	0,		/* Greenwich */
6513877Ssam #endif
66*30682Sbostic 	0*60,	"WET",	"WET DST",	/* Western European */
67*30682Sbostic 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
68*30682Sbostic      -10*60+30,	"CST",	"CST",		/* Aust: Central */
69*30682Sbostic 	-8*60,	"WST",	0,		/* Aust: Western */
701993Swnj 	-1
711993Swnj };
721993Swnj 
73*30682Sbostic /*
74*30682Sbostic  * tztab --
75*30682Sbostic  *	check static tables or create a new zone name; broken out so that
76*30682Sbostic  *	we can make a guess as to what the zone is if the standard tables
77*30682Sbostic  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
78*30682Sbostic  *	STANDARD LIBRARY.
79*30682Sbostic  */
80*30682Sbostic char *
81*30682Sbostic tztab(zone,dst)
82*30682Sbostic 	register int	zone;
83*30682Sbostic 	int	dst;
841993Swnj {
85*30682Sbostic 	register struct zone	*zp;
86*30682Sbostic 	register char	sign;
871993Swnj 
88*30682Sbostic 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
89*30682Sbostic 		if (zp->offset == zone) {
901993Swnj 			if (dst && zp->dlzone)
911993Swnj 				return(zp->dlzone);
921993Swnj 			if (!dst && zp->stdzone)
931993Swnj 				return(zp->stdzone);
941993Swnj 		}
95*30682Sbostic 
96*30682Sbostic 	if (zone < 0) {					/* create one */
971993Swnj 		zone = -zone;
98*30682Sbostic 		sign = '+';
99*30682Sbostic 	}
100*30682Sbostic 	else
101*30682Sbostic 		sign = '-';
102*30682Sbostic 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
1031993Swnj 	return(czone);
1041993Swnj }
105