tzselect.ksh revision 1.2 1 1.1 jtc #! /bin/ksh
2 1.2 perry #
3 1.2 perry # $NetBSD: tzselect.ksh,v 1.2 1998/01/09 04:11:58 perry Exp $
4 1.2 perry #
5 1.1 jtc # Ask the user about the time zone, and output the resulting TZ value to stdout.
6 1.1 jtc # Interact with the user via stderr and stdin.
7 1.1 jtc
8 1.1 jtc # Contributed by Paul Eggert <eggert (at] twinsun.com>.
9 1.1 jtc
10 1.1 jtc # Porting notes:
11 1.1 jtc #
12 1.1 jtc # This script requires several features of the Korn shell.
13 1.1 jtc # If your host lacks the Korn shell,
14 1.1 jtc # you can use either of the following free programs instead:
15 1.1 jtc #
16 1.1 jtc # Bourne-Again shell (bash)
17 1.2 perry # <URL:ftp://ftp.gnu.ai.mit.edu/pub/gnu/>
18 1.1 jtc #
19 1.1 jtc # Public domain ksh
20 1.2 perry # <URL:ftp://ftp.cs.mun.ca/pub/pdksh/pdksh.tar.gz>
21 1.1 jtc #
22 1.1 jtc # This script also uses several features of modern awk programs.
23 1.1 jtc # If your host lacks awk, or has an old awk that does not conform to Posix.2,
24 1.1 jtc # you can use either of the following free programs instead:
25 1.1 jtc #
26 1.1 jtc # GNU awk (gawk)
27 1.2 perry # <URL:ftp://ftp.gnu.ai.mit.edu/pub/gnu/>
28 1.1 jtc #
29 1.1 jtc # mawk
30 1.2 perry # <URL:ftp://ftp.whidbey.net/pub/brennan/>
31 1.1 jtc
32 1.1 jtc
33 1.1 jtc # Specify default values for environment variables if they are unset.
34 1.1 jtc : ${AWK=awk}
35 1.1 jtc : ${TZDIR=$(pwd)}
36 1.1 jtc
37 1.1 jtc # Check for awk Posix compliance.
38 1.1 jtc ($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1
39 1.1 jtc [ $? = 123 ] || {
40 1.1 jtc echo >&2 "$0: Sorry, your \`$AWK' program is not Posix compatible."
41 1.1 jtc exit 1
42 1.1 jtc }
43 1.1 jtc
44 1.1 jtc # Make sure the tables are readable.
45 1.1 jtc TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab
46 1.1 jtc TZ_ZONE_TABLE=$TZDIR/zone.tab
47 1.1 jtc for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
48 1.1 jtc do
49 1.1 jtc <$f || {
50 1.1 jtc echo >&2 "$0: time zone files are not set up correctly"
51 1.1 jtc exit 1
52 1.1 jtc }
53 1.1 jtc done
54 1.1 jtc
55 1.1 jtc newline='
56 1.1 jtc '
57 1.1 jtc IFS=$newline
58 1.1 jtc
59 1.1 jtc
60 1.2 perry # Work around a bug in bash 1.14.7 and earlier, where $PS3 is sent to stdout.
61 1.1 jtc case $(echo 1 | (select x in x; do break; done) 2>/dev/null) in
62 1.1 jtc ?*) PS3=
63 1.1 jtc esac
64 1.1 jtc
65 1.1 jtc
66 1.1 jtc # Begin the main loop. We come back here if the user wants to retry.
67 1.1 jtc while
68 1.1 jtc
69 1.1 jtc echo >&2 'Please identify a location' \
70 1.1 jtc 'so that time zone rules can be set correctly.'
71 1.1 jtc
72 1.1 jtc continent=
73 1.1 jtc country=
74 1.1 jtc region=
75 1.1 jtc
76 1.1 jtc
77 1.1 jtc # Ask the user for continent or ocean.
78 1.1 jtc
79 1.1 jtc echo >&2 'Please select a continent or ocean.'
80 1.1 jtc
81 1.1 jtc select continent in \
82 1.1 jtc Africa \
83 1.1 jtc Americas \
84 1.1 jtc Antarctica \
85 1.1 jtc 'Arctic Ocean' \
86 1.1 jtc Asia \
87 1.1 jtc 'Atlantic Ocean' \
88 1.1 jtc Australia \
89 1.1 jtc Europe \
90 1.1 jtc 'Indian Ocean' \
91 1.1 jtc 'Pacific Ocean' \
92 1.1 jtc 'none - I want to specify the time zone using the Posix TZ format.'
93 1.1 jtc do
94 1.1 jtc case $continent in
95 1.1 jtc '')
96 1.1 jtc echo >&2 'Please enter a number in range.';;
97 1.1 jtc ?*)
98 1.1 jtc case $continent in
99 1.1 jtc Americas) continent=America;;
100 1.1 jtc *' '*) continent=$(expr "$continent" : '\([^ ]*\)')
101 1.1 jtc esac
102 1.1 jtc break
103 1.1 jtc esac
104 1.1 jtc done
105 1.1 jtc case $continent in
106 1.1 jtc '')
107 1.1 jtc exit 1;;
108 1.1 jtc none)
109 1.1 jtc # Ask the user for a Posix TZ string. Check that it conforms.
110 1.1 jtc while
111 1.1 jtc echo >&2 'Please enter the desired value' \
112 1.1 jtc 'of the TZ environment variable.'
113 1.1 jtc echo >&2 'For example, GST-10 is a zone named GST' \
114 1.1 jtc 'that is 10 hours ahead (east) of UTC.'
115 1.1 jtc read TZ
116 1.1 jtc $AWK -v TZ="$TZ" 'BEGIN {
117 1.1 jtc tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+"
118 1.1 jtc time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?"
119 1.1 jtc offset = "[-+]?" time
120 1.1 jtc date = "(J?[0-9]+|M[0-9]+\.[0-9]+\.[0-9]+)"
121 1.1 jtc datetime = "," date "(/" time ")?"
122 1.1 jtc tzpattern = "^(:.*|" tzname offset "(" tzname \
123 1.1 jtc "(" offset ")?(" datetime datetime ")?)?)$"
124 1.1 jtc if (TZ ~ tzpattern) exit 1
125 1.1 jtc exit 0
126 1.1 jtc }'
127 1.1 jtc do
128 1.1 jtc echo >&2 "\`$TZ' is not a conforming" \
129 1.1 jtc 'Posix time zone string.'
130 1.1 jtc done
131 1.1 jtc TZ_for_date=$TZ;;
132 1.1 jtc *)
133 1.1 jtc # Get list of names of countries in the continent or ocean.
134 1.1 jtc countries=$($AWK -F'\t' \
135 1.1 jtc -v continent="$continent" \
136 1.1 jtc -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
137 1.1 jtc '
138 1.1 jtc /^#/ { next }
139 1.1 jtc $3 ~ ("^" continent "/") {
140 1.1 jtc if (!cc_seen[$1]++) cc_list[++ccs] = $1
141 1.1 jtc }
142 1.1 jtc END {
143 1.1 jtc while (getline <TZ_COUNTRY_TABLE) {
144 1.1 jtc if ($0 !~ /^#/) cc_name[$1] = $2
145 1.1 jtc }
146 1.1 jtc for (i = 1; i <= ccs; i++) {
147 1.1 jtc country = cc_list[i]
148 1.1 jtc if (cc_name[country]) {
149 1.1 jtc country = cc_name[country]
150 1.1 jtc }
151 1.1 jtc print country
152 1.1 jtc }
153 1.1 jtc }
154 1.1 jtc ' <$TZ_ZONE_TABLE | sort -f)
155 1.1 jtc
156 1.1 jtc
157 1.1 jtc # If there's more than one country, ask the user which one.
158 1.1 jtc case $countries in
159 1.1 jtc *"$newline"*)
160 1.1 jtc echo >&2 'Please select a country.'
161 1.1 jtc select country in $countries
162 1.1 jtc do
163 1.1 jtc case $country in
164 1.1 jtc '') echo >&2 'Please enter a number in range.';;
165 1.1 jtc ?*) break
166 1.1 jtc esac
167 1.1 jtc done
168 1.1 jtc
169 1.1 jtc case $country in
170 1.1 jtc '') exit 1
171 1.1 jtc esac;;
172 1.1 jtc *)
173 1.1 jtc country=$countries
174 1.1 jtc esac
175 1.1 jtc
176 1.1 jtc
177 1.1 jtc # Get list of names of time zone rule regions in the country.
178 1.1 jtc regions=$($AWK -F'\t' \
179 1.1 jtc -v country="$country" \
180 1.1 jtc -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
181 1.1 jtc '
182 1.1 jtc BEGIN {
183 1.1 jtc cc = country
184 1.1 jtc while (getline <TZ_COUNTRY_TABLE) {
185 1.1 jtc if ($0 !~ /^#/ && country == $2) {
186 1.1 jtc cc = $1
187 1.1 jtc break
188 1.1 jtc }
189 1.1 jtc }
190 1.1 jtc }
191 1.1 jtc $1 == cc { print $4 }
192 1.1 jtc ' <$TZ_ZONE_TABLE)
193 1.1 jtc
194 1.1 jtc
195 1.1 jtc # If there's more than one region, ask the user which one.
196 1.1 jtc case $regions in
197 1.1 jtc *"$newline"*)
198 1.1 jtc echo >&2 'Please select one of the following' \
199 1.1 jtc 'time zone regions.'
200 1.1 jtc select region in $regions
201 1.1 jtc do
202 1.1 jtc case $region in
203 1.1 jtc '') echo >&2 'Please enter a number in range.';;
204 1.1 jtc ?*) break
205 1.1 jtc esac
206 1.1 jtc done
207 1.1 jtc case $region in
208 1.1 jtc '') exit 1
209 1.1 jtc esac;;
210 1.1 jtc *)
211 1.1 jtc region=$regions
212 1.1 jtc esac
213 1.1 jtc
214 1.1 jtc # Determine TZ from country and region.
215 1.1 jtc TZ=$($AWK -F'\t' \
216 1.1 jtc -v country="$country" \
217 1.1 jtc -v region="$region" \
218 1.1 jtc -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
219 1.1 jtc '
220 1.1 jtc BEGIN {
221 1.1 jtc cc = country
222 1.1 jtc while (getline <TZ_COUNTRY_TABLE) {
223 1.1 jtc if ($0 !~ /^#/ && country == $2) {
224 1.1 jtc cc = $1
225 1.1 jtc break
226 1.1 jtc }
227 1.1 jtc }
228 1.1 jtc }
229 1.1 jtc $1 == cc && $4 == region { print $3 }
230 1.1 jtc ' <$TZ_ZONE_TABLE)
231 1.1 jtc
232 1.1 jtc # Make sure the corresponding zoneinfo file exists.
233 1.1 jtc TZ_for_date=$TZDIR/$TZ
234 1.1 jtc <$TZ_for_date || {
235 1.1 jtc echo >&2 "$0: time zone files are not set up correctly"
236 1.1 jtc exit 1
237 1.1 jtc }
238 1.1 jtc esac
239 1.1 jtc
240 1.1 jtc
241 1.1 jtc # Use the proposed TZ to output the current date relative to UTC.
242 1.1 jtc # Loop until they agree in seconds.
243 1.1 jtc # Give up after 8 unsuccessful tries.
244 1.1 jtc
245 1.1 jtc extra_info=
246 1.1 jtc for i in 1 2 3 4 5 6 7 8
247 1.1 jtc do
248 1.1 jtc TZdate=$(LANG=C TZ="$TZ_for_date" date)
249 1.1 jtc UTdate=$(LANG=C TZ=UTC0 date)
250 1.1 jtc TZsec=$(expr "$TZdate" : '.*:\([0-5][0-9]\)')
251 1.1 jtc UTsec=$(expr "$UTdate" : '.*:\([0-5][0-9]\)')
252 1.1 jtc case $TZsec in
253 1.1 jtc $UTsec)
254 1.1 jtc extra_info="
255 1.1 jtc Local time is now: $TZdate.
256 1.1 jtc Universal Time is now: $UTdate."
257 1.1 jtc break
258 1.1 jtc esac
259 1.1 jtc done
260 1.1 jtc
261 1.1 jtc
262 1.1 jtc # Output TZ info and ask the user to confirm.
263 1.1 jtc
264 1.1 jtc echo >&2 ""
265 1.1 jtc echo >&2 "The following information has been given:"
266 1.1 jtc echo >&2 ""
267 1.1 jtc case $country+$region in
268 1.1 jtc ?*+?*) echo >&2 " $country$newline $region";;
269 1.1 jtc ?*+) echo >&2 " $country";;
270 1.1 jtc +) echo >&2 " TZ='$TZ'"
271 1.1 jtc esac
272 1.1 jtc echo >&2 ""
273 1.1 jtc echo >&2 "Therefore TZ='$TZ' will be used.$extra_info"
274 1.1 jtc echo >&2 "Is the above information OK?"
275 1.1 jtc
276 1.1 jtc ok=
277 1.1 jtc select ok in Yes No
278 1.1 jtc do
279 1.1 jtc case $ok in
280 1.1 jtc '') echo >&2 'Please enter 1 for Yes, or 2 for No.';;
281 1.1 jtc ?*) break
282 1.1 jtc esac
283 1.1 jtc done
284 1.1 jtc case $ok in
285 1.1 jtc '') exit 1;;
286 1.1 jtc Yes) break
287 1.1 jtc esac
288 1.1 jtc do :
289 1.1 jtc done
290 1.1 jtc
291 1.1 jtc # Output the answer.
292 1.1 jtc echo "$TZ"
293