Home | History | Annotate | Line # | Download | only in time
tzselect.ksh revision 1.4
      1  1.1     jtc #! /bin/ksh
      2  1.2   perry #
      3  1.4  kleink #	$NetBSD: tzselect.ksh,v 1.4 1998/09/10 15:58:40 kleink Exp $
      4  1.2   perry #
      5  1.1     jtc # Ask the user about the time zone, and output the resulting TZ value to stdout.
      6  1.1     jtc # Interact with the user via stderr and stdin.
      7  1.1     jtc 
      8  1.1     jtc # Contributed by Paul Eggert <eggert (at] twinsun.com>.
      9  1.1     jtc 
     10  1.1     jtc # Porting notes:
     11  1.1     jtc #
     12  1.1     jtc # This script requires several features of the Korn shell.
     13  1.1     jtc # If your host lacks the Korn shell,
     14  1.1     jtc # you can use either of the following free programs instead:
     15  1.1     jtc #
     16  1.4  kleink #	<a href=ftp://ftp.gnu.org/pub/gnu/>
     17  1.1     jtc #	Bourne-Again shell (bash)
     18  1.3     jtc #	</a>
     19  1.1     jtc #
     20  1.3     jtc #	<a href=ftp://ftp.cs.mun.ca/pub/pdksh/pdksh.tar.gz>
     21  1.1     jtc #	Public domain ksh
     22  1.3     jtc #	</a>
     23  1.1     jtc #
     24  1.1     jtc # This script also uses several features of modern awk programs.
     25  1.1     jtc # If your host lacks awk, or has an old awk that does not conform to Posix.2,
     26  1.1     jtc # you can use either of the following free programs instead:
     27  1.1     jtc #
     28  1.4  kleink #	<a href=ftp://ftp.gnu.org/pub/gnu/>
     29  1.1     jtc #	GNU awk (gawk)
     30  1.3     jtc #	</a>
     31  1.1     jtc #
     32  1.3     jtc #	<a href=ftp://ftp.whidbey.net/pub/brennan/>
     33  1.1     jtc #	mawk
     34  1.3     jtc #	</a>
     35  1.1     jtc 
     36  1.1     jtc 
     37  1.1     jtc # Specify default values for environment variables if they are unset.
     38  1.1     jtc : ${AWK=awk}
     39  1.1     jtc : ${TZDIR=$(pwd)}
     40  1.1     jtc 
     41  1.1     jtc # Check for awk Posix compliance.
     42  1.1     jtc ($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1
     43  1.1     jtc [ $? = 123 ] || {
     44  1.1     jtc 	echo >&2 "$0: Sorry, your \`$AWK' program is not Posix compatible."
     45  1.1     jtc 	exit 1
     46  1.1     jtc }
     47  1.1     jtc 
     48  1.1     jtc # Make sure the tables are readable.
     49  1.1     jtc TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab
     50  1.1     jtc TZ_ZONE_TABLE=$TZDIR/zone.tab
     51  1.1     jtc for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
     52  1.1     jtc do
     53  1.1     jtc 	<$f || {
     54  1.1     jtc 		echo >&2 "$0: time zone files are not set up correctly"
     55  1.1     jtc 		exit 1
     56  1.1     jtc 	}
     57  1.1     jtc done
     58  1.1     jtc 
     59  1.1     jtc newline='
     60  1.1     jtc '
     61  1.1     jtc IFS=$newline
     62  1.1     jtc 
     63  1.1     jtc 
     64  1.2   perry # Work around a bug in bash 1.14.7 and earlier, where $PS3 is sent to stdout.
     65  1.1     jtc case $(echo 1 | (select x in x; do break; done) 2>/dev/null) in
     66  1.1     jtc ?*) PS3=
     67  1.1     jtc esac
     68  1.1     jtc 
     69  1.1     jtc 
     70  1.1     jtc # Begin the main loop.  We come back here if the user wants to retry.
     71  1.1     jtc while
     72  1.1     jtc 
     73  1.1     jtc 	echo >&2 'Please identify a location' \
     74  1.1     jtc 		'so that time zone rules can be set correctly.'
     75  1.1     jtc 
     76  1.1     jtc 	continent=
     77  1.1     jtc 	country=
     78  1.1     jtc 	region=
     79  1.1     jtc 
     80  1.1     jtc 
     81  1.1     jtc 	# Ask the user for continent or ocean.
     82  1.1     jtc 
     83  1.1     jtc 	echo >&2 'Please select a continent or ocean.'
     84  1.1     jtc 
     85  1.1     jtc 	select continent in \
     86  1.1     jtc 	    Africa \
     87  1.1     jtc 	    Americas \
     88  1.1     jtc 	    Antarctica \
     89  1.1     jtc 	    'Arctic Ocean' \
     90  1.1     jtc 	    Asia \
     91  1.1     jtc 	    'Atlantic Ocean' \
     92  1.1     jtc 	    Australia \
     93  1.1     jtc 	    Europe \
     94  1.1     jtc 	    'Indian Ocean' \
     95  1.1     jtc 	    'Pacific Ocean' \
     96  1.1     jtc 	    'none - I want to specify the time zone using the Posix TZ format.'
     97  1.1     jtc 	do
     98  1.1     jtc 	    case $continent in
     99  1.1     jtc 	    '')
    100  1.1     jtc 		echo >&2 'Please enter a number in range.';;
    101  1.1     jtc 	    ?*)
    102  1.1     jtc 		case $continent in
    103  1.1     jtc 		Americas) continent=America;;
    104  1.1     jtc 		*' '*) continent=$(expr "$continent" : '\([^ ]*\)')
    105  1.1     jtc 		esac
    106  1.1     jtc 		break
    107  1.1     jtc 	    esac
    108  1.1     jtc 	done
    109  1.1     jtc 	case $continent in
    110  1.1     jtc 	'')
    111  1.1     jtc 		exit 1;;
    112  1.1     jtc 	none)
    113  1.1     jtc 		# Ask the user for a Posix TZ string.  Check that it conforms.
    114  1.1     jtc 		while
    115  1.1     jtc 			echo >&2 'Please enter the desired value' \
    116  1.1     jtc 				'of the TZ environment variable.'
    117  1.1     jtc 			echo >&2 'For example, GST-10 is a zone named GST' \
    118  1.1     jtc 				'that is 10 hours ahead (east) of UTC.'
    119  1.1     jtc 			read TZ
    120  1.1     jtc 			$AWK -v TZ="$TZ" 'BEGIN {
    121  1.1     jtc 				tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+"
    122  1.1     jtc 				time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?"
    123  1.1     jtc 				offset = "[-+]?" time
    124  1.1     jtc 				date = "(J?[0-9]+|M[0-9]+\.[0-9]+\.[0-9]+)"
    125  1.1     jtc 				datetime = "," date "(/" time ")?"
    126  1.1     jtc 				tzpattern = "^(:.*|" tzname offset "(" tzname \
    127  1.1     jtc 				  "(" offset ")?(" datetime datetime ")?)?)$"
    128  1.1     jtc 				if (TZ ~ tzpattern) exit 1
    129  1.1     jtc 				exit 0
    130  1.1     jtc 			}'
    131  1.1     jtc 		do
    132  1.1     jtc 			echo >&2 "\`$TZ' is not a conforming" \
    133  1.1     jtc 				'Posix time zone string.'
    134  1.1     jtc 		done
    135  1.1     jtc 		TZ_for_date=$TZ;;
    136  1.1     jtc 	*)
    137  1.1     jtc 		# Get list of names of countries in the continent or ocean.
    138  1.1     jtc 		countries=$($AWK -F'\t' \
    139  1.1     jtc 			-v continent="$continent" \
    140  1.1     jtc 			-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    141  1.1     jtc 		'
    142  1.1     jtc 			/^#/ { next }
    143  1.1     jtc 			$3 ~ ("^" continent "/") {
    144  1.1     jtc 				if (!cc_seen[$1]++) cc_list[++ccs] = $1
    145  1.1     jtc 			}
    146  1.1     jtc 			END {
    147  1.1     jtc 				while (getline <TZ_COUNTRY_TABLE) {
    148  1.1     jtc 					if ($0 !~ /^#/) cc_name[$1] = $2
    149  1.1     jtc 				}
    150  1.1     jtc 				for (i = 1; i <= ccs; i++) {
    151  1.1     jtc 					country = cc_list[i]
    152  1.1     jtc 					if (cc_name[country]) {
    153  1.1     jtc 					  country = cc_name[country]
    154  1.1     jtc 					}
    155  1.1     jtc 					print country
    156  1.1     jtc 				}
    157  1.1     jtc 			}
    158  1.1     jtc 		' <$TZ_ZONE_TABLE | sort -f)
    159  1.1     jtc 
    160  1.1     jtc 
    161  1.1     jtc 		# If there's more than one country, ask the user which one.
    162  1.1     jtc 		case $countries in
    163  1.1     jtc 		*"$newline"*)
    164  1.1     jtc 			echo >&2 'Please select a country.'
    165  1.1     jtc 			select country in $countries
    166  1.1     jtc 			do
    167  1.1     jtc 			    case $country in
    168  1.1     jtc 			    '') echo >&2 'Please enter a number in range.';;
    169  1.1     jtc 			    ?*) break
    170  1.1     jtc 			    esac
    171  1.1     jtc 			done
    172  1.1     jtc 
    173  1.1     jtc 			case $country in
    174  1.1     jtc 			'') exit 1
    175  1.1     jtc 			esac;;
    176  1.1     jtc 		*)
    177  1.1     jtc 			country=$countries
    178  1.1     jtc 		esac
    179  1.1     jtc 
    180  1.1     jtc 
    181  1.1     jtc 		# Get list of names of time zone rule regions in the country.
    182  1.1     jtc 		regions=$($AWK -F'\t' \
    183  1.1     jtc 			-v country="$country" \
    184  1.1     jtc 			-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    185  1.1     jtc 		'
    186  1.1     jtc 			BEGIN {
    187  1.1     jtc 				cc = country
    188  1.1     jtc 				while (getline <TZ_COUNTRY_TABLE) {
    189  1.1     jtc 					if ($0 !~ /^#/  &&  country == $2) {
    190  1.1     jtc 						cc = $1
    191  1.1     jtc 						break
    192  1.1     jtc 					}
    193  1.1     jtc 				}
    194  1.1     jtc 			}
    195  1.1     jtc 			$1 == cc { print $4 }
    196  1.1     jtc 		' <$TZ_ZONE_TABLE)
    197  1.1     jtc 
    198  1.1     jtc 
    199  1.1     jtc 		# If there's more than one region, ask the user which one.
    200  1.1     jtc 		case $regions in
    201  1.1     jtc 		*"$newline"*)
    202  1.1     jtc 			echo >&2 'Please select one of the following' \
    203  1.1     jtc 				'time zone regions.'
    204  1.1     jtc 			select region in $regions
    205  1.1     jtc 			do
    206  1.1     jtc 				case $region in
    207  1.1     jtc 				'') echo >&2 'Please enter a number in range.';;
    208  1.1     jtc 				?*) break
    209  1.1     jtc 				esac
    210  1.1     jtc 			done
    211  1.1     jtc 			case $region in
    212  1.1     jtc 			'') exit 1
    213  1.1     jtc 			esac;;
    214  1.1     jtc 		*)
    215  1.1     jtc 			region=$regions
    216  1.1     jtc 		esac
    217  1.1     jtc 
    218  1.1     jtc 		# Determine TZ from country and region.
    219  1.1     jtc 		TZ=$($AWK -F'\t' \
    220  1.1     jtc 			-v country="$country" \
    221  1.1     jtc 			-v region="$region" \
    222  1.1     jtc 			-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    223  1.1     jtc 		'
    224  1.1     jtc 			BEGIN {
    225  1.1     jtc 				cc = country
    226  1.1     jtc 				while (getline <TZ_COUNTRY_TABLE) {
    227  1.1     jtc 					if ($0 !~ /^#/  &&  country == $2) {
    228  1.1     jtc 						cc = $1
    229  1.1     jtc 						break
    230  1.1     jtc 					}
    231  1.1     jtc 				}
    232  1.1     jtc 			}
    233  1.1     jtc 			$1 == cc && $4 == region { print $3 }
    234  1.1     jtc 		' <$TZ_ZONE_TABLE)
    235  1.1     jtc 
    236  1.1     jtc 		# Make sure the corresponding zoneinfo file exists.
    237  1.1     jtc 		TZ_for_date=$TZDIR/$TZ
    238  1.1     jtc 		<$TZ_for_date || {
    239  1.1     jtc 			echo >&2 "$0: time zone files are not set up correctly"
    240  1.1     jtc 			exit 1
    241  1.1     jtc 		}
    242  1.1     jtc 	esac
    243  1.1     jtc 
    244  1.1     jtc 
    245  1.1     jtc 	# Use the proposed TZ to output the current date relative to UTC.
    246  1.1     jtc 	# Loop until they agree in seconds.
    247  1.1     jtc 	# Give up after 8 unsuccessful tries.
    248  1.1     jtc 
    249  1.1     jtc 	extra_info=
    250  1.1     jtc 	for i in 1 2 3 4 5 6 7 8
    251  1.1     jtc 	do
    252  1.1     jtc 		TZdate=$(LANG=C TZ="$TZ_for_date" date)
    253  1.1     jtc 		UTdate=$(LANG=C TZ=UTC0 date)
    254  1.1     jtc 		TZsec=$(expr "$TZdate" : '.*:\([0-5][0-9]\)')
    255  1.1     jtc 		UTsec=$(expr "$UTdate" : '.*:\([0-5][0-9]\)')
    256  1.1     jtc 		case $TZsec in
    257  1.1     jtc 		$UTsec)
    258  1.1     jtc 			extra_info="
    259  1.1     jtc Local time is now:	$TZdate.
    260  1.1     jtc Universal Time is now:	$UTdate."
    261  1.1     jtc 			break
    262  1.1     jtc 		esac
    263  1.1     jtc 	done
    264  1.1     jtc 
    265  1.1     jtc 
    266  1.1     jtc 	# Output TZ info and ask the user to confirm.
    267  1.1     jtc 
    268  1.1     jtc 	echo >&2 ""
    269  1.1     jtc 	echo >&2 "The following information has been given:"
    270  1.1     jtc 	echo >&2 ""
    271  1.1     jtc 	case $country+$region in
    272  1.1     jtc 	?*+?*)	echo >&2 "	$country$newline	$region";;
    273  1.1     jtc 	?*+)	echo >&2 "	$country";;
    274  1.1     jtc 	+)	echo >&2 "	TZ='$TZ'"
    275  1.1     jtc 	esac
    276  1.1     jtc 	echo >&2 ""
    277  1.1     jtc 	echo >&2 "Therefore TZ='$TZ' will be used.$extra_info"
    278  1.1     jtc 	echo >&2 "Is the above information OK?"
    279  1.1     jtc 
    280  1.1     jtc 	ok=
    281  1.1     jtc 	select ok in Yes No
    282  1.1     jtc 	do
    283  1.1     jtc 	    case $ok in
    284  1.1     jtc 	    '') echo >&2 'Please enter 1 for Yes, or 2 for No.';;
    285  1.1     jtc 	    ?*) break
    286  1.1     jtc 	    esac
    287  1.1     jtc 	done
    288  1.1     jtc 	case $ok in
    289  1.1     jtc 	'') exit 1;;
    290  1.1     jtc 	Yes) break
    291  1.1     jtc 	esac
    292  1.1     jtc do :
    293  1.1     jtc done
    294  1.1     jtc 
    295  1.1     jtc # Output the answer.
    296  1.1     jtc echo "$TZ"
    297