[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Guessing the date style from the timezone for postgresql postinst



In order to help remove unnecessary prompts from the Debian PostgreSQLl
installation script, I want it to guess the local date style, to be chosen
from the following list:

   Style      Date            Datetime
   -------------------------------------------------------
   ISO        1999-07-17      1999-07-17 07:09:18+01
   SQL        17/07/1999      17/07/1999 07:09:19.00 BST
   POSTGRES   17-07-1999      Sat 17 Jul 07:09:19 1999 BST
   GERMAN     17.07.1999      17.07.1999 07:09:19.00 BST
   NONEURO    07-17-1999      Sat Jul 17 07:09:19 1999 BST
   US         07-17-1999      Sat Jul 17 07:09:19 1999 BST
   EURO       17-07-1999      Sat 17 Jul 07:09:19 1999 BST

I propose to include the attached script. If the zone belongs to USA or
Canada, I use American format; if it belongs to another country I use
European format; if the country is unidentified I try to guess from
the zone abbreviation and its offset from UTC.  For zones which aren't based
on regional names I use ISO.

Are there any other countries besides USA and Canada which use American
datestyle?  Am I right in thinking that Canada does? 

If you have a recent potato system, with timezone files in
/usr/share/zoneinfo, could you please run the script and let me know if
it gives WRONG results for you.  If it does, please tell me your timezone
and offset (date '+%Z %z') and what the date style ought to be. If you
can suggest a change to the script that will get it right without breaking 
results for other countries, that will be even better!

Please do NOT tell me if it is RIGHT or I will be overwhelmed with mail!

If anyone wants to see the result for all timezones, this Bourne shell
fragment will do it:

 (cd /usr/share/zoneinfo;
 for TZ in `find * -type f ! -name '*.tab' ! -name localtime`
    do echo -en `date '+%Z %z'`  \\t $TZ \\t
    /tmp/guess.datestyle
 done |
    awk '{printf "%7s %5s %40s %2s %s %s\n",$1, $2, $3, $4, $5, $6}' | 
    less)

#! /bin/sh
# Guess the postgresql datestyle to use for a given timezone (from glibc)

# make sure we can find the timezone database
ZONEDIR=/usr/share/zoneinfo
if [ ! -d "$ZONEDIR" ]
then
	echo "Sorry, I don't know where to find the timezone files; this
script expected to find them in $ZONEDIR"
	exit 1
fi

# make sure TZ is set and exported
if [ -z "$TZ" ]
then
	TZ=`date '+%Z'`
fi
export TZ

# find the timezone offset from UTC
tzno=`date '+%z'`

# Find the name of the zone - strip off unwanted header directories
x=`echo $TZ | cut -d/ -f1`
case $x in ( SystemV | posix | right )
	x=`echo $TZ | cut -d/ -f2-4`
	;;
	* )
		x=$TZ
		;;
esac

# What country does this zone belong to (if undefined, set it to __)
tzcountry=`grep $x $ZONEDIR/zone.tab | head -1 | cut -c1-2` 
if [ -z "$tzcountry" ]
then
	tzcountry=__
fi

# Guess the timezone
case $tzcountry in ( US | CA )
		# US date format
		# (I assume Canada uses US format)
		GuessDateStyle=US
		;;
	DE )
		# Germany has a style to itself
		GuessDateStyle=GERMAN
		;;
	__ )
		# No country, so see if the zone is a region or country name
		x=$TZ
		while echo $x | grep -q /
		do
			y=`dirname $x`
			case $y in ( SystemV | posix | right )
				y=`basename $x`
				;;
			esac
			x=$y
		done

		case $x in ( Africa | Canada | Indian | Mexico | America | Chile | GB | Iran | Mideast | Antarctica | Cuba | Israel | NZ | Singapore | Arctic | Jamaica | Asia | Japan | Navajo | Turkey | Atlantic | Kwajalein | Australia | Egypt | Libya | US | Brazil | Eire | Pacific | Hongkong | Poland | Europe | Iceland | Portugal)
			# try to guess the zone from the UTC offset and the
			# zone abbreviation
			if [ -z "$GuessDateStyle" ]
			then
				GuessDateStyle=EURO
                                # if the user has German locale, I assume
                                # he will want GERMAN style
				if [ "$LANG" = de_DE ]
				then
					GuessDateStyle=GERMAN
				elif [ $tzno -le -200 -a $tzno -gt -1200 ]
				then
					tzn=`date '+%Z'`
					case $tzn in ( ADT | AST | AKDT | AKST | CDT | CST | EDT | EST | HADT | HAST | HST | MDT | MST | NDT | PDT | PST)
						GuessDateStyle=US
						;;
					esac
				fi
			fi
			;;
		* )
			# Not a country or region, so use ISO
			GuessDateStyle=ISO
			;;
		esac
		;;
	* )
		# The rest of the world uses normal European format
		GuessDateStyle=EURO
		;;
esac
echo $tzcountry $GuessDateStyle
exit 0

# The End
      Vote against SPAM: http://www.politik-digital.de/spam/
                 ========================================
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
               PGP key from public servers; key ID 32B8FAA1
                 ========================================
     "Honor the LORD with thy substance, and with the  
      firstfruits of all thine increase; So shall thy barns 
      be filled with plenty, and thy presses shall burst out
      with new wine."      Proverbs 3:9,10 

Reply to: