Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 535 lines (480 sloc) 14.247 kb
6daf83c Paul Eggert Have tzselect.ksh use Bash by default; this can be overridden.
authored
1 #!/bin/bash
473db36 SCCS ID
Arthur David Olson authored
2
89a6008 Paul Eggert Output version info in a more-typical format.
authored
3 PKGVERSION='(tzcode) '
3323225 Paul Eggert Use a single version number for both code and data.
authored
4 TZVERSION=see_Makefile
a435f9f Paul Eggert Make bug-reporting address more configurable.
authored
5 REPORT_BUGS_TO=tz@iana.org
473db36 SCCS ID
Arthur David Olson authored
6
c6ac6fc created
Arthur David Olson authored
7 # Ask the user about the time zone, and output the resulting TZ value to stdout.
8 # Interact with the user via stderr and stdin.
9
ab5efb7 user@machine eliminated
Arthur David Olson authored
10 # Contributed by Paul Eggert.
c6ac6fc created
Arthur David Olson authored
11
12 # Porting notes:
13 #
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
14 # This script requires a Posix-like shell and prefers the extension of a
6daf83c Paul Eggert Have tzselect.ksh use Bash by default; this can be overridden.
authored
15 # 'select' statement. The 'select' statement was introduced in the
16 # Korn shell and is available in Bash and other shell implementations.
17 # If your host lacks both Bash and the Korn shell, you can get their
18 # source from one of these locations:
c6ac6fc created
Arthur David Olson authored
19 #
6daf83c Paul Eggert Have tzselect.ksh use Bash by default; this can be overridden.
authored
20 # Bash <http://www.gnu.org/software/bash/bash.html>
21 # Korn Shell <http://www.kornshell.com/>
22 # Public Domain Korn Shell <http://www.cs.mun.ca/~michael/pdksh/>
c6ac6fc created
Arthur David Olson authored
23 #
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
24 # For portability to Solaris 9 /bin/sh this script avoids some POSIX
25 # features and common extensions, such as $(...) (which works sometimes
26 # but not others), $((...)), and $10.
27 #
c6ac6fc created
Arthur David Olson authored
28 # This script also uses several features of modern awk programs.
6daf83c Paul Eggert Have tzselect.ksh use Bash by default; this can be overridden.
authored
29 # If your host lacks awk, or has an old awk that does not conform to Posix,
c6ac6fc created
Arthur David Olson authored
30 # you can use either of the following free programs instead:
31 #
6daf83c Paul Eggert Have tzselect.ksh use Bash by default; this can be overridden.
authored
32 # Gawk (GNU awk) <http://www.gnu.org/software/gawk/>
33 # mawk <http://invisible-island.net/mawk/>
c6ac6fc created
Arthur David Olson authored
34
35
36 # Specify default values for environment variables if they are unset.
37 : ${AWK=awk}
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
38 : ${TZDIR=`pwd`}
c6ac6fc created
Arthur David Olson authored
39
40 # Check for awk Posix compliance.
41 ($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1
42 [ $? = 123 ] || {
9a8aa79 Paul Eggert Don't abuse "`" to mean open quote.
authored
43 echo >&2 "$0: Sorry, your '$AWK' program is not Posix compatible."
c6ac6fc created
Arthur David Olson authored
44 exit 1
45 }
46
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
47 coord=
48 location_limit=10
4c73ed6 Paul Eggert Rename time.tab to zone1970.tab.
authored
49 zonetabtype=zone1970
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
50
2e053b5 Paul Eggert Remove -t ZONETABTYPE option from 'tzselect'.
authored
51 usage="Usage: tzselect [--version] [--help] [-c COORD] [-n LIMIT]
a2563ce --help and --version work
Arthur David Olson authored
52 Select a time zone interactively.
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
53
54 Options:
55
56 -c COORD
57 Instead of asking for continent and then country and then city,
58 ask for selection from time zones whose largest cities
59 are closest to the location with geographical coordinates COORD.
60 COORD should use ISO 6709 notation, for example, '-c +4852+00220'
c20dd19 Paul Eggert * tzselect.ksh (usage): Improve -c description.
authored
61 for Paris (in degrees and minutes, North and East), or
62 '-c -35-058' for Buenos Aires (in degrees, South and West).
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
63
64 -n LIMIT
65 Display at most LIMIT locations when -c is used (default $location_limit).
66
67 --version
68 Output version information.
69
70 --help
71 Output this help.
a2563ce --help and --version work
Arthur David Olson authored
72
cc3f938 Paul Eggert Add -t ZONETABTYPE option to 'tzselect'.
authored
73 Report bugs to $REPORT_BUGS_TO."
74
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
75 # Ask the user to select from the function's arguments,
76 # and assign the selected argument to the variable 'select_result'.
77 # Exit on EOF or I/O error. Use the shell's 'select' builtin if available,
78 # falling back on a less-nice but portable substitute otherwise.
79 if
80 case $BASH_VERSION in
81 ?*) : ;;
82 '')
83 # '; exit' should be redundant, but Dash doesn't properly fail without it.
b7816e7 Paul Eggert * tzselect.ksh, NEWS: Port to non-Bash shell that has 'select'.
authored
84 (eval 'set --; select x; do break; done; exit') </dev/null 2>/dev/null
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
85 esac
86 then
87 # Do this inside 'eval', as otherwise the shell might exit when parsing it
88 # even though it is never executed.
89 eval '
90 doselect() {
91 select select_result
92 do
93 case $select_result in
94 "") echo >&2 "Please enter a number in range." ;;
95 ?*) break
96 esac
97 done || exit
98 }
99
100 # Work around a bug in bash 1.14.7 and earlier, where $PS3 is sent to stdout.
101 case $BASH_VERSION in
102 [01].*)
103 case `echo 1 | (select x in x; do break; done) 2>/dev/null` in
104 ?*) PS3=
105 esac
106 esac
107 '
108 else
109 doselect() {
110 # Field width of the prompt numbers.
111 select_width=`expr $# : '.*'`
112
113 select_i=
114
115 while :
116 do
117 case $select_i in
118 '')
119 select_i=0
120 for select_word
121 do
122 select_i=`expr $select_i + 1`
0f9614b Paul Eggert * tzselect.ksh (doselect): Prompt to stderr, not stdout.
authored
123 printf >&2 "%${select_width}d) %s\\n" $select_i "$select_word"
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
124 done ;;
125 *[!0-9]*)
126 echo >&2 'Please enter a number in range.' ;;
127 *)
128 if test 1 -le $select_i && test $select_i -le $#; then
129 shift `expr $select_i - 1`
130 select_result=$1
131 break
132 fi
133 echo >&2 'Please enter a number in range.'
134 esac
135
136 # Prompt and read input.
0f9614b Paul Eggert * tzselect.ksh (doselect): Prompt to stderr, not stdout.
authored
137 printf >&2 %s "${PS3-#? }"
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
138 read select_i || exit
139 done
140 }
141 fi
142
67d9b6a Paul Eggert * tzselect.ksh: Fix typo in implementation of undocumented -t option.
authored
143 while getopts c:n:t:-: opt
cc3f938 Paul Eggert Add -t ZONETABTYPE option to 'tzselect'.
authored
144 do
145 case $opt$OPTARG in
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
146 c*)
147 coord=$OPTARG ;;
148 n*)
149 location_limit=$OPTARG ;;
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
150 t*) # Undocumented option, used for developer testing.
151 zonetabtype=$OPTARG ;;
cc3f938 Paul Eggert Add -t ZONETABTYPE option to 'tzselect'.
authored
152 -help)
153 exec echo "$usage" ;;
154 -version)
155 exec echo "tzselect $PKGVERSION$TZVERSION" ;;
156 -*)
157 echo >&2 "$0: -$opt$OPTARG: unknown option; try '$0 --help'"; exit 1 ;;
158 *)
159 echo >&2 "$0: try '$0 --help'"; exit 1 ;;
160 esac
161 done
162
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
163 shift `expr $OPTIND - 1`
cc3f938 Paul Eggert Add -t ZONETABTYPE option to 'tzselect'.
authored
164 case $# in
165 0) ;;
166 *) echo >&2 "$0: $1: unknown argument"; exit 1 ;;
167 esac
a2563ce --help and --version work
Arthur David Olson authored
168
c6ac6fc created
Arthur David Olson authored
169 # Make sure the tables are readable.
170 TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
171 TZ_ZONE_TABLE=$TZDIR/$zonetabtype.tab
c6ac6fc created
Arthur David Olson authored
172 for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
173 do
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
174 <"$f" || {
c6ac6fc created
Arthur David Olson authored
175 echo >&2 "$0: time zone files are not set up correctly"
176 exit 1
177 }
178 done
179
180 newline='
181 '
182 IFS=$newline
183
184
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
185 # Awk script to read a time zone table and output the same table,
186 # with each column preceded by its distance from 'here'.
187 output_distances='
188 BEGIN {
189 FS = "\t"
190 while (getline <TZ_COUNTRY_TABLE)
191 if ($0 ~ /^[^#]/)
192 country[$1] = $2
193 country["US"] = "US" # Otherwise the strings get too long.
194 }
6271179 Paul Eggert tzselect: improve heuristic in Africa
authored
195 function abs(x) {
196 return x < 0 ? -x : x;
197 }
198 function min(x, y) {
199 return x < y ? x : y;
200 }
201 function convert_coord(coord, deg, minute, ilen, sign, sec) {
e43fc49 Paul Eggert tzselect: -c COORD now supports more ISO 6709 forms
authored
202 if (coord ~ /^[-+]?[0-9]?[0-9][0-9][0-9][0-9][0-9][0-9]([^0-9]|$)/) {
203 degminsec = coord
204 intdeg = degminsec < 0 ? -int(-degminsec / 10000) : int(degminsec / 10000)
205 minsec = degminsec - intdeg * 10000
206 intmin = minsec < 0 ? -int(-minsec / 100) : int(minsec / 100)
207 sec = minsec - intmin * 100
208 deg = (intdeg * 3600 + intmin * 60 + sec) / 3600
209 } else if (coord ~ /^[-+]?[0-9]?[0-9][0-9][0-9][0-9]([^0-9]|$)/) {
210 degmin = coord
211 intdeg = degmin < 0 ? -int(-degmin / 100) : int(degmin / 100)
6271179 Paul Eggert tzselect: improve heuristic in Africa
authored
212 minute = degmin - intdeg * 100
213 deg = (intdeg * 60 + minute) / 60
e43fc49 Paul Eggert tzselect: -c COORD now supports more ISO 6709 forms
authored
214 } else
215 deg = coord
216 return deg * 0.017453292519943296
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
217 }
218 function convert_latitude(coord) {
219 match(coord, /..*[-+]/)
e43fc49 Paul Eggert tzselect: -c COORD now supports more ISO 6709 forms
authored
220 return convert_coord(substr(coord, 1, RLENGTH - 1))
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
221 }
222 function convert_longitude(coord) {
223 match(coord, /..*[-+]/)
e43fc49 Paul Eggert tzselect: -c COORD now supports more ISO 6709 forms
authored
224 return convert_coord(substr(coord, RLENGTH))
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
225 }
226 # Great-circle distance between points with given latitude and longitude.
227 # Inputs and output are in radians. This uses the great-circle special
228 # case of the Vicenty formula for distances on ellipsoids.
6271179 Paul Eggert tzselect: improve heuristic in Africa
authored
229 function gcdist(lat1, long1, lat2, long2, dlong, x, y, num, denom) {
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
230 dlong = long2 - long1
27bd92a Stefan Kuhn tzselect: Removed spaces in calls to awk-functions
Wuodan authored
231 x = cos(lat2) * sin(dlong)
232 y = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlong)
233 num = sqrt(x * x + y * y)
234 denom = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlong)
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
235 return atan2(num, denom)
236 }
6271179 Paul Eggert tzselect: improve heuristic in Africa
authored
237 # Parallel distance between points with given latitude and longitude.
238 # This is the product of the longitude difference and the cosine
239 # of the latitude of the point that is further from the equator.
240 # I.e., it considers longitudes to be further apart if they are
241 # nearer the equator.
242 function pardist(lat1, long1, lat2, long2) {
27bd92a Stefan Kuhn tzselect: Removed spaces in calls to awk-functions
Wuodan authored
243 return abs(long1 - long2) * min(cos(lat1), cos(lat2))
6271179 Paul Eggert tzselect: improve heuristic in Africa
authored
244 }
245 # The distance function is the sum of the great-circle distance and
246 # the parallel distance. It could be weighted.
247 function dist(lat1, long1, lat2, long2) {
27bd92a Stefan Kuhn tzselect: Removed spaces in calls to awk-functions
Wuodan authored
248 return gcdist(lat1, long1, lat2, long2) + pardist(lat1, long1, lat2, long2)
6271179 Paul Eggert tzselect: improve heuristic in Africa
authored
249 }
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
250 BEGIN {
251 coord_lat = convert_latitude(coord)
252 coord_long = convert_longitude(coord)
253 }
254 /^[^#]/ {
255 here_lat = convert_latitude($2)
256 here_long = convert_longitude($2)
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
257 line = $1 "\t" $2 "\t" $3
258 sep = "\t"
259 ncc = split($1, cc, /,/)
260 for (i = 1; i <= ncc; i++) {
261 line = line sep country[cc[i]]
262 sep = ", "
263 }
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
264 if (NF == 4)
265 line = line " - " $4
266 printf "%g\t%s\n", dist(coord_lat, coord_long, here_lat, here_long), line
267 }
268 '
c6ac6fc created
Arthur David Olson authored
269
270 # Begin the main loop. We come back here if the user wants to retry.
271 while
272
273 echo >&2 'Please identify a location' \
274 'so that time zone rules can be set correctly.'
275
276 continent=
277 country=
278 region=
279
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
280 case $coord in
281 ?*)
282 continent=coord;;
283 '')
c6ac6fc created
Arthur David Olson authored
284
285 # Ask the user for continent or ocean.
286
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
287 echo >&2 'Please select a continent, ocean, "coord", or "TZ".'
c6ac6fc created
Arthur David Olson authored
288
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
289 quoted_continents=`
868ed00 Paul Eggert tzselect: work around an old BusyBox awk bug
authored
290 $AWK '
291 BEGIN { FS = "\t" }
b383bf6 Paul Eggert * tzselect.ksh: Compute list of continents from the time zone table,
authored
292 /^[^#]/ {
293 entry = substr($3, 1, index($3, "/") - 1)
294 if (entry == "America")
295 entry = entry "s"
296 if (entry ~ /^(Arctic|Atlantic|Indian|Pacific)$/)
297 entry = entry " Ocean"
298 printf "'\''%s'\''\n", entry
299 }
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
300 ' <"$TZ_ZONE_TABLE" |
b383bf6 Paul Eggert * tzselect.ksh: Compute list of continents from the time zone table,
authored
301 sort -u |
302 tr '\n' ' '
303 echo ''
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
304 `
b383bf6 Paul Eggert * tzselect.ksh: Compute list of continents from the time zone table,
authored
305
306 eval '
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
307 doselect '"$quoted_continents"' \
b383bf6 Paul Eggert * tzselect.ksh: Compute list of continents from the time zone table,
authored
308 "coord - I want to use geographical coordinates." \
309 "TZ - I want to specify the time zone using the Posix TZ format."
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
310 continent=$select_result
311 case $continent in
312 Americas) continent=America;;
313 *" "*) continent=`expr "$continent" : '\''\([^ ]*\)'\''`
314 esac
b383bf6 Paul Eggert * tzselect.ksh: Compute list of continents from the time zone table,
authored
315 '
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
316 esac
317
c6ac6fc created
Arthur David Olson authored
318 case $continent in
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
319 TZ)
c6ac6fc created
Arthur David Olson authored
320 # Ask the user for a Posix TZ string. Check that it conforms.
321 while
322 echo >&2 'Please enter the desired value' \
323 'of the TZ environment variable.'
324 echo >&2 'For example, GST-10 is a zone named GST' \
325 'that is 10 hours ahead (east) of UTC.'
326 read TZ
327 $AWK -v TZ="$TZ" 'BEGIN {
328 tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+"
329 time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?"
330 offset = "[-+]?" time
66cda43 Stefan Kuhn tzselect: Fix for gawk treating '\.' as plain '.'
Wuodan authored
331 date = "(J?[0-9]+|M[0-9]+\\.[0-9]+\\.[0-9]+)"
c6ac6fc created
Arthur David Olson authored
332 datetime = "," date "(/" time ")?"
333 tzpattern = "^(:.*|" tzname offset "(" tzname \
334 "(" offset ")?(" datetime datetime ")?)?)$"
335 if (TZ ~ tzpattern) exit 1
336 exit 0
337 }'
338 do
9a8aa79 Paul Eggert Don't abuse "`" to mean open quote.
authored
339 echo >&2 "'$TZ' is not a conforming" \
c6ac6fc created
Arthur David Olson authored
340 'Posix time zone string.'
341 done
342 TZ_for_date=$TZ;;
343 *)
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
344 case $continent in
345 coord)
346 case $coord in
347 '')
348 echo >&2 'Please enter coordinates' \
349 'in ISO 6709 notation.'
350 echo >&2 'For example, +4042-07403 stands for'
351 echo >&2 '40 degrees 42 minutes north,' \
352 '74 degrees 3 minutes west.'
353 read coord;;
354 esac
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
355 distance_table=`$AWK \
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
356 -v coord="$coord" \
357 -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
358 "$output_distances" <"$TZ_ZONE_TABLE" |
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
359 sort -n |
360 sed "${location_limit}q"
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
361 `
362 regions=`echo "$distance_table" | $AWK '
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
363 BEGIN { FS = "\t" }
364 { print $NF }
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
365 '`
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
366 echo >&2 'Please select one of the following' \
367 'time zone regions,'
368 echo >&2 'listed roughly in increasing order' \
369 "of distance from $coord".
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
370 doselect $regions
371 region=$select_result
372 TZ=`echo "$distance_table" | $AWK -v region="$region" '
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
373 BEGIN { FS="\t" }
374 $NF == region { print $4 }
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
375 '`
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
376 ;;
377 *)
c6ac6fc created
Arthur David Olson authored
378 # Get list of names of countries in the continent or ocean.
868ed00 Paul Eggert tzselect: work around an old BusyBox awk bug
authored
379 countries=`$AWK \
c6ac6fc created
Arthur David Olson authored
380 -v continent="$continent" \
381 -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
382 '
868ed00 Paul Eggert tzselect: work around an old BusyBox awk bug
authored
383 BEGIN { FS = "\t" }
c6ac6fc created
Arthur David Olson authored
384 /^#/ { next }
385 $3 ~ ("^" continent "/") {
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
386 ncc = split($1, cc, /,/)
387 for (i = 1; i <= ncc; i++)
388 if (!cc_seen[cc[i]]++) cc_list[++ccs] = cc[i]
c6ac6fc created
Arthur David Olson authored
389 }
390 END {
391 while (getline <TZ_COUNTRY_TABLE) {
392 if ($0 !~ /^#/) cc_name[$1] = $2
393 }
394 for (i = 1; i <= ccs; i++) {
395 country = cc_list[i]
396 if (cc_name[country]) {
397 country = cc_name[country]
398 }
399 print country
400 }
401 }
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
402 ' <"$TZ_ZONE_TABLE" | sort -f`
c6ac6fc created
Arthur David Olson authored
403
404
405 # If there's more than one country, ask the user which one.
406 case $countries in
407 *"$newline"*)
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
408 echo >&2 'Please select a country' \
409 'whose clocks agree with yours.'
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
410 doselect $countries
411 country=$select_result;;
c6ac6fc created
Arthur David Olson authored
412 *)
413 country=$countries
414 esac
415
416
417 # Get list of names of time zone rule regions in the country.
868ed00 Paul Eggert tzselect: work around an old BusyBox awk bug
authored
418 regions=`$AWK \
c6ac6fc created
Arthur David Olson authored
419 -v country="$country" \
420 -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
421 '
422 BEGIN {
868ed00 Paul Eggert tzselect: work around an old BusyBox awk bug
authored
423 FS = "\t"
c6ac6fc created
Arthur David Olson authored
424 cc = country
425 while (getline <TZ_COUNTRY_TABLE) {
426 if ($0 !~ /^#/ && country == $2) {
427 cc = $1
428 break
429 }
430 }
431 }
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
432 $1 ~ cc { print $4 }
433 ' <"$TZ_ZONE_TABLE"`
c6ac6fc created
Arthur David Olson authored
434
435
436 # If there's more than one region, ask the user which one.
437 case $regions in
438 *"$newline"*)
439 echo >&2 'Please select one of the following' \
440 'time zone regions.'
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
441 doselect $regions
442 region=$select_result;;
c6ac6fc created
Arthur David Olson authored
443 *)
444 region=$regions
445 esac
446
447 # Determine TZ from country and region.
868ed00 Paul Eggert tzselect: work around an old BusyBox awk bug
authored
448 TZ=`$AWK \
c6ac6fc created
Arthur David Olson authored
449 -v country="$country" \
450 -v region="$region" \
451 -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
452 '
453 BEGIN {
868ed00 Paul Eggert tzselect: work around an old BusyBox awk bug
authored
454 FS = "\t"
c6ac6fc created
Arthur David Olson authored
455 cc = country
456 while (getline <TZ_COUNTRY_TABLE) {
457 if ($0 !~ /^#/ && country == $2) {
458 cc = $1
459 break
460 }
461 }
462 }
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
463 $1 ~ cc && $4 == region { print $3 }
464 ' <"$TZ_ZONE_TABLE"`
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
465 esac
c6ac6fc created
Arthur David Olson authored
466
467 # Make sure the corresponding zoneinfo file exists.
468 TZ_for_date=$TZDIR/$TZ
5ddc47f Paul Eggert New file time.tab, superseding zone.tab
authored
469 <"$TZ_for_date" || {
c6ac6fc created
Arthur David Olson authored
470 echo >&2 "$0: time zone files are not set up correctly"
471 exit 1
472 }
473 esac
474
475
476 # Use the proposed TZ to output the current date relative to UTC.
477 # Loop until they agree in seconds.
478 # Give up after 8 unsuccessful tries.
479
480 extra_info=
481 for i in 1 2 3 4 5 6 7 8
482 do
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
483 TZdate=`LANG=C TZ="$TZ_for_date" date`
484 UTdate=`LANG=C TZ=UTC0 date`
485 TZsec=`expr "$TZdate" : '.*:\([0-5][0-9]\)'`
486 UTsec=`expr "$UTdate" : '.*:\([0-5][0-9]\)'`
c6ac6fc created
Arthur David Olson authored
487 case $TZsec in
488 $UTsec)
489 extra_info="
490 Local time is now: $TZdate.
491 Universal Time is now: $UTdate."
492 break
493 esac
494 done
495
496
497 # Output TZ info and ask the user to confirm.
498
499 echo >&2 ""
500 echo >&2 "The following information has been given:"
501 echo >&2 ""
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
502 case $country%$region%$coord in
503 ?*%?*%) echo >&2 " $country$newline $region";;
504 ?*%%) echo >&2 " $country";;
505 %?*%?*) echo >&2 " coord $coord$newline $region";;
506 %%?*) echo >&2 " coord $coord";;
276f85d Stefan Kuhn tzselect: Display Posix TZ format when used
Wuodan authored
507 *) echo >&2 " TZ='$TZ'"
c6ac6fc created
Arthur David Olson authored
508 esac
509 echo >&2 ""
510 echo >&2 "Therefore TZ='$TZ' will be used.$extra_info"
511 echo >&2 "Is the above information OK?"
512
be06aa4 Paul Eggert tzselect: port to /bin/sh
authored
513 doselect Yes No
514 ok=$select_result
c6ac6fc created
Arthur David Olson authored
515 case $ok in
516 Yes) break
517 esac
ebdf0c5 Paul Eggert tzselect: new options -c COORD and -n LIMIT
authored
518 do coord=
c6ac6fc created
Arthur David Olson authored
519 done
520
dad1278 Eggert mods
Arthur David Olson authored
521 case $SHELL in
522 *csh) file=.login line="setenv TZ '$TZ'";;
523 *) file=.profile line="TZ='$TZ'; export TZ"
524 esac
525
526 echo >&2 "
527 You can make this change permanent for yourself by appending the line
528 $line
529 to the file '$file' in your home directory; then log out and log in again.
530
531 Here is that TZ value again, this time on standard output so that you
532 can use the $0 command in shell scripts:"
533
c6ac6fc created
Arthur David Olson authored
534 echo "$TZ"
Something went wrong with that request. Please try again.