1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
#! /usr/bin/ksh
#
# Copyright 2005 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# ident "%Z%%M% %I% %E% SMI"
#
# '@(#)tzselect.ksh 1.8'
# Ask the user about the time zone, and output the resulting TZ value to stdout.
# Interact with the user via stderr and stdin.
# Contributed by Paul Eggert
# Porting notes:
#
# This script requires several features of the Korn shell.
# If your host lacks the Korn shell,
# you can use either of the following free programs instead:
#
# <a href=ftp://ftp.gnu.org/pub/gnu/>
# Bourne-Again shell (bash)
# </a>
#
# <a href=ftp://ftp.cs.mun.ca/pub/pdksh/pdksh.tar.gz>
# Public domain ksh
# </a>
#
# This script also uses several features of modern awk programs.
# If your host lacks awk, or has an old awk that does not conform to POSIX.2,
# you can use either of the following free programs instead:
#
# <a href=ftp://ftp.gnu.org/pub/gnu/>
# GNU awk (gawk)
# </a>
#
# <a href=ftp://ftp.whidbey.net/pub/brennan/>
# mawk
# </a>
AWK=/usr/bin/nawk
GREP=/usr/bin/grep
EXPR=/usr/bin/expr
LOCALE=/usr/bin/locale
SORT=/usr/bin/sort
PRINTF=/usr/bin/printf
DATE=/usr/bin/date
GETTEXT=/usr/bin/gettext
TZDIR=/usr/share/lib/zoneinfo
# Messages
ERR_NO_SETUP="%s: time zone files are not set up correctly"
INFO_LOCATION="Please identify a location so that time zone rules \
can be set correctly."
INFO_SELECT_CONT="Please select a continent or ocean."
INFO_POSIX="none - I want to specify the time zone using the POSIX \
TZ format."
WARN_ENTER_NUM="Please enter a number in range."
INFO_ENTER_POSIX="Please enter the desired value of the TZ environment \
variable."
INFO_POSIX_EX="For example, GST-10 is a zone named GST that is 10 hours \
ahead (east) of UTC."
ERR_INV_POSIX="\`%s\' is not a conforming POSIX time zone string."
INFO_SELECT_CNTRY="Please select a country or region."
INFO_SELECT_TZ="Please select one of the following time zone regions."
INFO_EXTRA1="Local time is now: %s"
INFO_EXTRA2="Universal Time is now: %s"
INFO_INFO="The following information has been given:"
INFO_TZ="Therefore TZ='%s' will be used."
INFO_OK="Is the above information OK?"
INFO_YES="Yes"
INFO_NO="No"
WARN_ENTER_YORN="Please enter 1 for Yes, or 2 for No."
INFO_FINE="Here is the TZ value again, this time on standard output:"
# I18n support
TEXTDOMAINDIR=/usr/lib/locale; export TEXTDOMAINDIR
TEXTDOMAIN=SUNW_OST_OSCMD; export TEXTDOMAIN
DOMAIN2=SUNW_OST_ZONEINFO
# Make sure the tables are readable.
TZ_COUNTRY_TABLE=$TZDIR/tab/country.tab
TZ_ZONE_TABLE=$TZDIR/tab/zone_sun.tab
for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
do
<$f || {
$PRINTF >&2 "`$GETTEXT "$ERR_NO_SETUP"`\n" $0
exit 1
}
done
newline='
'
IFS=$newline
# For C locale, don't need to call gettext(1)
loc_messages=`$LOCALE | $GREP LC_MESSAGES | $AWK -F"=" '{print $2}`
if [ "$loc_messages" = "\"C\"" -o "$loc_messages" = "C" ]; then
is_C=1
else
is_C=0
fi
iafrica=`$GETTEXT $DOMAIN2 Africa`
iamerica=`$GETTEXT $DOMAIN2 Americas`
iantarctica=`$GETTEXT $DOMAIN2 Antarctica`
iarcticocean=`$GETTEXT $DOMAIN2 "Arctic Ocean"`
iasia=`$GETTEXT $DOMAIN2 Asia`
iatlanticocean=`$GETTEXT $DOMAIN2 "Atlantic Ocean"`
iaustralia=`$GETTEXT $DOMAIN2 Australia`
ieurope=`$GETTEXT $DOMAIN2 Europe`
ipacificocean=`$GETTEXT $DOMAIN2 "Pacific Ocean"`
iindianocean=`$GETTEXT $DOMAIN2 "Indian Ocean"`
none=`$GETTEXT "$INFO_POSIX"`
# Begin the main loop. We come back here if the user wants to retry.
while
$PRINTF >&2 "`$GETTEXT "$INFO_LOCATION"`\n"
continent=
country=
region=
# Ask the user for continent or ocean.
$PRINTF >&2 "`$GETTEXT "$INFO_SELECT_CONT"`\n"
select continent in \
$iafrica \
$iamerica \
$iantarctica \
$iarcticocean \
$iasia \
$iatlanticocean \
$iaustralia \
$ieurope \
$iindianocean \
$ipacificocean \
$none
do
case $continent in
'')
$PRINTF >&2 "`$GETTEXT "$WARN_ENTER_NUM"`\n";;
?*)
case $continent in
$iafrica) continent=Africa;;
$iamerica) continent=America;;
$iantarctica) continent=Antarctica;;
$iarcticocean) continent=Arctic;;
$iasia) continent=Asia;;
$iatlanticocean) continent=Atlantic;;
$iaustralia) continent=Australia;;
$ieurope) continent=Europe;;
$iindianocean) continent=Indian;;
$ipacificocean) continent=Pacific;;
$none) continent=none;;
esac
break
esac
done
case $continent in
'')
exit 1;;
none)
# Ask the user for a POSIX TZ string. Check that it conforms.
while
$PRINTF >&2 "`$GETTEXT "$INFO_ENTER_POSIX"`\n"
$PRINTF >&2 "`$GETTEXT "$INFO_POSIX_EX"`\n"
read TZ
env LC_ALL=C $AWK -v TZ="$TZ" 'BEGIN {
tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+"
time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?"
offset = "[-+]?" time
date = "(J?[0-9]+|M[0-9]+\.[0-9]+\.[0-9]+)"
datetime = "," date "(/" time ")?"
tzpattern = "^(:.*|" tzname offset "(" tzname \
"(" offset ")?(" datetime datetime ")?)?)$"
if (TZ ~ tzpattern) exit 1
exit 0
}'
do
$PRINTF >&2 "`$GETTEXT "$ERR_INV_POSIX"`\n" $TZ
done
TZ_for_date=$TZ;;
*)
# Get list of names of countries in the continent or ocean.
countries=$($AWK -F'\t' \
-v continent="$continent" \
-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
'
/^#/ { next }
$3 ~ ("^" continent "/") {
if (!cc_seen[$1]++) cc_list[++ccs] = $1
}
END {
while (getline <TZ_COUNTRY_TABLE) {
if ($0 !~ /^#/) cc_name[$1] = $2
}
for (i = 1; i <= ccs; i++) {
country = cc_list[i]
if (cc_name[country]) {
country = cc_name[country]
}
print country
}
}
' <$TZ_ZONE_TABLE | $SORT -f)
# i18n country names
c=0
set -A icountry
for country in $countries
do
if [ $is_C -eq 1 ]; then
icountry[c]=$country
else
icountry[c]=`${GETTEXT} ${DOMAIN2} $country`
fi
ocountry[c]="$country"
c=$(( $c + 1 ))
done
maxnum=$c
# If there's more than one country, ask the user which one.
case $countries in
*"$newline"*)
$PRINTF >&2 "`$GETTEXT "$INFO_SELECT_CNTRY"`\n"
select xcountry in ${icountry[*]}
do
case $xcountry in
'')
$PRINTF >&2 "`$GETTEXT "$WARN_ENTER_NUM"`\n"
;;
?*) c=0
while true; do
if [ "$xcountry" = "${icountry[$c]}" ];
then
country="${ocountry[$c]}"
break
fi
if [ $c -lt $maxnum ]; then
c=$(( $c + 1 ))
else
break
fi
done
break
esac
done
case $xcountry in
'') exit 1
esac;;
*)
country=$countries
xcountry=$countries
esac
# Get list of names of time zone rule regions in the country.
regions=$($AWK -F'\t' \
-v country="$country" \
-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
'
BEGIN {
cc = country
while (getline <TZ_COUNTRY_TABLE) {
if ($0 !~ /^#/ && country == $2) {
cc = $1
break
}
}
}
$1 == cc { print $5 }
' <$TZ_ZONE_TABLE)
# I18n region names
c=0
set -A iregion
for region in $regions
do
if [ $is_C -eq 1 ]; then
iregion[c]=$region
else
iregion[c]=`${GETTEXT} ${DOMAIN2} $region`
fi
oregion[c]="$region"
c=$(( $c + 1 ))
done
maxnum=$c
# If there's more than one region, ask the user which one.
case $regions in
*"$newline"*)
$PRINTF >&2 "`$GETTEXT "$INFO_SELECT_TZ"`\n"
select xregion in ${iregion[*]}
do
case $xregion in
'')
$PRINTF >&2 "`$GETTEXT "$WARN_ENTER_NUM"`\n"
;;
?*) c=0
while true; do
if [ "$xregion" = "${iregion[$c]}" ];
then
region="${oregion[$c]}"
break
fi
if [ $c -lt $maxnum ]; then
c=$(( $c + 1 ))
else
break
fi
done
break
esac
done
case $region in
'') exit 1
esac;;
*)
region=$regions
xregion=$regions
esac
# Determine TZ from country and region.
TZ=$($AWK -F'\t' \
-v country="$country" \
-v region="$region" \
-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
'
BEGIN {
cc = country
while (getline <TZ_COUNTRY_TABLE) {
if ($0 !~ /^#/ && country == $2) {
cc = $1
break
}
}
}
$1 == cc && $5 == region {
# Check if tzname mapped to
# backward compatible tzname
if ($4 == "-") {
print $3
} else {
print $4
}
}
' <$TZ_ZONE_TABLE)
# Make sure the corresponding zoneinfo file exists.
TZ_for_date=$TZDIR/$TZ
<$TZ_for_date || {
$PRINTF >&2 "`$GETTEXT "$ERR_NO_SETUP"`\n" $0
exit 1
}
# Absolute path TZ's not supported
TZ_for_date=$TZ
esac
# Use the proposed TZ to output the current date relative to UTC.
# Loop until they agree in seconds.
# Give up after 8 unsuccessful tries.
extra_info1=
extra_info2=
for i in 1 2 3 4 5 6 7 8
do
TZdate=$(LANG=C TZ="$TZ_for_date" $DATE)
UTdate=$(LANG=C TZ=UTC0 $DATE)
TZsec=$($EXPR "$TZdate" : '.*:\([0-5][0-9]\)')
UTsec=$($EXPR "$UTdate" : '.*:\([0-5][0-9]\)')
case $TZsec in
$UTsec)
extra_info1=$($PRINTF "`$GETTEXT "$INFO_EXTRA1"`" \
"$TZdate")
extra_info2=$($PRINTF "`$GETTEXT "$INFO_EXTRA2"`" \
"$UTdate")
break
esac
done
# Output TZ info and ask the user to confirm.
$PRINTF >&2 "\n"
$PRINTF >&2 "`$GETTEXT "$INFO_INFO"`\n"
$PRINTF >&2 "\n"
case $country+$region in
?*+?*) $PRINTF >&2 " $xcountry$newline $xregion\n";;
?*+) $PRINTF >&2 " $xcountry\n";;
+) $PRINTF >&2 " TZ='$TZ'\n"
esac
$PRINTF >&2 "\n"
$PRINTF >&2 "`$GETTEXT "$INFO_TZ"`\n" "$TZ"
$PRINTF >&2 "$extra_info1\n"
$PRINTF >&2 "$extra_info2\n"
$PRINTF >&2 "`$GETTEXT "$INFO_OK"`\n"
ok=
# select ok in Yes No
Yes="`$GETTEXT "$INFO_YES"`"
No="`$GETTEXT "$INFO_NO"`"
select ok in $Yes $No
do
case $ok in
'')
$PRINTF >&2 "`$GETTEXT "$WARN_ENTER_YORN"`\n"
;;
?*) break
esac
done
case $ok in
'') exit 1;;
$Yes) break
esac
do :
done
$PRINTF >&2 "`$GETTEXT "$INFO_FINE"`\n"
$PRINTF "%s\n" "$TZ"
|