xref: /csrg-svn/lib/libc/gen/timezone.c (revision 34821)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)timezone.c	5.8 (Berkeley) 06/27/88";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <stdio.h>
25 #include <tzfile.h>
26 
27 /*
28  * timezone --
29  *	The arguments are the number of minutes of time you are westward
30  *	from Greenwich and whether DST is in effect.  It returns a string
31  *	giving the name of the local timezone.  Should be replaced, in the
32  *	application code, by a call to localtime.
33  */
34 
35 static char	czone[TZ_MAX_CHARS];		/* space for zone name */
36 
37 char *
38 timezone(zone, dst)
39 	int	zone,
40 		dst;
41 {
42 	register char	*beg,
43 			*end;
44 	char	*getenv(), *index(), *strncpy(), *_tztab();
45 
46 	if (beg = getenv("TZNAME")) {		/* set in environment */
47 		if (end = index(beg, ',')) {	/* "PST,PDT" */
48 			if (dst)
49 				return(++end);
50 			*end = '\0';
51 			(void)strncpy(czone,beg,sizeof(czone) - 1);
52 			czone[sizeof(czone) - 1] = '\0';
53 			*end = ',';
54 			return(czone);
55 		}
56 		return(beg);
57 	}
58 	return(_tztab(zone,dst));	/* default: table or created zone */
59 }
60 
61 static struct zone {
62 	int	offset;
63 	char	*stdzone;
64 	char	*dlzone;
65 } zonetab[] = {
66 	-1*60,	"MET",	"MET DST",	/* Middle European */
67 	-2*60,	"EET",	"EET DST",	/* Eastern European */
68 	4*60,	"AST",	"ADT",		/* Atlantic */
69 	5*60,	"EST",	"EDT",		/* Eastern */
70 	6*60,	"CST",	"CDT",		/* Central */
71 	7*60,	"MST",	"MDT",		/* Mountain */
72 	8*60,	"PST",	"PDT",		/* Pacific */
73 #ifdef notdef
74 	/* there's no way to distinguish this from WET */
75 	0,	"GMT",	0,		/* Greenwich */
76 #endif
77 	0*60,	"WET",	"WET DST",	/* Western European */
78 	-10*60,	"EST",	"EST",		/* Aust: Eastern */
79      -10*60+30,	"CST",	"CST",		/* Aust: Central */
80 	-8*60,	"WST",	0,		/* Aust: Western */
81 	-1
82 };
83 
84 /*
85  * _tztab --
86  *	check static tables or create a new zone name; broken out so that
87  *	we can make a guess as to what the zone is if the standard tables
88  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
89  *	STANDARD LIBRARY.
90  */
91 char *
92 _tztab(zone,dst)
93 	register int	zone;
94 	int	dst;
95 {
96 	register struct zone	*zp;
97 	register char	sign;
98 
99 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
100 		if (zp->offset == zone) {
101 			if (dst && zp->dlzone)
102 				return(zp->dlzone);
103 			if (!dst && zp->stdzone)
104 				return(zp->stdzone);
105 		}
106 
107 	if (zone < 0) {					/* create one */
108 		zone = -zone;
109 		sign = '+';
110 	}
111 	else
112 		sign = '-';
113 	(void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
114 	return(czone);
115 }
116