Check supported DF_1_XXX bits
[glibc.git] / timezone / tzselect.ksh
blob8e66b4427355940e3b827e4f990270c992729dc1
1 #!/bin/bash
3 PKGVERSION='(tzcode) '
4 TZVERSION=see_Makefile
5 REPORT_BUGS_TO=tz@iana.org
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.
10 # Contributed by Paul Eggert.
12 # Porting notes:
14 # This script requires a Posix-like shell with the extension of a
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:
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/>
24 # This script also uses several features of modern awk programs.
25 # If your host lacks awk, or has an old awk that does not conform to Posix,
26 # you can use either of the following free programs instead:
28 # Gawk (GNU awk) <http://www.gnu.org/software/gawk/>
29 # mawk <http://invisible-island.net/mawk/>
32 # Specify default values for environment variables if they are unset.
33 : ${AWK=awk}
34 : ${TZDIR=$(pwd)}
36 # Check for awk Posix compliance.
37 ($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1
38 [ $? = 123 ] || {
39 echo >&2 "$0: Sorry, your \`$AWK' program is not Posix compatible."
40 exit 1
43 if [ "$1" = "--help" ]; then
44 cat <<EOF
45 Usage: tzselect
46 Select a time zone interactively.
48 Report bugs to $REPORT_BUGS_TO.
49 EOF
50 exit
51 elif [ "$1" = "--version" ]; then
52 cat <<EOF
53 tzselect $PKGVERSION$TZVERSION
54 EOF
55 exit
58 # Make sure the tables are readable.
59 TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab
60 TZ_ZONE_TABLE=$TZDIR/zone.tab
61 for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
63 <$f || {
64 echo >&2 "$0: time zone files are not set up correctly"
65 exit 1
67 done
69 newline='
71 IFS=$newline
74 # Work around a bug in bash 1.14.7 and earlier, where $PS3 is sent to stdout.
75 case $(echo 1 | (select x in x; do break; done) 2>/dev/null) in
76 ?*) PS3=
77 esac
80 # Begin the main loop. We come back here if the user wants to retry.
81 while
83 echo >&2 'Please identify a location' \
84 'so that time zone rules can be set correctly.'
86 continent=
87 country=
88 region=
91 # Ask the user for continent or ocean.
93 echo >&2 'Please select a continent or ocean.'
95 select continent in \
96 Africa \
97 Americas \
98 Antarctica \
99 'Arctic Ocean' \
100 Asia \
101 'Atlantic Ocean' \
102 Australia \
103 Europe \
104 'Indian Ocean' \
105 'Pacific Ocean' \
106 'none - I want to specify the time zone using the Posix TZ format.'
108 case $continent in
110 echo >&2 'Please enter a number in range.';;
112 case $continent in
113 Americas) continent=America;;
114 *' '*) continent=$(expr "$continent" : '\([^ ]*\)')
115 esac
116 break
117 esac
118 done
119 case $continent in
121 exit 1;;
122 none)
123 # Ask the user for a Posix TZ string. Check that it conforms.
124 while
125 echo >&2 'Please enter the desired value' \
126 'of the TZ environment variable.'
127 echo >&2 'For example, GST-10 is a zone named GST' \
128 'that is 10 hours ahead (east) of UTC.'
129 read TZ
130 $AWK -v TZ="$TZ" 'BEGIN {
131 tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+"
132 time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?"
133 offset = "[-+]?" time
134 date = "(J?[0-9]+|M[0-9]+\.[0-9]+\.[0-9]+)"
135 datetime = "," date "(/" time ")?"
136 tzpattern = "^(:.*|" tzname offset "(" tzname \
137 "(" offset ")?(" datetime datetime ")?)?)$"
138 if (TZ ~ tzpattern) exit 1
139 exit 0
142 echo >&2 "\`$TZ' is not a conforming" \
143 'Posix time zone string.'
144 done
145 TZ_for_date=$TZ;;
147 # Get list of names of countries in the continent or ocean.
148 countries=$($AWK -F'\t' \
149 -v continent="$continent" \
150 -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
152 /^#/ { next }
153 $3 ~ ("^" continent "/") {
154 if (!cc_seen[$1]++) cc_list[++ccs] = $1
156 END {
157 while (getline <TZ_COUNTRY_TABLE) {
158 if ($0 !~ /^#/) cc_name[$1] = $2
160 for (i = 1; i <= ccs; i++) {
161 country = cc_list[i]
162 if (cc_name[country]) {
163 country = cc_name[country]
165 print country
168 ' <$TZ_ZONE_TABLE | sort -f)
171 # If there's more than one country, ask the user which one.
172 case $countries in
173 *"$newline"*)
174 echo >&2 'Please select a country.'
175 select country in $countries
177 case $country in
178 '') echo >&2 'Please enter a number in range.';;
179 ?*) break
180 esac
181 done
183 case $country in
184 '') exit 1
185 esac;;
187 country=$countries
188 esac
191 # Get list of names of time zone rule regions in the country.
192 regions=$($AWK -F'\t' \
193 -v country="$country" \
194 -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
196 BEGIN {
197 cc = country
198 while (getline <TZ_COUNTRY_TABLE) {
199 if ($0 !~ /^#/ && country == $2) {
200 cc = $1
201 break
205 $1 == cc { print $4 }
206 ' <$TZ_ZONE_TABLE)
209 # If there's more than one region, ask the user which one.
210 case $regions in
211 *"$newline"*)
212 echo >&2 'Please select one of the following' \
213 'time zone regions.'
214 select region in $regions
216 case $region in
217 '') echo >&2 'Please enter a number in range.';;
218 ?*) break
219 esac
220 done
221 case $region in
222 '') exit 1
223 esac;;
225 region=$regions
226 esac
228 # Determine TZ from country and region.
229 TZ=$($AWK -F'\t' \
230 -v country="$country" \
231 -v region="$region" \
232 -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
234 BEGIN {
235 cc = country
236 while (getline <TZ_COUNTRY_TABLE) {
237 if ($0 !~ /^#/ && country == $2) {
238 cc = $1
239 break
243 $1 == cc && $4 == region { print $3 }
244 ' <$TZ_ZONE_TABLE)
246 # Make sure the corresponding zoneinfo file exists.
247 TZ_for_date=$TZDIR/$TZ
248 <$TZ_for_date || {
249 echo >&2 "$0: time zone files are not set up correctly"
250 exit 1
252 esac
255 # Use the proposed TZ to output the current date relative to UTC.
256 # Loop until they agree in seconds.
257 # Give up after 8 unsuccessful tries.
259 extra_info=
260 for i in 1 2 3 4 5 6 7 8
262 TZdate=$(LANG=C TZ="$TZ_for_date" date)
263 UTdate=$(LANG=C TZ=UTC0 date)
264 TZsec=$(expr "$TZdate" : '.*:\([0-5][0-9]\)')
265 UTsec=$(expr "$UTdate" : '.*:\([0-5][0-9]\)')
266 case $TZsec in
267 $UTsec)
268 extra_info="
269 Local time is now: $TZdate.
270 Universal Time is now: $UTdate."
271 break
272 esac
273 done
276 # Output TZ info and ask the user to confirm.
278 echo >&2 ""
279 echo >&2 "The following information has been given:"
280 echo >&2 ""
281 case $country+$region in
282 ?*+?*) echo >&2 " $country$newline $region";;
283 ?*+) echo >&2 " $country";;
284 +) echo >&2 " TZ='$TZ'"
285 esac
286 echo >&2 ""
287 echo >&2 "Therefore TZ='$TZ' will be used.$extra_info"
288 echo >&2 "Is the above information OK?"
291 select ok in Yes No
293 case $ok in
294 '') echo >&2 'Please enter 1 for Yes, or 2 for No.';;
295 ?*) break
296 esac
297 done
298 case $ok in
299 '') exit 1;;
300 Yes) break
301 esac
302 do :
303 done
305 case $SHELL in
306 *csh) file=.login line="setenv TZ '$TZ'";;
307 *) file=.profile line="TZ='$TZ'; export TZ"
308 esac
310 echo >&2 "
311 You can make this change permanent for yourself by appending the line
312 $line
313 to the file '$file' in your home directory; then log out and log in again.
315 Here is that TZ value again, this time on standard output so that you
316 can use the $0 command in shell scripts:"
318 echo "$TZ"