xref: /csrg-svn/lib/libc/gen/timezone.c (revision 61111)
11993Swnj /*
2*61111Sbostic  * Copyright (c) 1987, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
432753Sbostic  *
542627Sbostic  * %sccs.include.redist.c%
621365Sdist  */
721365Sdist 
830683Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)timezone.c	8.1 (Berkeley) 06/04/93";
1032753Sbostic #endif /* LIBC_SCCS and not lint */
1121365Sdist 
1230682Sbostic #include <sys/types.h>
1330682Sbostic #include <sys/time.h>
1430682Sbostic #include <stdio.h>
1546597Sdonn #include <stdlib.h>
1646597Sdonn #include <string.h>
1730682Sbostic #include <tzfile.h>
1830682Sbostic 
1946597Sdonn char *_tztab();
2046597Sdonn 
2121365Sdist /*
2230682Sbostic  * timezone --
2330682Sbostic  *	The arguments are the number of minutes of time you are westward
2430682Sbostic  *	from Greenwich and whether DST is in effect.  It returns a string
2530682Sbostic  *	giving the name of the local timezone.  Should be replaced, in the
2630682Sbostic  *	application code, by a call to localtime.
271993Swnj  */
281993Swnj 
2930682Sbostic static char	czone[TZ_MAX_CHARS];		/* space for zone name */
3030682Sbostic 
3130682Sbostic char *
timezone(zone,dst)3230682Sbostic timezone(zone, dst)
3330682Sbostic 	int	zone,
3430682Sbostic 		dst;
3530682Sbostic {
3630682Sbostic 	register char	*beg,
3730682Sbostic 			*end;
3830682Sbostic 
3930682Sbostic 	if (beg = getenv("TZNAME")) {		/* set in environment */
4030682Sbostic 		if (end = index(beg, ',')) {	/* "PST,PDT" */
4130682Sbostic 			if (dst)
4230682Sbostic 				return(++end);
4330682Sbostic 			*end = '\0';
4430682Sbostic 			(void)strncpy(czone,beg,sizeof(czone) - 1);
4530682Sbostic 			czone[sizeof(czone) - 1] = '\0';
4630682Sbostic 			*end = ',';
4730682Sbostic 			return(czone);
4830682Sbostic 		}
4930682Sbostic 		return(beg);
5030682Sbostic 	}
5130820Skarels 	return(_tztab(zone,dst));	/* default: table or created zone */
5230682Sbostic }
5330682Sbostic 
541993Swnj static struct zone {
551993Swnj 	int	offset;
561993Swnj 	char	*stdzone;
571993Swnj 	char	*dlzone;
581993Swnj } zonetab[] = {
5930682Sbostic 	-1*60,	"MET",	"MET DST",	/* Middle European */
6030682Sbostic 	-2*60,	"EET",	"EET DST",	/* Eastern European */
6130682Sbostic 	4*60,	"AST",	"ADT",		/* Atlantic */
6230682Sbostic 	5*60,	"EST",	"EDT",		/* Eastern */
6330682Sbostic 	6*60,	"CST",	"CDT",		/* Central */
6430682Sbostic 	7*60,	"MST",	"MDT",		/* Mountain */
6530682Sbostic 	8*60,	"PST",	"PDT",		/* Pacific */
6613877Ssam #ifdef notdef
6713877Ssam 	/* there's no way to distinguish this from WET */
6830682Sbostic 	0,	"GMT",	0,		/* Greenwich */
6913877Ssam #endif
7030682Sbostic 	0*60,	"WET",	"WET DST",	/* Western European */
7130682Sbostic 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
7230682Sbostic      -10*60+30,	"CST",	"CST",		/* Aust: Central */
7330682Sbostic 	-8*60,	"WST",	0,		/* Aust: Western */
741993Swnj 	-1
751993Swnj };
761993Swnj 
7730682Sbostic /*
7830820Skarels  * _tztab --
7930682Sbostic  *	check static tables or create a new zone name; broken out so that
8030682Sbostic  *	we can make a guess as to what the zone is if the standard tables
8130682Sbostic  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
8230682Sbostic  *	STANDARD LIBRARY.
8330682Sbostic  */
8430682Sbostic char *
_tztab(zone,dst)8530713Sbostic _tztab(zone,dst)
8630682Sbostic 	register int	zone;
8730682Sbostic 	int	dst;
881993Swnj {
8930682Sbostic 	register struct zone	*zp;
9030682Sbostic 	register char	sign;
911993Swnj 
9230682Sbostic 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
9330682Sbostic 		if (zp->offset == zone) {
941993Swnj 			if (dst && zp->dlzone)
951993Swnj 				return(zp->dlzone);
961993Swnj 			if (!dst && zp->stdzone)
971993Swnj 				return(zp->stdzone);
981993Swnj 		}
9930682Sbostic 
10030682Sbostic 	if (zone < 0) {					/* create one */
1011993Swnj 		zone = -zone;
10230682Sbostic 		sign = '+';
10330682Sbostic 	}
10430682Sbostic 	else
10530682Sbostic 		sign = '-';
10630682Sbostic 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
1071993Swnj 	return(czone);
1081993Swnj }
109