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