From 670a687dea6773147a227bebfa9d801dae739ee0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 7 Jan 2016 11:45:07 +0000 Subject: Update timezone code from tzcode 2015g. This patch updates the timezone code from tzcode 2015g. The Makefile and README changes are based on those in Paul's patch . Tested for x86_64 and x86. 2016-01-06 Paul Eggert Joseph Myers * timezone/private.h: Update from tzcode 2015g. * timezone/tzfile.h: Likewise. * timezone/tzselect.ksh: Likewise. * timezone/zdump.c: Likewise. * timezone/zic.c: Likewise. * timezone/ialloc.c: Remove file. * timezone/scheck.c: Likewise. * timezone/Makefile (extra-objs): Remove variable. ($(objpfx)zic): Do not depend on scheck.o and ialloc.o. (tz-cflags): Add -DHAVE_GETTEXT -DUSE_LTZ=0 -Wno-maybe-uninitialized. (CFLAGS-zdump.c): Remove -fwrapv -DNOID -DHAVE_GETTEXT. (CFLAGS-zic.c): Remove -DNOID -DHAVE_GETTEXT. (CFLAGS-ialloc.c): Remove variable. (CFLAGS-scheck.c): Likewise. * timezone/README: Update list of files from tzcode. --- timezone/tzselect.ksh | 138 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 95 insertions(+), 43 deletions(-) (limited to 'timezone/tzselect.ksh') diff --git a/timezone/tzselect.ksh b/timezone/tzselect.ksh index 9d7069116a..2c3b2f4438 100755 --- a/timezone/tzselect.ksh +++ b/timezone/tzselect.ksh @@ -37,15 +37,22 @@ REPORT_BUGS_TO=tz@iana.org : ${AWK=awk} : ${TZDIR=`pwd`} +# Output one argument as-is to standard output. +# Safer than 'echo', which can mishandle '\' or leading '-'. +say() { + printf '%s\n' "$1" +} + # Check for awk Posix compliance. ($AWK -v x=y 'BEGIN { exit 123 }') /dev/null 2>&1 [ $? = 123 ] || { - echo >&2 "$0: Sorry, your \`$AWK' program is not Posix compatible." + say >&2 "$0: Sorry, your '$AWK' program is not Posix compatible." exit 1 } coord= location_limit=10 +zonetabtype=zone1970 usage="Usage: tzselect [--version] [--help] [-c COORD] [-n LIMIT] Select a time zone interactively. @@ -80,7 +87,7 @@ if ?*) : ;; '') # '; exit' should be redundant, but Dash doesn't properly fail without it. - (eval 'set --; select x; do break; done; exit') 2>/dev/null + (eval 'set --; select x; do break; done; exit') /dev/null esac then # Do this inside 'eval', as otherwise the shell might exit when parsing it @@ -139,41 +146,58 @@ else } fi -while getopts c:n:-: opt +while getopts c:n:t:-: opt do case $opt$OPTARG in c*) coord=$OPTARG ;; n*) location_limit=$OPTARG ;; + t*) # Undocumented option, used for developer testing. + zonetabtype=$OPTARG ;; -help) exec echo "$usage" ;; -version) exec echo "tzselect $PKGVERSION$TZVERSION" ;; -*) - echo >&2 "$0: -$opt$OPTARG: unknown option; try '$0 --help'"; exit 1 ;; + say >&2 "$0: -$opt$OPTARG: unknown option; try '$0 --help'"; exit 1 ;; *) - echo >&2 "$0: try '$0 --help'"; exit 1 ;; + say >&2 "$0: try '$0 --help'"; exit 1 ;; esac done shift `expr $OPTIND - 1` case $# in 0) ;; -*) echo >&2 "$0: $1: unknown argument"; exit 1 ;; +*) say >&2 "$0: $1: unknown argument"; exit 1 ;; esac # Make sure the tables are readable. TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab -TZ_ZONE_TABLE=$TZDIR/zone.tab +TZ_ZONE_TABLE=$TZDIR/$zonetabtype.tab for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE do - <$f || { - echo >&2 "$0: time zone files are not set up correctly" + <"$f" || { + say >&2 "$0: time zone files are not set up correctly" exit 1 } done +# If the current locale does not support UTF-8, convert data to current +# locale's format if possible, as the shell aligns columns better that way. +# Check the UTF-8 of U+12345 CUNEIFORM SIGN URU TIMES KI. +! $AWK 'BEGIN { u12345 = "\360\222\215\205"; exit length(u12345) != 1 }' && + { tmp=`(mktemp -d) 2>/dev/null` || { + tmp=${TMPDIR-/tmp}/tzselect.$$ && + (umask 77 && mkdir -- "$tmp") + };} && + trap 'status=$?; rm -fr -- "$tmp"; exit $status' 0 HUP INT PIPE TERM && + (iconv -f UTF-8 -t //TRANSLIT <"$TZ_COUNTRY_TABLE" >$tmp/iso3166.tab) \ + 2>/dev/null && + TZ_COUNTRY_TABLE=$tmp/iso3166.tab && + iconv -f UTF-8 -t //TRANSLIT <"$TZ_ZONE_TABLE" >$tmp/$zonetabtype.tab && + TZ_ZONE_TABLE=$tmp/$zonetabtype.tab + newline=' ' IFS=$newline @@ -189,7 +213,13 @@ output_distances=' country[$1] = $2 country["US"] = "US" # Otherwise the strings get too long. } - function convert_coord(coord, deg, min, ilen, sign, sec) { + function abs(x) { + return x < 0 ? -x : x; + } + function min(x, y) { + return x < y ? x : y; + } + function convert_coord(coord, deg, minute, ilen, sign, sec) { if (coord ~ /^[-+]?[0-9]?[0-9][0-9][0-9][0-9][0-9][0-9]([^0-9]|$)/) { degminsec = coord intdeg = degminsec < 0 ? -int(-degminsec / 10000) : int(degminsec / 10000) @@ -200,8 +230,8 @@ output_distances=' } else if (coord ~ /^[-+]?[0-9]?[0-9][0-9][0-9][0-9]([^0-9]|$)/) { degmin = coord intdeg = degmin < 0 ? -int(-degmin / 100) : int(degmin / 100) - min = degmin - intdeg * 100 - deg = (intdeg * 60 + min) / 60 + minute = degmin - intdeg * 100 + deg = (intdeg * 60 + minute) / 60 } else deg = coord return deg * 0.017453292519943296 @@ -217,14 +247,27 @@ output_distances=' # Great-circle distance between points with given latitude and longitude. # Inputs and output are in radians. This uses the great-circle special # case of the Vicenty formula for distances on ellipsoids. - function dist(lat1, long1, lat2, long2, dlong, x, y, num, denom) { + function gcdist(lat1, long1, lat2, long2, dlong, x, y, num, denom) { dlong = long2 - long1 - x = cos (lat2) * sin (dlong) - y = cos (lat1) * sin (lat2) - sin (lat1) * cos (lat2) * cos (dlong) - num = sqrt (x * x + y * y) - denom = sin (lat1) * sin (lat2) + cos (lat1) * cos (lat2) * cos (dlong) + x = cos(lat2) * sin(dlong) + y = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlong) + num = sqrt(x * x + y * y) + denom = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlong) return atan2(num, denom) } + # Parallel distance between points with given latitude and longitude. + # This is the product of the longitude difference and the cosine + # of the latitude of the point that is further from the equator. + # I.e., it considers longitudes to be further apart if they are + # nearer the equator. + function pardist(lat1, long1, lat2, long2) { + return abs(long1 - long2) * min(cos(lat1), cos(lat2)) + } + # The distance function is the sum of the great-circle distance and + # the parallel distance. It could be weighted. + function dist(lat1, long1, lat2, long2) { + return gcdist(lat1, long1, lat2, long2) + pardist(lat1, long1, lat2, long2) + } BEGIN { coord_lat = convert_latitude(coord) coord_long = convert_longitude(coord) @@ -232,7 +275,13 @@ output_distances=' /^[^#]/ { here_lat = convert_latitude($2) here_long = convert_longitude($2) - line = $1 "\t" $2 "\t" $3 "\t" country[$1] + line = $1 "\t" $2 "\t" $3 + sep = "\t" + ncc = split($1, cc, /,/) + for (i = 1; i <= ncc; i++) { + line = line sep country[cc[i]] + sep = ", " + } if (NF == 4) line = line " - " $4 printf "%g\t%s\n", dist(coord_lat, coord_long, here_lat, here_long), line @@ -269,7 +318,7 @@ while entry = entry " Ocean" printf "'\''%s'\''\n", entry } - ' $TZ_ZONE_TABLE | + ' <"$TZ_ZONE_TABLE" | sort -u | tr '\n' ' ' echo '' @@ -300,7 +349,7 @@ while 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]+)" + date = "(J?[0-9]+|M[0-9]+\\.[0-9]+\\.[0-9]+)" datetime = "," date "(/" time ")?" tzpattern = "^(:.*|" tzname offset "(" tzname \ "(" offset ")?(" datetime datetime ")?)?)$" @@ -308,8 +357,7 @@ while exit 0 }' do - echo >&2 "\`$TZ' is not a conforming" \ - 'Posix time zone string.' + say >&2 "'$TZ' is not a conforming Posix time zone string." done TZ_for_date=$TZ;; *) @@ -327,11 +375,11 @@ while distance_table=`$AWK \ -v coord="$coord" \ -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \ - "$output_distances" <$TZ_ZONE_TABLE | + "$output_distances" <"$TZ_ZONE_TABLE" | sort -n | sed "${location_limit}q" ` - regions=`echo "$distance_table" | $AWK ' + regions=`say "$distance_table" | $AWK ' BEGIN { FS = "\t" } { print $NF } '` @@ -341,7 +389,7 @@ while "of distance from $coord". doselect $regions region=$select_result - TZ=`echo "$distance_table" | $AWK -v region="$region" ' + TZ=`say "$distance_table" | $AWK -v region="$region" ' BEGIN { FS="\t" } $NF == region { print $4 } '` @@ -355,7 +403,9 @@ while BEGIN { FS = "\t" } /^#/ { next } $3 ~ ("^" continent "/") { - if (!cc_seen[$1]++) cc_list[++ccs] = $1 + ncc = split($1, cc, /,/) + for (i = 1; i <= ncc; i++) + if (!cc_seen[cc[i]]++) cc_list[++ccs] = cc[i] } END { while (getline &2 "$0: time zone files are not set up correctly" + <"$TZ_for_date" || { + say >&2 "$0: time zone files are not set up correctly" exit 1 } esac @@ -470,15 +522,15 @@ Universal Time is now: $UTdate." echo >&2 "The following information has been given:" echo >&2 "" case $country%$region%$coord in - ?*%?*%) echo >&2 " $country$newline $region";; - ?*%%) echo >&2 " $country";; - %?*%?*) echo >&2 " coord $coord$newline $region";; - %%?*) echo >&2 " coord $coord";; - +) echo >&2 " TZ='$TZ'" + ?*%?*%) say >&2 " $country$newline $region";; + ?*%%) say >&2 " $country";; + %?*%?*) say >&2 " coord $coord$newline $region";; + %%?*) say >&2 " coord $coord";; + *) say >&2 " TZ='$TZ'" esac - echo >&2 "" - echo >&2 "Therefore TZ='$TZ' will be used.$extra_info" - echo >&2 "Is the above information OK?" + say >&2 "" + say >&2 "Therefore TZ='$TZ' will be used.$extra_info" + say >&2 "Is the above information OK?" doselect Yes No ok=$select_result @@ -493,7 +545,7 @@ case $SHELL in *) file=.profile line="TZ='$TZ'; export TZ" esac -echo >&2 " +say >&2 " You can make this change permanent for yourself by appending the line $line to the file '$file' in your home directory; then log out and log in again. @@ -501,4 +553,4 @@ to the file '$file' in your home directory; then log out and log in again. Here is that TZ value again, this time on standard output so that you can use the $0 command in shell scripts:" -echo "$TZ" +say "$TZ" -- cgit v1.2.1