4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / lib / libc / gen / timezone.c
CommitLineData
bb0cfa24 1/*
74155b62
KB
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
063836de 4 *
269a7923 5 * %sccs.include.redist.c%
bb0cfa24
DF
6 */
7
03f05ee5 8#if defined(LIBC_SCCS) && !defined(lint)
74155b62 9static char sccsid[] = "@(#)timezone.c 8.1 (Berkeley) %G%";
063836de 10#endif /* LIBC_SCCS and not lint */
5d10bbb4
KB
11
12#include <sys/types.h>
13#include <sys/time.h>
14#include <stdio.h>
c5980113
DS
15#include <stdlib.h>
16#include <string.h>
5d10bbb4 17#include <tzfile.h>
bb0cfa24 18
c5980113
DS
19char *_tztab();
20
9d342dd9 21/*
5d10bbb4
KB
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.
9d342dd9
BJ
27 */
28
5d10bbb4
KB
29static char czone[TZ_MAX_CHARS]; /* space for zone name */
30
31char *
32timezone(zone, dst)
33 int zone,
34 dst;
35{
36 register char *beg,
37 *end;
5d10bbb4
KB
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 }
db546bc5 51 return(_tztab(zone,dst)); /* default: table or created zone */
5d10bbb4
KB
52}
53
9d342dd9
BJ
54static struct zone {
55 int offset;
56 char *stdzone;
57 char *dlzone;
58} zonetab[] = {
5d10bbb4
KB
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 */
7f253f66
SL
66#ifdef notdef
67 /* there's no way to distinguish this from WET */
5d10bbb4 68 0, "GMT", 0, /* Greenwich */
7f253f66 69#endif
5d10bbb4
KB
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 */
9d342dd9
BJ
74 -1
75};
76
5d10bbb4 77/*
db546bc5 78 * _tztab --
5d10bbb4
KB
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 */
84char *
2555d7f3 85_tztab(zone,dst)
5d10bbb4
KB
86 register int zone;
87 int dst;
9d342dd9 88{
5d10bbb4
KB
89 register struct zone *zp;
90 register char sign;
9d342dd9 91
5d10bbb4
KB
92 for (zp = zonetab; zp->offset != -1;++zp) /* static tables */
93 if (zp->offset == zone) {
9d342dd9
BJ
94 if (dst && zp->dlzone)
95 return(zp->dlzone);
96 if (!dst && zp->stdzone)
97 return(zp->stdzone);
98 }
5d10bbb4
KB
99
100 if (zone < 0) { /* create one */
9d342dd9 101 zone = -zone;
5d10bbb4
KB
102 sign = '+';
103 }
104 else
105 sign = '-';
106 (void)sprintf(czone,"GMT%c%d:%02d",sign,zone / 60,zone % 60);
9d342dd9
BJ
107 return(czone);
108}