xref: /original-bsd/lib/libc/gen/timezone.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)timezone.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <tzfile.h>
18 
19 char *_tztab();
20 
21 /*
22  * timezone --
23  *	The arguments are the number of minutes of time you are westward
24  *	from Greenwich and whether DST is in effect.  It returns a string
25  *	giving the name of the local timezone.  Should be replaced, in the
26  *	application code, by a call to localtime.
27  */
28 
29 static char	czone[TZ_MAX_CHARS];		/* space for zone name */
30 
31 char *
32 timezone(zone, dst)
33 	int	zone,
34 		dst;
35 {
36 	register char	*beg,
37 			*end;
38 
39 	if (beg = getenv("TZNAME")) {		/* set in environment */
40 		if (end = index(beg, ',')) {	/* "PST,PDT" */
41 			if (dst)
42 				return(++end);
43 			*end = '\0';
44 			(void)strncpy(czone,beg,sizeof(czone) - 1);
45 			czone[sizeof(czone) - 1] = '\0';
46 			*end = ',';
47 			return(czone);
48 		}
49 		return(beg);
50 	}
51 	return(_tztab(zone,dst));	/* default: table or created zone */
52 }
53 
54 static struct zone {
55 	int	offset;
56 	char	*stdzone;
57 	char	*dlzone;
58 } zonetab[] = {
59 	-1*60,	"MET",	"MET DST",	/* Middle European */
60 	-2*60,	"EET",	"EET DST",	/* Eastern European */
61 	4*60,	"AST",	"ADT",		/* Atlantic */
62 	5*60,	"EST",	"EDT",		/* Eastern */
63 	6*60,	"CST",	"CDT",		/* Central */
64 	7*60,	"MST",	"MDT",		/* Mountain */
65 	8*60,	"PST",	"PDT",		/* Pacific */
66 #ifdef notdef
67 	/* there's no way to distinguish this from WET */
68 	0,	"GMT",	0,		/* Greenwich */
69 #endif
70 	0*60,	"WET",	"WET DST",	/* Western European */
71 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
72      -10*60+30,	"CST",	"CST",		/* Aust: Central */
73 	-8*60,	"WST",	0,		/* Aust: Western */
74 	-1
75 };
76 
77 /*
78  * _tztab --
79  *	check static tables or create a new zone name; broken out so that
80  *	we can make a guess as to what the zone is if the standard tables
81  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
82  *	STANDARD LIBRARY.
83  */
84 char *
85 _tztab(zone,dst)
86 	register int	zone;
87 	int	dst;
88 {
89 	register struct zone	*zp;
90 	register char	sign;
91 
92 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
93 		if (zp->offset == zone) {
94 			if (dst && zp->dlzone)
95 				return(zp->dlzone);
96 			if (!dst && zp->stdzone)
97 				return(zp->stdzone);
98 		}
99 
100 	if (zone < 0) {					/* create one */
101 		zone = -zone;
102 		sign = '+';
103 	}
104 	else
105 		sign = '-';
106 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
107 	return(czone);
108 }
109