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