Home | History | Annotate | Line # | Download | only in time
tzselect.ksh revision 1.20
      1   1.8  christos #! /bin/bash
      2   1.2     perry #
      3  1.18  christos # Ask the user about the time zone, and output the resulting TZ value to stdout.
      4  1.18  christos # Interact with the user via stderr and stdin.
      5  1.18  christos #
      6  1.20  christos #	$NetBSD: tzselect.ksh,v 1.20 2022/08/16 11:07:40 christos Exp $
      7   1.2     perry #
      8   1.8  christos PKGVERSION='(tzcode) '
      9   1.8  christos TZVERSION=see_Makefile
     10   1.8  christos REPORT_BUGS_TO=tz@iana.org
     11   1.5    kleink 
     12  1.15  christos # Contributed by Paul Eggert.  This file is in the public domain.
     13   1.1       jtc 
     14   1.1       jtc # Porting notes:
     15   1.1       jtc #
     16  1.10  christos # This script requires a Posix-like shell and prefers the extension of a
     17   1.8  christos # 'select' statement.  The 'select' statement was introduced in the
     18   1.8  christos # Korn shell and is available in Bash and other shell implementations.
     19   1.8  christos # If your host lacks both Bash and the Korn shell, you can get their
     20   1.8  christos # source from one of these locations:
     21   1.1       jtc #
     22  1.16  christos #	Bash <https://www.gnu.org/software/bash/>
     23   1.8  christos #	Korn Shell <http://www.kornshell.com/>
     24  1.19  christos #	MirBSD Korn Shell <http://www.mirbsd.org/mksh.htm>
     25   1.1       jtc #
     26  1.19  christos # For portability to Solaris 10 /bin/sh (supported by Oracle through
     27  1.19  christos # January 2024) this script avoids some POSIX features and common
     28  1.19  christos # extensions, such as $(...) (which works sometimes but not others),
     29  1.19  christos # $((...)), ! CMD, ${#ID}, ${ID##PAT}, ${ID%%PAT}, and $10.
     30  1.19  christos 
     31  1.10  christos #
     32   1.1       jtc # This script also uses several features of modern awk programs.
     33   1.8  christos # If your host lacks awk, or has an old awk that does not conform to Posix,
     34   1.1       jtc # you can use either of the following free programs instead:
     35   1.1       jtc #
     36  1.16  christos #	Gawk (GNU awk) <https://www.gnu.org/software/gawk/>
     37  1.16  christos #	mawk <https://invisible-island.net/mawk/>
     38   1.1       jtc 
     39   1.1       jtc 
     40   1.1       jtc # Specify default values for environment variables if they are unset.
     41   1.1       jtc : ${AWK=awk}
     42  1.10  christos : ${TZDIR=`pwd`}
     43   1.1       jtc 
     44  1.14  christos # Output one argument as-is to standard output.
     45  1.14  christos # Safer than 'echo', which can mishandle '\' or leading '-'.
     46  1.14  christos say() {
     47  1.14  christos     printf '%s\n' "$1"
     48  1.14  christos }
     49  1.14  christos 
     50   1.1       jtc # Check for awk Posix compliance.
     51   1.1       jtc ($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1
     52   1.1       jtc [ $? = 123 ] || {
     53  1.14  christos 	say >&2 "$0: Sorry, your '$AWK' program is not Posix compatible."
     54   1.1       jtc 	exit 1
     55   1.1       jtc }
     56   1.1       jtc 
     57   1.9  christos coord=
     58   1.9  christos location_limit=10
     59  1.11  christos zonetabtype=zone1970
     60   1.9  christos 
     61   1.9  christos usage="Usage: tzselect [--version] [--help] [-c COORD] [-n LIMIT]
     62  1.18  christos Select a timezone interactively.
     63   1.6   mlelstv 
     64   1.9  christos Options:
     65   1.9  christos 
     66   1.9  christos   -c COORD
     67   1.9  christos     Instead of asking for continent and then country and then city,
     68   1.9  christos     ask for selection from time zones whose largest cities
     69   1.9  christos     are closest to the location with geographical coordinates COORD.
     70   1.9  christos     COORD should use ISO 6709 notation, for example, '-c +4852+00220'
     71   1.9  christos     for Paris (in degrees and minutes, North and East), or
     72   1.9  christos     '-c -35-058' for Buenos Aires (in degrees, South and West).
     73   1.9  christos 
     74   1.9  christos   -n LIMIT
     75   1.9  christos     Display at most LIMIT locations when -c is used (default $location_limit).
     76   1.9  christos 
     77   1.9  christos   --version
     78   1.9  christos     Output version information.
     79   1.9  christos 
     80   1.9  christos   --help
     81   1.9  christos     Output this help.
     82   1.9  christos 
     83   1.9  christos Report bugs to $REPORT_BUGS_TO."
     84   1.9  christos 
     85  1.10  christos # Ask the user to select from the function's arguments,
     86  1.10  christos # and assign the selected argument to the variable 'select_result'.
     87  1.10  christos # Exit on EOF or I/O error.  Use the shell's 'select' builtin if available,
     88  1.10  christos # falling back on a less-nice but portable substitute otherwise.
     89  1.10  christos if
     90  1.10  christos   case $BASH_VERSION in
     91  1.10  christos   ?*) : ;;
     92  1.10  christos   '')
     93  1.10  christos     # '; exit' should be redundant, but Dash doesn't properly fail without it.
     94  1.11  christos     (eval 'set --; select x; do break; done; exit') </dev/null 2>/dev/null
     95  1.10  christos   esac
     96  1.10  christos then
     97  1.10  christos   # Do this inside 'eval', as otherwise the shell might exit when parsing it
     98  1.10  christos   # even though it is never executed.
     99  1.10  christos   eval '
    100  1.10  christos     doselect() {
    101  1.10  christos       select select_result
    102  1.10  christos       do
    103  1.10  christos 	case $select_result in
    104  1.10  christos 	"") echo >&2 "Please enter a number in range." ;;
    105  1.10  christos 	?*) break
    106  1.10  christos 	esac
    107  1.10  christos       done || exit
    108  1.10  christos     }
    109  1.10  christos   '
    110  1.10  christos else
    111  1.10  christos   doselect() {
    112  1.10  christos     # Field width of the prompt numbers.
    113  1.10  christos     select_width=`expr $# : '.*'`
    114  1.10  christos 
    115  1.10  christos     select_i=
    116  1.10  christos 
    117  1.10  christos     while :
    118  1.10  christos     do
    119  1.10  christos       case $select_i in
    120  1.10  christos       '')
    121  1.10  christos 	select_i=0
    122  1.10  christos 	for select_word
    123  1.10  christos 	do
    124  1.10  christos 	  select_i=`expr $select_i + 1`
    125  1.10  christos 	  printf >&2 "%${select_width}d) %s\\n" $select_i "$select_word"
    126  1.10  christos 	done ;;
    127  1.10  christos       *[!0-9]*)
    128  1.10  christos 	echo >&2 'Please enter a number in range.' ;;
    129  1.10  christos       *)
    130  1.10  christos 	if test 1 -le $select_i && test $select_i -le $#; then
    131  1.10  christos 	  shift `expr $select_i - 1`
    132  1.10  christos 	  select_result=$1
    133  1.10  christos 	  break
    134  1.10  christos 	fi
    135  1.10  christos 	echo >&2 'Please enter a number in range.'
    136  1.10  christos       esac
    137  1.10  christos 
    138  1.10  christos       # Prompt and read input.
    139  1.10  christos       printf >&2 %s "${PS3-#? }"
    140  1.10  christos       read select_i || exit
    141  1.10  christos     done
    142  1.10  christos   }
    143  1.10  christos fi
    144  1.10  christos 
    145  1.12  christos while getopts c:n:t:-: opt
    146   1.9  christos do
    147   1.9  christos     case $opt$OPTARG in
    148   1.9  christos     c*)
    149   1.9  christos 	coord=$OPTARG ;;
    150   1.9  christos     n*)
    151   1.9  christos 	location_limit=$OPTARG ;;
    152  1.11  christos     t*) # Undocumented option, used for developer testing.
    153  1.11  christos 	zonetabtype=$OPTARG ;;
    154   1.9  christos     -help)
    155   1.9  christos 	exec echo "$usage" ;;
    156   1.9  christos     -version)
    157   1.9  christos 	exec echo "tzselect $PKGVERSION$TZVERSION" ;;
    158   1.9  christos     -*)
    159  1.14  christos 	say >&2 "$0: -$opt$OPTARG: unknown option; try '$0 --help'"; exit 1 ;;
    160   1.9  christos     *)
    161  1.14  christos 	say >&2 "$0: try '$0 --help'"; exit 1 ;;
    162   1.9  christos     esac
    163   1.9  christos done
    164   1.9  christos 
    165  1.10  christos shift `expr $OPTIND - 1`
    166   1.9  christos case $# in
    167   1.9  christos 0) ;;
    168  1.14  christos *) say >&2 "$0: $1: unknown argument"; exit 1 ;;
    169   1.9  christos esac
    170   1.6   mlelstv 
    171   1.1       jtc # Make sure the tables are readable.
    172   1.1       jtc TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab
    173  1.11  christos TZ_ZONE_TABLE=$TZDIR/$zonetabtype.tab
    174   1.1       jtc for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
    175   1.1       jtc do
    176  1.11  christos 	<"$f" || {
    177  1.14  christos 		say >&2 "$0: time zone files are not set up correctly"
    178   1.1       jtc 		exit 1
    179   1.1       jtc 	}
    180   1.1       jtc done
    181   1.1       jtc 
    182  1.14  christos # If the current locale does not support UTF-8, convert data to current
    183  1.14  christos # locale's format if possible, as the shell aligns columns better that way.
    184  1.14  christos # Check the UTF-8 of U+12345 CUNEIFORM SIGN URU TIMES KI.
    185  1.19  christos $AWK 'BEGIN { u12345 = "\360\222\215\205"; exit length(u12345) != 1 }' || {
    186  1.14  christos     { tmp=`(mktemp -d) 2>/dev/null` || {
    187  1.14  christos 	tmp=${TMPDIR-/tmp}/tzselect.$$ &&
    188  1.14  christos 	(umask 77 && mkdir -- "$tmp")
    189  1.14  christos     };} &&
    190  1.14  christos     trap 'status=$?; rm -fr -- "$tmp"; exit $status' 0 HUP INT PIPE TERM &&
    191  1.14  christos     (iconv -f UTF-8 -t //TRANSLIT <"$TZ_COUNTRY_TABLE" >$tmp/iso3166.tab) \
    192  1.14  christos         2>/dev/null &&
    193  1.14  christos     TZ_COUNTRY_TABLE=$tmp/iso3166.tab &&
    194  1.14  christos     iconv -f UTF-8 -t //TRANSLIT <"$TZ_ZONE_TABLE" >$tmp/$zonetabtype.tab &&
    195  1.14  christos     TZ_ZONE_TABLE=$tmp/$zonetabtype.tab
    196  1.19  christos }
    197  1.14  christos 
    198   1.1       jtc newline='
    199   1.1       jtc '
    200   1.1       jtc IFS=$newline
    201   1.1       jtc 
    202   1.1       jtc 
    203   1.9  christos # Awk script to read a time zone table and output the same table,
    204   1.9  christos # with each column preceded by its distance from 'here'.
    205   1.9  christos output_distances='
    206   1.9  christos   BEGIN {
    207   1.9  christos     FS = "\t"
    208   1.9  christos     while (getline <TZ_COUNTRY_TABLE)
    209   1.9  christos       if ($0 ~ /^[^#]/)
    210   1.9  christos         country[$1] = $2
    211   1.9  christos     country["US"] = "US" # Otherwise the strings get too long.
    212   1.9  christos   }
    213  1.12  christos   function abs(x) {
    214  1.12  christos     return x < 0 ? -x : x;
    215  1.12  christos   }
    216  1.12  christos   function min(x, y) {
    217  1.12  christos     return x < y ? x : y;
    218  1.12  christos   }
    219  1.12  christos   function convert_coord(coord, deg, minute, ilen, sign, sec) {
    220   1.9  christos     if (coord ~ /^[-+]?[0-9]?[0-9][0-9][0-9][0-9][0-9][0-9]([^0-9]|$)/) {
    221   1.9  christos       degminsec = coord
    222   1.9  christos       intdeg = degminsec < 0 ? -int(-degminsec / 10000) : int(degminsec / 10000)
    223   1.9  christos       minsec = degminsec - intdeg * 10000
    224   1.9  christos       intmin = minsec < 0 ? -int(-minsec / 100) : int(minsec / 100)
    225   1.9  christos       sec = minsec - intmin * 100
    226   1.9  christos       deg = (intdeg * 3600 + intmin * 60 + sec) / 3600
    227   1.9  christos     } else if (coord ~ /^[-+]?[0-9]?[0-9][0-9][0-9][0-9]([^0-9]|$)/) {
    228   1.9  christos       degmin = coord
    229   1.9  christos       intdeg = degmin < 0 ? -int(-degmin / 100) : int(degmin / 100)
    230  1.12  christos       minute = degmin - intdeg * 100
    231  1.12  christos       deg = (intdeg * 60 + minute) / 60
    232   1.9  christos     } else
    233   1.9  christos       deg = coord
    234   1.9  christos     return deg * 0.017453292519943296
    235   1.9  christos   }
    236   1.9  christos   function convert_latitude(coord) {
    237   1.9  christos     match(coord, /..*[-+]/)
    238   1.9  christos     return convert_coord(substr(coord, 1, RLENGTH - 1))
    239   1.9  christos   }
    240   1.9  christos   function convert_longitude(coord) {
    241   1.9  christos     match(coord, /..*[-+]/)
    242   1.9  christos     return convert_coord(substr(coord, RLENGTH))
    243   1.9  christos   }
    244   1.9  christos   # Great-circle distance between points with given latitude and longitude.
    245   1.9  christos   # Inputs and output are in radians.  This uses the great-circle special
    246   1.9  christos   # case of the Vicenty formula for distances on ellipsoids.
    247  1.12  christos   function gcdist(lat1, long1, lat2, long2, dlong, x, y, num, denom) {
    248   1.9  christos     dlong = long2 - long1
    249  1.13  christos     x = cos(lat2) * sin(dlong)
    250  1.13  christos     y = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlong)
    251  1.13  christos     num = sqrt(x * x + y * y)
    252  1.13  christos     denom = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlong)
    253   1.9  christos     return atan2(num, denom)
    254   1.9  christos   }
    255  1.12  christos   # Parallel distance between points with given latitude and longitude.
    256  1.12  christos   # This is the product of the longitude difference and the cosine
    257  1.12  christos   # of the latitude of the point that is further from the equator.
    258  1.12  christos   # I.e., it considers longitudes to be further apart if they are
    259  1.12  christos   # nearer the equator.
    260  1.12  christos   function pardist(lat1, long1, lat2, long2) {
    261  1.13  christos     return abs(long1 - long2) * min(cos(lat1), cos(lat2))
    262  1.12  christos   }
    263  1.12  christos   # The distance function is the sum of the great-circle distance and
    264  1.12  christos   # the parallel distance.  It could be weighted.
    265  1.12  christos   function dist(lat1, long1, lat2, long2) {
    266  1.13  christos     return gcdist(lat1, long1, lat2, long2) + pardist(lat1, long1, lat2, long2)
    267  1.12  christos   }
    268   1.9  christos   BEGIN {
    269   1.9  christos     coord_lat = convert_latitude(coord)
    270   1.9  christos     coord_long = convert_longitude(coord)
    271   1.9  christos   }
    272   1.9  christos   /^[^#]/ {
    273   1.9  christos     here_lat = convert_latitude($2)
    274   1.9  christos     here_long = convert_longitude($2)
    275  1.11  christos     line = $1 "\t" $2 "\t" $3
    276  1.11  christos     sep = "\t"
    277  1.11  christos     ncc = split($1, cc, /,/)
    278  1.11  christos     for (i = 1; i <= ncc; i++) {
    279  1.11  christos       line = line sep country[cc[i]]
    280  1.11  christos       sep = ", "
    281  1.11  christos     }
    282   1.9  christos     if (NF == 4)
    283   1.9  christos       line = line " - " $4
    284   1.9  christos     printf "%g\t%s\n", dist(coord_lat, coord_long, here_lat, here_long), line
    285   1.9  christos   }
    286   1.9  christos '
    287   1.1       jtc 
    288   1.1       jtc # Begin the main loop.  We come back here if the user wants to retry.
    289   1.1       jtc while
    290   1.1       jtc 
    291   1.1       jtc 	echo >&2 'Please identify a location' \
    292   1.1       jtc 		'so that time zone rules can be set correctly.'
    293   1.1       jtc 
    294   1.1       jtc 	continent=
    295   1.1       jtc 	country=
    296   1.1       jtc 	region=
    297   1.1       jtc 
    298   1.9  christos 	case $coord in
    299   1.9  christos 	?*)
    300   1.9  christos 		continent=coord;;
    301   1.9  christos 	'')
    302   1.1       jtc 
    303   1.1       jtc 	# Ask the user for continent or ocean.
    304   1.1       jtc 
    305   1.9  christos 	echo >&2 'Please select a continent, ocean, "coord", or "TZ".'
    306   1.1       jtc 
    307  1.10  christos         quoted_continents=`
    308  1.10  christos 	  $AWK '
    309  1.20  christos 	    function handle_entry(entry) {
    310  1.20  christos 	      entry = substr(entry, 1, index(entry, "/") - 1)
    311  1.20  christos 	      if (entry == "America")
    312  1.20  christos 	       entry = entry "s"
    313  1.20  christos 	      if (entry ~ /^(Arctic|Atlantic|Indian|Pacific)$/)
    314  1.20  christos 	       entry = entry " Ocean"
    315  1.20  christos 	      printf "'\''%s'\''\n", entry
    316  1.20  christos 	    }
    317  1.10  christos 	    BEGIN { FS = "\t" }
    318   1.9  christos 	    /^[^#]/ {
    319  1.20  christos               handle_entry($3)
    320   1.9  christos             }
    321  1.20  christos 	    /^#@/ {
    322  1.20  christos 	      ncont = split($2, cont, /,/)
    323  1.20  christos 	      for (ci = 1; ci <= ncont; ci++) {
    324  1.20  christos 	        handle_entry(cont[ci])
    325  1.20  christos 	      }
    326  1.20  christos 	    }
    327  1.11  christos           ' <"$TZ_ZONE_TABLE" |
    328   1.9  christos 	  sort -u |
    329   1.9  christos 	  tr '\n' ' '
    330   1.9  christos 	  echo ''
    331  1.10  christos 	`
    332   1.9  christos 
    333   1.9  christos 	eval '
    334  1.10  christos 	    doselect '"$quoted_continents"' \
    335   1.9  christos 		"coord - I want to use geographical coordinates." \
    336  1.18  christos 		"TZ - I want to specify the timezone using the Posix TZ format."
    337  1.10  christos 	    continent=$select_result
    338  1.10  christos 	    case $continent in
    339  1.10  christos 	    Americas) continent=America;;
    340  1.10  christos 	    *" "*) continent=`expr "$continent" : '\''\([^ ]*\)'\''`
    341  1.10  christos 	    esac
    342   1.9  christos 	'
    343   1.9  christos 	esac
    344   1.9  christos 
    345   1.1       jtc 	case $continent in
    346   1.9  christos 	TZ)
    347   1.1       jtc 		# Ask the user for a Posix TZ string.  Check that it conforms.
    348   1.1       jtc 		while
    349   1.1       jtc 			echo >&2 'Please enter the desired value' \
    350   1.1       jtc 				'of the TZ environment variable.'
    351  1.18  christos 			echo >&2 'For example, AEST-10 is abbreviated' \
    352  1.18  christos 				'AEST and is 10 hours'
    353  1.17  christos 			echo >&2 'ahead (east) of Greenwich,' \
    354  1.17  christos 				'with no daylight saving time.'
    355   1.1       jtc 			read TZ
    356   1.1       jtc 			$AWK -v TZ="$TZ" 'BEGIN {
    357  1.15  christos 				tzname = "(<[[:alnum:]+-]{3,}>|[[:alpha:]]{3,})"
    358  1.15  christos 				time = "(2[0-4]|[0-1]?[0-9])" \
    359  1.15  christos 				  "(:[0-5][0-9](:[0-5][0-9])?)?"
    360   1.1       jtc 				offset = "[-+]?" time
    361  1.15  christos 				mdate = "M([1-9]|1[0-2])\\.[1-5]\\.[0-6]"
    362  1.15  christos 				jdate = "((J[1-9]|[0-9]|J?[1-9][0-9]" \
    363  1.15  christos 				  "|J?[1-2][0-9][0-9])|J?3[0-5][0-9]|J?36[0-5])"
    364  1.15  christos 				datetime = ",(" mdate "|" jdate ")(/" time ")?"
    365   1.1       jtc 				tzpattern = "^(:.*|" tzname offset "(" tzname \
    366   1.1       jtc 				  "(" offset ")?(" datetime datetime ")?)?)$"
    367   1.1       jtc 				if (TZ ~ tzpattern) exit 1
    368   1.1       jtc 				exit 0
    369   1.1       jtc 			}'
    370   1.1       jtc 		do
    371  1.18  christos 		    say >&2 "'$TZ' is not a conforming Posix timezone string."
    372   1.1       jtc 		done
    373   1.1       jtc 		TZ_for_date=$TZ;;
    374   1.1       jtc 	*)
    375   1.9  christos 		case $continent in
    376   1.9  christos 		coord)
    377   1.9  christos 		    case $coord in
    378   1.9  christos 		    '')
    379   1.9  christos 			echo >&2 'Please enter coordinates' \
    380   1.9  christos 				'in ISO 6709 notation.'
    381   1.9  christos 			echo >&2 'For example, +4042-07403 stands for'
    382   1.9  christos 			echo >&2 '40 degrees 42 minutes north,' \
    383   1.9  christos 				'74 degrees 3 minutes west.'
    384   1.9  christos 			read coord;;
    385   1.9  christos 		    esac
    386  1.10  christos 		    distance_table=`$AWK \
    387   1.9  christos 			    -v coord="$coord" \
    388   1.9  christos 			    -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    389  1.11  christos 			    "$output_distances" <"$TZ_ZONE_TABLE" |
    390   1.9  christos 		      sort -n |
    391   1.9  christos 		      sed "${location_limit}q"
    392  1.10  christos 		    `
    393  1.14  christos 		    regions=`say "$distance_table" | $AWK '
    394   1.9  christos 		      BEGIN { FS = "\t" }
    395   1.9  christos 		      { print $NF }
    396  1.10  christos 		    '`
    397  1.18  christos 		    echo >&2 'Please select one of the following timezones,' \
    398   1.9  christos 		    echo >&2 'listed roughly in increasing order' \
    399   1.9  christos 			    "of distance from $coord".
    400  1.10  christos 		    doselect $regions
    401  1.10  christos 		    region=$select_result
    402  1.14  christos 		    TZ=`say "$distance_table" | $AWK -v region="$region" '
    403   1.9  christos 		      BEGIN { FS="\t" }
    404   1.9  christos 		      $NF == region { print $4 }
    405  1.10  christos 		    '`
    406   1.9  christos 		    ;;
    407   1.9  christos 		*)
    408   1.1       jtc 		# Get list of names of countries in the continent or ocean.
    409  1.10  christos 		countries=`$AWK \
    410  1.20  christos 			-v continent_re="^$continent/" \
    411   1.1       jtc 			-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    412   1.1       jtc 		'
    413  1.10  christos 			BEGIN { FS = "\t" }
    414  1.20  christos 			/^#$/ { next }
    415  1.20  christos 			/^#[^@]/ { next }
    416  1.20  christos 			{
    417  1.20  christos 			  commentary = $0 ~ /^#@/
    418  1.20  christos 			  if (commentary) {
    419  1.20  christos 			    col1ccs = substr($1, 3)
    420  1.20  christos 			    conts = $2
    421  1.20  christos 			  } else {
    422  1.20  christos 			    col1ccs = $1
    423  1.20  christos 			    conts = $3
    424  1.20  christos 			  }
    425  1.20  christos 			  ncc = split(col1ccs, cc, /,/)
    426  1.20  christos 			  ncont = split(conts, cont, /,/)
    427  1.20  christos 			  for (i = 1; i <= ncc; i++) {
    428  1.20  christos 			    elsewhere = commentary
    429  1.20  christos 			    for (ci = 1; ci <= ncont; ci++) {
    430  1.20  christos 			      if (cont[ci] ~ continent_re) {
    431  1.11  christos 				if (!cc_seen[cc[i]]++) cc_list[++ccs] = cc[i]
    432  1.20  christos 				elsewhere = 0
    433  1.20  christos 			      }
    434  1.20  christos 			    }
    435  1.20  christos 			    if (elsewhere) {
    436  1.20  christos 			      for (i = 1; i <= ncc; i++) {
    437  1.20  christos 			        cc_elsewhere[cc[i]] = 1
    438  1.20  christos 			      }
    439  1.20  christos 			    }
    440  1.20  christos 			  }
    441   1.1       jtc 			}
    442   1.1       jtc 			END {
    443   1.1       jtc 				while (getline <TZ_COUNTRY_TABLE) {
    444   1.1       jtc 					if ($0 !~ /^#/) cc_name[$1] = $2
    445   1.1       jtc 				}
    446   1.1       jtc 				for (i = 1; i <= ccs; i++) {
    447   1.1       jtc 					country = cc_list[i]
    448  1.20  christos 					if (cc_elsewhere[country]) continue
    449   1.1       jtc 					if (cc_name[country]) {
    450   1.1       jtc 					  country = cc_name[country]
    451   1.1       jtc 					}
    452   1.1       jtc 					print country
    453   1.1       jtc 				}
    454   1.1       jtc 			}
    455  1.11  christos 		' <"$TZ_ZONE_TABLE" | sort -f`
    456   1.1       jtc 
    457   1.1       jtc 
    458   1.1       jtc 		# If there's more than one country, ask the user which one.
    459   1.1       jtc 		case $countries in
    460   1.1       jtc 		*"$newline"*)
    461   1.9  christos 			echo >&2 'Please select a country' \
    462   1.9  christos 				'whose clocks agree with yours.'
    463  1.10  christos 			doselect $countries
    464  1.10  christos 			country=$select_result;;
    465   1.1       jtc 		*)
    466   1.1       jtc 			country=$countries
    467   1.1       jtc 		esac
    468   1.1       jtc 
    469   1.1       jtc 
    470  1.18  christos 		# Get list of timezones in the country.
    471  1.10  christos 		regions=`$AWK \
    472   1.1       jtc 			-v country="$country" \
    473   1.1       jtc 			-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    474   1.1       jtc 		'
    475   1.1       jtc 			BEGIN {
    476  1.10  christos 				FS = "\t"
    477   1.1       jtc 				cc = country
    478   1.1       jtc 				while (getline <TZ_COUNTRY_TABLE) {
    479   1.1       jtc 					if ($0 !~ /^#/  &&  country == $2) {
    480   1.1       jtc 						cc = $1
    481   1.1       jtc 						break
    482   1.1       jtc 					}
    483   1.1       jtc 				}
    484   1.1       jtc 			}
    485  1.14  christos 			/^#/ { next }
    486  1.11  christos 			$1 ~ cc { print $4 }
    487  1.11  christos 		' <"$TZ_ZONE_TABLE"`
    488   1.1       jtc 
    489   1.1       jtc 
    490   1.1       jtc 		# If there's more than one region, ask the user which one.
    491   1.1       jtc 		case $regions in
    492   1.1       jtc 		*"$newline"*)
    493  1.18  christos 			echo >&2 'Please select one of the following timezones.'
    494  1.10  christos 			doselect $regions
    495  1.10  christos 			region=$select_result;;
    496   1.1       jtc 		*)
    497   1.1       jtc 			region=$regions
    498   1.1       jtc 		esac
    499   1.1       jtc 
    500   1.1       jtc 		# Determine TZ from country and region.
    501  1.10  christos 		TZ=`$AWK \
    502   1.1       jtc 			-v country="$country" \
    503   1.1       jtc 			-v region="$region" \
    504   1.1       jtc 			-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    505   1.1       jtc 		'
    506   1.1       jtc 			BEGIN {
    507  1.10  christos 				FS = "\t"
    508   1.1       jtc 				cc = country
    509   1.1       jtc 				while (getline <TZ_COUNTRY_TABLE) {
    510   1.1       jtc 					if ($0 !~ /^#/  &&  country == $2) {
    511   1.1       jtc 						cc = $1
    512   1.1       jtc 						break
    513   1.1       jtc 					}
    514   1.1       jtc 				}
    515   1.1       jtc 			}
    516  1.14  christos 			/^#/ { next }
    517  1.11  christos 			$1 ~ cc && $4 == region { print $3 }
    518  1.11  christos 		' <"$TZ_ZONE_TABLE"`
    519   1.9  christos 		esac
    520   1.1       jtc 
    521   1.1       jtc 		# Make sure the corresponding zoneinfo file exists.
    522   1.1       jtc 		TZ_for_date=$TZDIR/$TZ
    523  1.11  christos 		<"$TZ_for_date" || {
    524  1.14  christos 			say >&2 "$0: time zone files are not set up correctly"
    525   1.1       jtc 			exit 1
    526   1.1       jtc 		}
    527   1.1       jtc 	esac
    528   1.1       jtc 
    529   1.1       jtc 
    530   1.1       jtc 	# Use the proposed TZ to output the current date relative to UTC.
    531   1.1       jtc 	# Loop until they agree in seconds.
    532   1.1       jtc 	# Give up after 8 unsuccessful tries.
    533   1.1       jtc 
    534   1.1       jtc 	extra_info=
    535   1.1       jtc 	for i in 1 2 3 4 5 6 7 8
    536   1.1       jtc 	do
    537  1.10  christos 		TZdate=`LANG=C TZ="$TZ_for_date" date`
    538  1.10  christos 		UTdate=`LANG=C TZ=UTC0 date`
    539  1.10  christos 		TZsec=`expr "$TZdate" : '.*:\([0-5][0-9]\)'`
    540  1.10  christos 		UTsec=`expr "$UTdate" : '.*:\([0-5][0-9]\)'`
    541   1.1       jtc 		case $TZsec in
    542   1.1       jtc 		$UTsec)
    543   1.1       jtc 			extra_info="
    544  1.15  christos Selected time is now:	$TZdate.
    545   1.1       jtc Universal Time is now:	$UTdate."
    546   1.1       jtc 			break
    547   1.1       jtc 		esac
    548   1.1       jtc 	done
    549   1.1       jtc 
    550   1.1       jtc 
    551   1.1       jtc 	# Output TZ info and ask the user to confirm.
    552   1.1       jtc 
    553   1.1       jtc 	echo >&2 ""
    554   1.1       jtc 	echo >&2 "The following information has been given:"
    555   1.1       jtc 	echo >&2 ""
    556   1.9  christos 	case $country%$region%$coord in
    557  1.14  christos 	?*%?*%)	say >&2 "	$country$newline	$region";;
    558  1.14  christos 	?*%%)	say >&2 "	$country";;
    559  1.14  christos 	%?*%?*) say >&2 "	coord $coord$newline	$region";;
    560  1.14  christos 	%%?*)	say >&2 "	coord $coord";;
    561  1.14  christos 	*)	say >&2 "	TZ='$TZ'"
    562   1.1       jtc 	esac
    563  1.14  christos 	say >&2 ""
    564  1.14  christos 	say >&2 "Therefore TZ='$TZ' will be used.$extra_info"
    565  1.14  christos 	say >&2 "Is the above information OK?"
    566   1.1       jtc 
    567  1.10  christos 	doselect Yes No
    568  1.10  christos 	ok=$select_result
    569   1.1       jtc 	case $ok in
    570   1.1       jtc 	Yes) break
    571   1.1       jtc 	esac
    572   1.9  christos do coord=
    573   1.1       jtc done
    574   1.1       jtc 
    575   1.5    kleink case $SHELL in
    576   1.5    kleink *csh) file=.login line="setenv TZ '$TZ'";;
    577   1.5    kleink *) file=.profile line="TZ='$TZ'; export TZ"
    578   1.5    kleink esac
    579   1.5    kleink 
    580  1.15  christos test -t 1 && say >&2 "
    581   1.5    kleink You can make this change permanent for yourself by appending the line
    582   1.5    kleink 	$line
    583   1.5    kleink to the file '$file' in your home directory; then log out and log in again.
    584   1.5    kleink 
    585   1.5    kleink Here is that TZ value again, this time on standard output so that you
    586   1.5    kleink can use the $0 command in shell scripts:"
    587   1.5    kleink 
    588  1.14  christos say "$TZ"
    589