File: tzwatch

package info (click to toggle)
gworldclock 1.4.4-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,168 kB
  • ctags: 169
  • sloc: sh: 4,086; ansic: 2,233; makefile: 71; sed: 16
file content (156 lines) | stat: -rwxr-xr-x 4,982 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash

# tzwatch

# Displays the time in a number of time zones, specified by the user.
# The list of time zones (format as TZ variable) is kept in ${HOME}/.tzlist
# The time zone is chosen using tzselect 
# (Debian default is /usr/bin/tzselect, in package libc6).
# ( ${HOME}/.tzlist could be edited by hand, with a valid value for the TZ variable on each line)

# Command line options:
#    -c, configure time zones (add or remove)
#    -f FORMAT  display time according to FORMAT (see 'date' command)
# If no command line options are given, the time in each zone at the
# given moment is printed.

# Things that could be done:
# Possibly this would be better done in perl.  Too bad.
# A window-graphics display could be nice too.
# Add an option -r [n] to  run continuously (display updated every n seconds)

# This program is considered copyrighted and distributable under the terms of the GPL, which I will not bother copying here.

# Drew Parsons <dparsons@debian.org>
# Version 1.2.3 14 July 2002
#  only refer to timezone label for existing zones when configuring
# Version 1.2.2, 20 Jan 2002
#   change "timezone" to "time zone".  tzselect is now in package libc6.
# Version 1.2.1, May 2000
#   made recognition of timezone "Local" case-insensitive
# Version 1.2, March 2000
#   configfile renamed to .tzlist (for compatibility with gworldclock), 'local' zone added
# Version 1.1, [2] January 2000 (should that be 1.0.1 ?? )
#   tzselect gone missing in potato.  Located in slink's timezones package.
# Version 1.0, 29 November 1999
#   Original version


# default config file
TZWATCH=${HOME}/.tzlist

if [ "$1" == "--help" ]; then
    echo "Usage: tzwatch [-c] [-f FORMAT]"
    echo "  -c          configure time zones (add or remove)"
    echo "  -f FORMAT   display time according to FORMAT (see 'date' command)"
    exit 0;
fi


# set config file if not yet done
if [ ! -s $TZWATCH ]; then
    if [ `type -p tzselect` ]; then
	tzselect > $TZWATCH
    else
	echo tzselect not found.  Cannot configure tzwatch.
	exit 1
    fi
fi

while getopts "cf:" OPT; do
    case $OPT in
    c) # configure: add or remove entry
	choice=
	while [ "$choice" != "Done" ]; do
	    echo Choose zone to remove or add new zone:
	    select choice in \
		`awk  '// {print $1}' $TZWATCH` 'Add Zone' 'Add Local Time' 'Done' 
	    do
		case $choice in
		'')  
		    echo 'Please enter a number in range.';;
		'Add Zone')
		    if [ `type -p tzselect` ]; then
			tzselect >> $TZWATCH
		    else
			echo tzselect not found.  Cannot add time zone.
		    fi
# there ought to something we can do here to make sure the zone has not been already be chosen, sort lines and removes duplicates or whatever
		    echo
		    break;;

		'Add Local Time')
		    if [ `type -p tzselect` ]; then
			echo Local >> $TZWATCH
		    else
			echo tzselect not found.  Cannot add local time.
		    fi
		    break;;

		'Done')
		    break;;
		?*)
#swapping '/' in zone name to '\/' for sed.  I can't believe how many darn  \\\\\'s you need!
		    debarredzone=`echo $choice | sed 's|/|\\\/|'`
		    sed -e "\|$debarredzone|d" $TZWATCH | cat > $TZWATCH
		    break;;
		esac
	    done
	done;;
    f) #format string for date output
	DATEFORMAT=$OPTARG
	# if the given format has no leading +, then add one
        if [ -z "$(echo $DATEFORMAT |
	       sed "{s/^[\'\"]//g; s/[\'\"]$//g}" |  # strip surrounding quotemarks
	       awk /^\\+/)" ]; then   # check for leading +
	   DATEFORMAT=+$DATEFORMAT
	fi;;
    ?*)
	 echo Usage: tzwatch [-c] [-f FORMAT]
	 echo -e "Try \`tzwatch --help' for more information."
	 exit 0;;
    esac	
done


##################################################################3
# finally, display times
# zdump could be used here, but then '-f' formatting would be lost.  
# Not that I use date formatting, but it's the principle of the matter.

# wc always prints file name.  How annoying.  awk it away.
NZ=`wc -l $TZWATCH | awk '{print $1}'`
n=1
until [ $(( $n )) -gt $(( $NZ )) ]; do

#grab zone from config file
    zoneRaw=`awk -v nn=$n 'NR==nn { print $1}' $TZWATCH`
# allow "local" time zone to be in any case, "Local" or "local" or whatever
    zone=`echo $zoneRaw | sed s/local/Local/I`


    # Create time string.
    # Sorry about the doubled date calls, it was the only way I could
    # get the format string handled correctly (without providing a default 
    # format string myself in the case where the user does not give one)
    if [ -z "$DATEFORMAT" ]; then
        if [ "x$zone" == "xLocal" ]; then
	    TZdate=$(LANG=C date)
        else
	    TZdate=$(LANG=C TZ="$zone" date)
        fi
    else
        if [ "x$zone" == "xLocal" ]; then
	    TZdate=$(LANG=C date "$DATEFORMAT")
        else
	    TZdate=$(LANG=C TZ="$zone" date "$DATEFORMAT")
        fi
    fi

# and display 
# I'd want the zone on the left, but then the date strings would be misaligned
    echo "$TZdate   $zone"

# why doesn't '+=' work for incrementing n, like in C?
    n=$(( $n + 1 ))
done