xref: /csrg-svn/lib/libc/gen/timezone.c (revision 42627)
11993Swnj /*
230682Sbostic  * Copyright (c) 1987 Regents of the University of California.
332753Sbostic  * All rights reserved.
432753Sbostic  *
5*42627Sbostic  * %sccs.include.redist.c%
621365Sdist  */
721365Sdist 
830683Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*42627Sbostic static char sccsid[] = "@(#)timezone.c	5.9 (Berkeley) 06/01/90";
1032753Sbostic #endif /* LIBC_SCCS and not lint */
1121365Sdist 
1230682Sbostic #include <sys/types.h>
1330682Sbostic #include <sys/time.h>
1430682Sbostic #include <stdio.h>
1530682Sbostic #include <tzfile.h>
1630682Sbostic 
1721365Sdist /*
1830682Sbostic  * timezone --
1930682Sbostic  *	The arguments are the number of minutes of time you are westward
2030682Sbostic  *	from Greenwich and whether DST is in effect.  It returns a string
2130682Sbostic  *	giving the name of the local timezone.  Should be replaced, in the
2230682Sbostic  *	application code, by a call to localtime.
231993Swnj  */
241993Swnj 
2530682Sbostic static char	czone[TZ_MAX_CHARS];		/* space for zone name */
2630682Sbostic 
2730682Sbostic char *
2830682Sbostic timezone(zone, dst)
2930682Sbostic 	int	zone,
3030682Sbostic 		dst;
3130682Sbostic {
3230682Sbostic 	register char	*beg,
3330682Sbostic 			*end;
3430820Skarels 	char	*getenv(), *index(), *strncpy(), *_tztab();
3530682Sbostic 
3630682Sbostic 	if (beg = getenv("TZNAME")) {		/* set in environment */
3730682Sbostic 		if (end = index(beg, ',')) {	/* "PST,PDT" */
3830682Sbostic 			if (dst)
3930682Sbostic 				return(++end);
4030682Sbostic 			*end = '\0';
4130682Sbostic 			(void)strncpy(czone,beg,sizeof(czone) - 1);
4230682Sbostic 			czone[sizeof(czone) - 1] = '\0';
4330682Sbostic 			*end = ',';
4430682Sbostic 			return(czone);
4530682Sbostic 		}
4630682Sbostic 		return(beg);
4730682Sbostic 	}
4830820Skarels 	return(_tztab(zone,dst));	/* default: table or created zone */
4930682Sbostic }
5030682Sbostic 
511993Swnj static struct zone {
521993Swnj 	int	offset;
531993Swnj 	char	*stdzone;
541993Swnj 	char	*dlzone;
551993Swnj } zonetab[] = {
5630682Sbostic 	-1*60,	"MET",	"MET DST",	/* Middle European */
5730682Sbostic 	-2*60,	"EET",	"EET DST",	/* Eastern European */
5830682Sbostic 	4*60,	"AST",	"ADT",		/* Atlantic */
5930682Sbostic 	5*60,	"EST",	"EDT",		/* Eastern */
6030682Sbostic 	6*60,	"CST",	"CDT",		/* Central */
6130682Sbostic 	7*60,	"MST",	"MDT",		/* Mountain */
6230682Sbostic 	8*60,	"PST",	"PDT",		/* Pacific */
6313877Ssam #ifdef notdef
6413877Ssam 	/* there's no way to distinguish this from WET */
6530682Sbostic 	0,	"GMT",	0,		/* Greenwich */
6613877Ssam #endif
6730682Sbostic 	0*60,	"WET",	"WET DST",	/* Western European */
6830682Sbostic 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
6930682Sbostic      -10*60+30,	"CST",	"CST",		/* Aust: Central */
7030682Sbostic 	-8*60,	"WST",	0,		/* Aust: Western */
711993Swnj 	-1
721993Swnj };
731993Swnj 
7430682Sbostic /*
7530820Skarels  * _tztab --
7630682Sbostic  *	check static tables or create a new zone name; broken out so that
7730682Sbostic  *	we can make a guess as to what the zone is if the standard tables
7830682Sbostic  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
7930682Sbostic  *	STANDARD LIBRARY.
8030682Sbostic  */
8130682Sbostic char *
8230713Sbostic _tztab(zone,dst)
8330682Sbostic 	register int	zone;
8430682Sbostic 	int	dst;
851993Swnj {
8630682Sbostic 	register struct zone	*zp;
8730682Sbostic 	register char	sign;
881993Swnj 
8930682Sbostic 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
9030682Sbostic 		if (zp->offset == zone) {
911993Swnj 			if (dst && zp->dlzone)
921993Swnj 				return(zp->dlzone);
931993Swnj 			if (!dst && zp->stdzone)
941993Swnj 				return(zp->stdzone);
951993Swnj 		}
9630682Sbostic 
9730682Sbostic 	if (zone < 0) {					/* create one */
981993Swnj 		zone = -zone;
9930682Sbostic 		sign = '+';
10030682Sbostic 	}
10130682Sbostic 	else
10230682Sbostic 		sign = '-';
10330682Sbostic 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
1041993Swnj 	return(czone);
1051993Swnj }
106