xref: /csrg-svn/lib/libc/gen/timezone.c (revision 30820)
11993Swnj /*
230682Sbostic  * 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 
730683Sbostic #if defined(LIBC_SCCS) && !defined(lint)
8*30820Skarels static char sccsid[] = "@(#)timezone.c	5.6 (Berkeley) 04/06/87";
930683Sbostic #endif LIBC_SCCS and not lint
1021365Sdist 
1130682Sbostic #include <sys/types.h>
1230682Sbostic #include <sys/time.h>
1330682Sbostic #include <stdio.h>
1430682Sbostic #include <tzfile.h>
1530682Sbostic 
1621365Sdist /*
1730682Sbostic  * timezone --
1830682Sbostic  *	The arguments are the number of minutes of time you are westward
1930682Sbostic  *	from Greenwich and whether DST is in effect.  It returns a string
2030682Sbostic  *	giving the name of the local timezone.  Should be replaced, in the
2130682Sbostic  *	application code, by a call to localtime.
221993Swnj  */
231993Swnj 
2430682Sbostic static char	czone[TZ_MAX_CHARS];		/* space for zone name */
2530682Sbostic 
2630682Sbostic char *
2730682Sbostic timezone(zone, dst)
2830682Sbostic 	int	zone,
2930682Sbostic 		dst;
3030682Sbostic {
3130682Sbostic 	register char	*beg,
3230682Sbostic 			*end;
33*30820Skarels 	char	*getenv(), *index(), *strncpy(), *_tztab();
3430682Sbostic 
3530682Sbostic 	if (beg = getenv("TZNAME")) {		/* set in environment */
3630682Sbostic 		if (end = index(beg, ',')) {	/* "PST,PDT" */
3730682Sbostic 			if (dst)
3830682Sbostic 				return(++end);
3930682Sbostic 			*end = '\0';
4030682Sbostic 			(void)strncpy(czone,beg,sizeof(czone) - 1);
4130682Sbostic 			czone[sizeof(czone) - 1] = '\0';
4230682Sbostic 			*end = ',';
4330682Sbostic 			return(czone);
4430682Sbostic 		}
4530682Sbostic 		return(beg);
4630682Sbostic 	}
47*30820Skarels 	return(_tztab(zone,dst));	/* default: table or created zone */
4830682Sbostic }
4930682Sbostic 
501993Swnj static struct zone {
511993Swnj 	int	offset;
521993Swnj 	char	*stdzone;
531993Swnj 	char	*dlzone;
541993Swnj } zonetab[] = {
5530682Sbostic 	-1*60,	"MET",	"MET DST",	/* Middle European */
5630682Sbostic 	-2*60,	"EET",	"EET DST",	/* Eastern European */
5730682Sbostic 	4*60,	"AST",	"ADT",		/* Atlantic */
5830682Sbostic 	5*60,	"EST",	"EDT",		/* Eastern */
5930682Sbostic 	6*60,	"CST",	"CDT",		/* Central */
6030682Sbostic 	7*60,	"MST",	"MDT",		/* Mountain */
6130682Sbostic 	8*60,	"PST",	"PDT",		/* Pacific */
6213877Ssam #ifdef notdef
6313877Ssam 	/* there's no way to distinguish this from WET */
6430682Sbostic 	0,	"GMT",	0,		/* Greenwich */
6513877Ssam #endif
6630682Sbostic 	0*60,	"WET",	"WET DST",	/* Western European */
6730682Sbostic 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
6830682Sbostic      -10*60+30,	"CST",	"CST",		/* Aust: Central */
6930682Sbostic 	-8*60,	"WST",	0,		/* Aust: Western */
701993Swnj 	-1
711993Swnj };
721993Swnj 
7330682Sbostic /*
74*30820Skarels  * _tztab --
7530682Sbostic  *	check static tables or create a new zone name; broken out so that
7630682Sbostic  *	we can make a guess as to what the zone is if the standard tables
7730682Sbostic  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
7830682Sbostic  *	STANDARD LIBRARY.
7930682Sbostic  */
8030682Sbostic char *
8130713Sbostic _tztab(zone,dst)
8230682Sbostic 	register int	zone;
8330682Sbostic 	int	dst;
841993Swnj {
8530682Sbostic 	register struct zone	*zp;
8630682Sbostic 	register char	sign;
871993Swnj 
8830682Sbostic 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
8930682Sbostic 		if (zp->offset == zone) {
901993Swnj 			if (dst && zp->dlzone)
911993Swnj 				return(zp->dlzone);
921993Swnj 			if (!dst && zp->stdzone)
931993Swnj 				return(zp->stdzone);
941993Swnj 		}
9530682Sbostic 
9630682Sbostic 	if (zone < 0) {					/* create one */
971993Swnj 		zone = -zone;
9830682Sbostic 		sign = '+';
9930682Sbostic 	}
10030682Sbostic 	else
10130682Sbostic 		sign = '-';
10230682Sbostic 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
1031993Swnj 	return(czone);
1041993Swnj }
105