xref: /csrg-svn/lib/libc/gen/timezone.c (revision 32753)
11993Swnj /*
230682Sbostic  * Copyright (c) 1987 Regents of the University of California.
3*32753Sbostic  * All rights reserved.
4*32753Sbostic  *
5*32753Sbostic  * Redistribution and use in source and binary forms are permitted
6*32753Sbostic  * provided that this notice is preserved and that due credit is given
7*32753Sbostic  * to the University of California at Berkeley. The name of the University
8*32753Sbostic  * may not be used to endorse or promote products derived from this
9*32753Sbostic  * software without specific written prior permission. This software
10*32753Sbostic  * is provided ``as is'' without express or implied warranty.
1121365Sdist  */
1221365Sdist 
1330683Sbostic #if defined(LIBC_SCCS) && !defined(lint)
14*32753Sbostic static char sccsid[] = "@(#)timezone.c	5.7 (Berkeley) 12/03/87";
15*32753Sbostic #endif /* LIBC_SCCS and not lint */
1621365Sdist 
1730682Sbostic #include <sys/types.h>
1830682Sbostic #include <sys/time.h>
1930682Sbostic #include <stdio.h>
2030682Sbostic #include <tzfile.h>
2130682Sbostic 
2221365Sdist /*
2330682Sbostic  * timezone --
2430682Sbostic  *	The arguments are the number of minutes of time you are westward
2530682Sbostic  *	from Greenwich and whether DST is in effect.  It returns a string
2630682Sbostic  *	giving the name of the local timezone.  Should be replaced, in the
2730682Sbostic  *	application code, by a call to localtime.
281993Swnj  */
291993Swnj 
3030682Sbostic static char	czone[TZ_MAX_CHARS];		/* space for zone name */
3130682Sbostic 
3230682Sbostic char *
3330682Sbostic timezone(zone, dst)
3430682Sbostic 	int	zone,
3530682Sbostic 		dst;
3630682Sbostic {
3730682Sbostic 	register char	*beg,
3830682Sbostic 			*end;
3930820Skarels 	char	*getenv(), *index(), *strncpy(), *_tztab();
4030682Sbostic 
4130682Sbostic 	if (beg = getenv("TZNAME")) {		/* set in environment */
4230682Sbostic 		if (end = index(beg, ',')) {	/* "PST,PDT" */
4330682Sbostic 			if (dst)
4430682Sbostic 				return(++end);
4530682Sbostic 			*end = '\0';
4630682Sbostic 			(void)strncpy(czone,beg,sizeof(czone) - 1);
4730682Sbostic 			czone[sizeof(czone) - 1] = '\0';
4830682Sbostic 			*end = ',';
4930682Sbostic 			return(czone);
5030682Sbostic 		}
5130682Sbostic 		return(beg);
5230682Sbostic 	}
5330820Skarels 	return(_tztab(zone,dst));	/* default: table or created zone */
5430682Sbostic }
5530682Sbostic 
561993Swnj static struct zone {
571993Swnj 	int	offset;
581993Swnj 	char	*stdzone;
591993Swnj 	char	*dlzone;
601993Swnj } zonetab[] = {
6130682Sbostic 	-1*60,	"MET",	"MET DST",	/* Middle European */
6230682Sbostic 	-2*60,	"EET",	"EET DST",	/* Eastern European */
6330682Sbostic 	4*60,	"AST",	"ADT",		/* Atlantic */
6430682Sbostic 	5*60,	"EST",	"EDT",		/* Eastern */
6530682Sbostic 	6*60,	"CST",	"CDT",		/* Central */
6630682Sbostic 	7*60,	"MST",	"MDT",		/* Mountain */
6730682Sbostic 	8*60,	"PST",	"PDT",		/* Pacific */
6813877Ssam #ifdef notdef
6913877Ssam 	/* there's no way to distinguish this from WET */
7030682Sbostic 	0,	"GMT",	0,		/* Greenwich */
7113877Ssam #endif
7230682Sbostic 	0*60,	"WET",	"WET DST",	/* Western European */
7330682Sbostic 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
7430682Sbostic      -10*60+30,	"CST",	"CST",		/* Aust: Central */
7530682Sbostic 	-8*60,	"WST",	0,		/* Aust: Western */
761993Swnj 	-1
771993Swnj };
781993Swnj 
7930682Sbostic /*
8030820Skarels  * _tztab --
8130682Sbostic  *	check static tables or create a new zone name; broken out so that
8230682Sbostic  *	we can make a guess as to what the zone is if the standard tables
8330682Sbostic  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
8430682Sbostic  *	STANDARD LIBRARY.
8530682Sbostic  */
8630682Sbostic char *
8730713Sbostic _tztab(zone,dst)
8830682Sbostic 	register int	zone;
8930682Sbostic 	int	dst;
901993Swnj {
9130682Sbostic 	register struct zone	*zp;
9230682Sbostic 	register char	sign;
931993Swnj 
9430682Sbostic 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
9530682Sbostic 		if (zp->offset == zone) {
961993Swnj 			if (dst && zp->dlzone)
971993Swnj 				return(zp->dlzone);
981993Swnj 			if (!dst && zp->stdzone)
991993Swnj 				return(zp->stdzone);
1001993Swnj 		}
10130682Sbostic 
10230682Sbostic 	if (zone < 0) {					/* create one */
1031993Swnj 		zone = -zone;
10430682Sbostic 		sign = '+';
10530682Sbostic 	}
10630682Sbostic 	else
10730682Sbostic 		sign = '-';
10830682Sbostic 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
1091993Swnj 	return(czone);
1101993Swnj }
111