import tzcode96l
This commit is contained in:
parent
0b77b04b6e
commit
b27646e2c2
152
lib/libc/time/checktab.awk
Normal file
152
lib/libc/time/checktab.awk
Normal file
@ -0,0 +1,152 @@
|
||||
# Check tz tables for consistency.
|
||||
|
||||
# Contributed by Paul Eggert <eggert@twinsun.com>.
|
||||
|
||||
BEGIN {
|
||||
FS = "\t"
|
||||
|
||||
if (!iso_table) iso_table = "iso3166.tab"
|
||||
if (!zone_table) zone_table = "zone.tab"
|
||||
if (!want_warnings) want_warnings = -1
|
||||
|
||||
while (getline <iso_table) {
|
||||
iso_NR++
|
||||
if ($0 ~ /^#/) continue
|
||||
if (NF != 2) {
|
||||
printf "%s:%d: wrong number of columns\n",
|
||||
iso_table, iso_NR >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
cc = $1
|
||||
name = $2
|
||||
if (cc !~ /^[A-Z][A-Z]$/) {
|
||||
printf "%s:%d: invalid country code `%s'\n", \
|
||||
iso_table, iso_NR, cc >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
if (cc <= cc0) {
|
||||
printf "%s:%d: country code `%s' is %s\n", \
|
||||
iso_table, iso_NR, cc, \
|
||||
cc==cc0 ? "duplicate" : "out of order" \
|
||||
>>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
cc0 = cc
|
||||
if (name2cc[name]) {
|
||||
printf "%s:%d: `%s' and `%s' have the sname name\n", \
|
||||
iso_table, iso_NR, name2cc[name], cc \
|
||||
>>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
name2cc[name] = cc
|
||||
cc2name[cc] = name
|
||||
cc2NR[cc] = iso_NR
|
||||
}
|
||||
|
||||
zone_table = "zone.tab"
|
||||
cc0 = ""
|
||||
|
||||
while (getline <zone_table) {
|
||||
zone_NR++
|
||||
if ($0 ~ /^#/) continue
|
||||
if (NF != 3 && NF != 4) {
|
||||
printf "%s:%d: wrong number of columns\n",
|
||||
zone_table, zone_NR >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
cc = $1
|
||||
coordinates = $2
|
||||
tz = $3
|
||||
comments = $4
|
||||
if (cc < cc0) {
|
||||
printf "%s:%d: country code `%s' is out of order\n", \
|
||||
zone_table, zone_NR, cc >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
cc0 = cc
|
||||
if (tz2cc[tz]) {
|
||||
printf "%s:%d: %s: duplicate TZ column\n", \
|
||||
zone_table, zone_NR, tz >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
tz2cc[tz] = cc
|
||||
tz2comments[tz] = comments
|
||||
tz2NR[tz] = zone_NR
|
||||
if (cc2name[cc]) {
|
||||
cc_used[cc]++
|
||||
} else {
|
||||
printf "%s:%d: %s: unknown country code\n", \
|
||||
zone_table, zone_NR, cc >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
if (coordinates !~ /^[-+][0-9][0-9][0-5][0-9][-+][01][0-9][0-9][0-5][0-9]$/ \
|
||||
&& coordinates !~ /^[-+][0-9][0-9][0-5][0-9][0-5][0-9][-+][01][0-9][0-9][0-5][0-9][0-5][0-9]$/) {
|
||||
printf "%s:%d: %s: invalid coordinates\n", \
|
||||
zone_table, zone_NR, coordinates >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
}
|
||||
|
||||
for (tz in tz2cc) {
|
||||
if (cc_used[tz2cc[tz]] == 1) {
|
||||
if (tz2comments[tz]) {
|
||||
printf "%s:%d: unnecessary comment `%s'\n", \
|
||||
zone_table, tz2NR[tz], tz2comments[tz] \
|
||||
>>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
} else {
|
||||
if (!tz2comments[tz]) {
|
||||
printf "%s:%d: missing comment\n", \
|
||||
zone_table, tz2NR[tz] >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FS = " "
|
||||
}
|
||||
|
||||
{
|
||||
tz = ""
|
||||
if ($1 == "Zone") tz = $2
|
||||
if ($1 == "Link") {
|
||||
# Ignore Link commands if source and destination basenames
|
||||
# are identical, e.g. Europe/Istanbul versus Asia/Istanbul.
|
||||
src = $2
|
||||
dst = $3
|
||||
while ((i = index(src, "/"))) src = substr(src, i+1)
|
||||
while ((i = index(dst, "/"))) dst = substr(dst, i+1)
|
||||
if (src != dst) tz = $3
|
||||
}
|
||||
if (tz && tz ~ /\//) {
|
||||
if (!tz2cc[tz]) {
|
||||
printf "%s: no data for `%s'\n", zone_table, tz \
|
||||
>>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
zoneSeen[tz] = 1
|
||||
}
|
||||
}
|
||||
|
||||
END {
|
||||
for (tz in tz2cc) {
|
||||
if (!zoneSeen[tz]) {
|
||||
printf "%s:%d: no Zone table for `%s'\n", \
|
||||
zone_table, tz2NR[tz], tz >>"/dev/stderr"
|
||||
status = 1
|
||||
}
|
||||
}
|
||||
|
||||
if (0 < want_warnings) {
|
||||
for (cc in cc2name) {
|
||||
if (!cc_used[cc]) {
|
||||
printf "%s:%d: warning:" \
|
||||
"no Zone entries for %s (%s)\n",
|
||||
iso_table, cc2NR[cc], cc, cc2name[cc]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit status
|
||||
}
|
40
lib/libc/time/tzselect.8
Normal file
40
lib/libc/time/tzselect.8
Normal file
@ -0,0 +1,40 @@
|
||||
.TH TZSELECT 8
|
||||
.SH NAME
|
||||
tzselect \- select a time zone
|
||||
.SH SYNOPSIS
|
||||
.B tzselect
|
||||
.SH DESCRIPTION
|
||||
The
|
||||
.B tzselect
|
||||
program asks the user for information about the current location,
|
||||
and outputs the resulting time zone description to standard output.
|
||||
The output is suitable as a value for the TZ environment variable.
|
||||
.PP
|
||||
All interaction with the user is done via standard input and standard error.
|
||||
.SH "ENVIRONMENT VARIABLES"
|
||||
.TP
|
||||
\f3AWK\fP
|
||||
Name of a Posix-compliant
|
||||
.I awk
|
||||
program (default:
|
||||
.BR awk ).
|
||||
.TP
|
||||
\f3TZDIR\fP
|
||||
Name of the directory containing time zone data files (default:
|
||||
.BR /usr/local/etc/zoneinfo ).
|
||||
.SH FILES
|
||||
.TP
|
||||
\f2TZDIR\fP\f3/iso3166.tab\fP
|
||||
Table of ISO 3166 2-letter country codes and country names.
|
||||
.TP
|
||||
\f2TZDIR\fP\f3/zone.tab\fP
|
||||
Table of country codes, latitude and longitude, TZ values, and
|
||||
descriptive comments.
|
||||
.TP
|
||||
\f2TZDIR\fP\f3/\fP\f2TZ\fP
|
||||
Time zone data file for time zone \f2TZ\fP.
|
||||
.SH "EXIT STATUS"
|
||||
The exit status is zero if a time zone was successfully obtained from the user,
|
||||
nonzero otherwise.
|
||||
.SH "SEE ALSO"
|
||||
newctime(3), tzfile(5), zdump(8), zic(8)
|
292
lib/libc/time/tzselect.ksh
Normal file
292
lib/libc/time/tzselect.ksh
Normal file
@ -0,0 +1,292 @@
|
||||
#! /bin/ksh
|
||||
# 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 <eggert@twinsun.com>.
|
||||
|
||||
# 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:
|
||||
#
|
||||
# Bourne-Again shell (bash)
|
||||
# <URL:ftp://prep.ai.mit.edu:/pub/gnu/bash-1.14.7.tar.gz>
|
||||
# (or any later version)
|
||||
#
|
||||
# Public domain ksh
|
||||
# <URL:ftp://ftp.cs.mun.ca:/pub/pdksh/pdksh.tar.gz>
|
||||
#
|
||||
# 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:
|
||||
#
|
||||
# GNU awk (gawk)
|
||||
# <URL:ftp://prep.ai.mit.edu:/pub/gnu/gawk-3.0.0.tar.gz>
|
||||
# (or any later version)
|
||||
#
|
||||
# mawk
|
||||
# <URL:ftp://oxy.edu/public/mawk1.2.2.tar.gz>
|
||||
# (or any later version)
|
||||
|
||||
|
||||
# Specify default values for environment variables if they are unset.
|
||||
: ${AWK=awk}
|
||||
: ${TZDIR=$(pwd)}
|
||||
|
||||
# Check for awk Posix compliance.
|
||||
($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1
|
||||
[ $? = 123 ] || {
|
||||
echo >&2 "$0: Sorry, your \`$AWK' program is not Posix compatible."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Make sure the tables are readable.
|
||||
TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab
|
||||
TZ_ZONE_TABLE=$TZDIR/zone.tab
|
||||
for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
|
||||
do
|
||||
<$f || {
|
||||
echo >&2 "$0: time zone files are not set up correctly"
|
||||
exit 1
|
||||
}
|
||||
done
|
||||
|
||||
newline='
|
||||
'
|
||||
IFS=$newline
|
||||
|
||||
|
||||
# Work around a bash bug, where $PS3 is sent to stdout.
|
||||
case $(echo 1 | (select x in x; do break; done) 2>/dev/null) in
|
||||
?*) PS3=
|
||||
esac
|
||||
|
||||
|
||||
# Begin the main loop. We come back here if the user wants to retry.
|
||||
while
|
||||
|
||||
echo >&2 'Please identify a location' \
|
||||
'so that time zone rules can be set correctly.'
|
||||
|
||||
continent=
|
||||
country=
|
||||
region=
|
||||
|
||||
|
||||
# Ask the user for continent or ocean.
|
||||
|
||||
echo >&2 'Please select a continent or ocean.'
|
||||
|
||||
select continent in \
|
||||
Africa \
|
||||
Americas \
|
||||
Antarctica \
|
||||
'Arctic Ocean' \
|
||||
Asia \
|
||||
'Atlantic Ocean' \
|
||||
Australia \
|
||||
Europe \
|
||||
'Indian Ocean' \
|
||||
'Pacific Ocean' \
|
||||
'none - I want to specify the time zone using the Posix TZ format.'
|
||||
do
|
||||
case $continent in
|
||||
'')
|
||||
echo >&2 'Please enter a number in range.';;
|
||||
?*)
|
||||
case $continent in
|
||||
Americas) continent=America;;
|
||||
*' '*) continent=$(expr "$continent" : '\([^ ]*\)')
|
||||
esac
|
||||
break
|
||||
esac
|
||||
done
|
||||
case $continent in
|
||||
'')
|
||||
exit 1;;
|
||||
none)
|
||||
# Ask the user for a Posix TZ string. Check that it conforms.
|
||||
while
|
||||
echo >&2 'Please enter the desired value' \
|
||||
'of the TZ environment variable.'
|
||||
echo >&2 'For example, GST-10 is a zone named GST' \
|
||||
'that is 10 hours ahead (east) of UTC.'
|
||||
read TZ
|
||||
$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
|
||||
echo >&2 "\`$TZ' is not a conforming" \
|
||||
'Posix time zone string.'
|
||||
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)
|
||||
|
||||
|
||||
# If there's more than one country, ask the user which one.
|
||||
case $countries in
|
||||
*"$newline"*)
|
||||
echo >&2 'Please select a country.'
|
||||
select country in $countries
|
||||
do
|
||||
case $country in
|
||||
'') echo >&2 'Please enter a number in range.';;
|
||||
?*) break
|
||||
esac
|
||||
done
|
||||
|
||||
case $country in
|
||||
'') exit 1
|
||||
esac;;
|
||||
*)
|
||||
country=$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 $4 }
|
||||
' <$TZ_ZONE_TABLE)
|
||||
|
||||
|
||||
# If there's more than one region, ask the user which one.
|
||||
case $regions in
|
||||
*"$newline"*)
|
||||
echo >&2 'Please select one of the following' \
|
||||
'time zone regions.'
|
||||
select region in $regions
|
||||
do
|
||||
case $region in
|
||||
'') echo >&2 'Please enter a number in range.';;
|
||||
?*) break
|
||||
esac
|
||||
done
|
||||
case $region in
|
||||
'') exit 1
|
||||
esac;;
|
||||
*)
|
||||
region=$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 && $4 == region { print $3 }
|
||||
' <$TZ_ZONE_TABLE)
|
||||
|
||||
# Make sure the corresponding zoneinfo file exists.
|
||||
TZ_for_date=$TZDIR/$TZ
|
||||
<$TZ_for_date || {
|
||||
echo >&2 "$0: time zone files are not set up correctly"
|
||||
exit 1
|
||||
}
|
||||
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_info=
|
||||
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_info="
|
||||
Local time is now: $TZdate.
|
||||
Universal Time is now: $UTdate."
|
||||
break
|
||||
esac
|
||||
done
|
||||
|
||||
|
||||
# Output TZ info and ask the user to confirm.
|
||||
|
||||
echo >&2 ""
|
||||
echo >&2 "The following information has been given:"
|
||||
echo >&2 ""
|
||||
case $country+$region in
|
||||
?*+?*) echo >&2 " $country$newline $region";;
|
||||
?*+) echo >&2 " $country";;
|
||||
+) echo >&2 " TZ='$TZ'"
|
||||
esac
|
||||
echo >&2 ""
|
||||
echo >&2 "Therefore TZ='$TZ' will be used.$extra_info"
|
||||
echo >&2 "Is the above information OK?"
|
||||
|
||||
ok=
|
||||
select ok in Yes No
|
||||
do
|
||||
case $ok in
|
||||
'') echo >&2 'Please enter 1 for Yes, or 2 for No.';;
|
||||
?*) break
|
||||
esac
|
||||
done
|
||||
case $ok in
|
||||
'') exit 1;;
|
||||
Yes) break
|
||||
esac
|
||||
do :
|
||||
done
|
||||
|
||||
# Output the answer.
|
||||
echo "$TZ"
|
Loading…
Reference in New Issue
Block a user