#!/bin/zsh -f ###################################################################### {{{1 # # timez # # This shell script just spits out date/times for different timezones. # # Written & Copyright by Christian J. Robinson # sometime before the date of April 01, 1999 - You may copy/use this under # the terms of the GNU General Public License, version 2 or later. # # RCS: $Id: timez,v 2.6 2002/04/08 08:20:53 infynity Exp $ # # TODO: # - MUCH more commentary. # - Check to see if a zone is valid somehow. # ###################################################################### }}}1 # Default zones, define these as you wish: # {{{1 # (Check /usr/lib/zoneinfo | /usr/share/zoneinfo) # A zone of "-" produces a separator in the output. zones=( "Zulu" "-" "Australia/Perth" "Australia/Adelaide" "Australia/Brisbane" "Australia/Melbourne" "Australia/Queensland" "Australia/Sydney" "-" "US/Hawaii" "US/Pacific" "America/Phoenix" "US/Mountain" "US/Central" "US/Eastern" "-" "Europe/London" "Europe/Amsterdam" "Asia/Gaza" "Asia/Manila" "Hongkong" ) # }}}1 # You shouldn't be messing with stuff below this line. # Functions: ######################################################### {{{1 main() # {{{2 { local i output hline sep top bot nlines tlines hline=$(rpt $HLINE 56) sep="$BLD$LTEE$hline$RTEE$RST" bot="$BLD$BLCORNER$hline$BRCORNER$RST" top="$BLD$TLCORNER$hline$TRCORNER$RST"; tlines=1 if [[ -z $opt_n ]] then top="$top\n$BLD$VLINE Location: Zone: Date: Time: $VLINE$RST" top="$top\n$sep" tlines=3 fi output="$top\n"; nlines=$tlines for i in $zones # {{{3 do if [[ "$i" == "-" ]] then output="${output}$sep\n" ((++nlines)) else output="${output}$(scriptdate $i)\n" ((++nlines)) fi if [[ $nlines -ge (( LINES - 2 )) && ! -z $opt_p ]] then echo "$output$bot" read -k \?"Press any key to continue: " output="\r$top\n"; nlines=$tlines fi done # }}}3 echo "$output$bot" } simple_main() # {{{2 { for i in $zones do if [[ "$i" == "-" ]] then echo "------------------------------------------------------------" else echo "$i: \c" #TZ=$i date export TZ=$i print -P '%D{%a %b %e %T %Z %Y}' fi done } getargs () # {{{2 { local arg tmp newargv # Magic going on here: for arg in $argv # {{{3 do if [[ $arg == -- ]] then newargv=( $newargv $argv ) break elif [[ $arg == -[^-]?* ]] then # Break the argument to component characters. tmp=( ${(s::)arg[2,-1]} ) # Insert -'s in front of each character and make each element # part of newargv: newargv=( $newargv -${(j: -:)=tmp} ) else newargv=( $newargv $arg ) fi shift argv done # }}}3 # Now that the arguments have been munged about, throw them back # into argv for the next stage: argv=( $newargv ); newargv=() until [[ -z "$argv" ]] # {{{3 do arg=$argv[1] if [[ "$arg[1]" != '-' ]] then newargv=($newargv $arg) shift argv continue fi case $arg in # {{{4 -c) opt_c=1 ;; --color) opt_color=1 ;; --nocolor) opt_nocolor=1 ;; --nolines|-l) opt_l=1 ;; --noheader|-n) opt_n=1 ;; --page|-p) opt_p=1 ;; --simple|-s) opt_s=1 ;; --zonefile|-z) shift argv ZONEFILE=$argv[1] ;; --help|-h) usage 1 exit 0 ;; --version|-v) version exit 0 ;; --) shift argv; break ;; *) echo "Unknown option: $arg" >&2 usage 2 exit 1 ;; esac # }}}4 [[ ! -z "$argv" ]] && shift argv done # }}}3 argv=( $newargv $argv ) if [[ -n $argv ]] then zones=( $argv ) ZONEFILE="" fi } curses_init () # {{{2 { local i j acsc smacs rmacs term_colors=$(echotc Co 2>/dev/null) # {{{3 if [[ $term_colors -gt 0 ]] then colors=1 else colors=0 fi #}}}3 if [[ -n $opt_color || ( $colors == 0 && -n $opt_c ) ]] # {{{3 then colors=1 elif [[ -n $opt_nocolor || ( $colors == 1 && -n $opt_c ) ]] then colors=0 fi # }}}3 if [[ $colors == 1 && $term_colors -le 0 ]] # {{{3 then # User 'forced' colors, but echotc says we have no colors, # so we try for standard ANSI. RST="\e[0m" BLK="\e[30m" RED="\e[31m" GRN="\e[32m" YEL="\e[33m" BLU="\e[34m" MAG="\e[35m" CYA="\e[36m" WHT="\e[37m" BLD="\e[1m" BLINK="\e[5m" UNDERLINE="\e[4m" revBLK="\e[40m" revRED="\e[41m" revGRN="\e[42m" revYEL="\e[43m" revBLU="\e[44m" revMAG="\e[45m" revCYA="\e[46m" revWHT="\e[47m" elif [[ $colors == 1 ]] then # Apparently the terminal is capable of color, # so use 'echotc' to find out how to set them. RST=$(echotc me 2>/dev/null) BLK=$(echotc AF 0 2>/dev/null) RED=$(echotc AF 1 2>/dev/null) GRN=$(echotc AF 2 2>/dev/null) YEL=$(echotc AF 3 2>/dev/null) BLU=$(echotc AF 4 2>/dev/null) MAG=$(echotc AF 5 2>/dev/null) CYA=$(echotc AF 6 2>/dev/null) WHT=$(echotc AF 7 2>/dev/null) BLD=$(echotc md 2>/dev/null) BLINK=$(echotc mb 2>/dev/null) UNDERLINE=$(echotc us 2>/dev/null) revBLK=$(echotc AB 0 2>/dev/null) revRED=$(echotc AB 1 2>/dev/null) revGRN=$(echotc AB 2 2>/dev/null) revYEL=$(echotc AB 3 2>/dev/null) revBLU=$(echotc AB 4 2>/dev/null) revMAG=$(echotc AB 5 2>/dev/null) revCYA=$(echotc AB 6 2>/dev/null) revWHT=$(echotc AB 7 2>/dev/null) fi # }}}3 if [[ -z $opt_l ]] # {{{3 then smacs=$(echotc as 2>/dev/null) rmacs=$(echotc ae 2>/dev/null) acsc=$(echotc ac 2>/dev/null) let i=1; let j=0 while [[ -n $acsc[$i] ]] # {{{4 do case $acsc[$i] in j) BRCORNER="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; k) TRCORNER="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; l) TLCORNER="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; m) BLCORNER="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; n) CTEE="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; q) HLINE="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; t) LTEE="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; u) RTEE="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; v) BTEE="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; w) TTEE="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; x) VLINE="$smacs$acsc[$i + 1]$rmacs"; ((++j)) ;; esac ((i+=2)) done # }}}4 fi # }}}3 if [[ $j != 11 ]] # {{{3 then BRCORNER="\x27" TRCORNER='.' TLCORNER='.' BLCORNER='\x60' CTEE='+' HLINE='-' LTEE='|' RTEE='|' BTEE='-' TTEE='-' VLINE='|' fi # }}}3 } # center() args: {{{2 # 1 - What to pad. # 2 - Pad to length n. # 3 - Left padding character. (Optional.) # 4 - Right padding character. (Optional.) # 5 - What to insert between both sides of arg1 and the padding. # (Optional.) # # Args 3, 4, and 5 can be multiple characters, but they are assumed to be # "one" character. If they're more than that, you'd better know what # you're doing. center() { local len half odd extra left right left=' '; right=' ' [[ ! -z $3 ]] && left=$3 [[ ! -z $4 ]] && right=$4 let len=$(($2-${#1})) if [[ $len < 1 ]] # {{{3 then echo $1 return fi # }}}3 let half=$(($len/2)) let odd=$(($len%2)) [[ $odd != 0 ]] && extra="$right" if [[ -z $5 ]] # {{{3 then echo "$(rpt $left $half)$1$(rpt $right $half)$extra" elif [[ $half == 1 ]] then echo "$5$1$5$extra" elif [[ $half == 0 && ! -z $extra ]] then if [[ ! -z $5 ]] # {{{4 then echo "$1$5" else echo "$1$extra" fi # }}}4 else echo "$(rpt $left $(($half-1)))$5$1$5$(rpt $right $(($half-1)))$extra" fi # }}}3 } rpt() # {{{2 { local i result let i=0 while [[ $i -lt $2 ]] # {{{3 do result="$result$1" ((++i)) done # }}}3 echo "$result" } scriptdate() # {{{2 { local tmp1 tmp2 tmp3 mytz mytz_short time hour set -A tmp1 ${(s:/:)1} if [[ -n ${tmp1[2]} ]] # {{{3 then typeset -R 10 tmp2=${tmp1[1]} tmp3=${(j:/:)tmp1[2,-1]} tmp3="$tmp3 $(rpt . $((21 - $#tmp3 )))" mytz="$tmp2/$tmp3" else mytz=$(center $1 33 " " . " ") fi # }}}3 #set -A time $(TZ=$1 date "+%a %d %l %M %p %Z") export TZ=$1 set -A time $(print -P '%D{%a %d %l %M %p %Z}') typeset -R 2 hour=${time[3]} typeset -R 4 mytz_short=${time[6]} echo "$BLD$VLINE$RST $BLD$GRN$mytz$RST $BLD$MAG$mytz_short$RST \c" echo "$BLD$YEL${time[1]}$RST $BLD$RED${time[2]}$RST \c" echo "$BLD$BLU$hour:${time[4]}$RST $BLD$CYA${time[5]}$RST $BLD$VLINE$RST" } usage() # {{{2 { cat >&$1 < Use "zonefile" instead of ~/.timez-zones -h, --help Print this statement and exit. -v, --version Print the version and exit. The script will print color if your terminal is capable of color. The -c option just reverses this behavior. (Forces color on non-color capable terminals & suppresses color on color capable terminals.) It will also print line glyphs if the terminal is capable of doing so. The -l or --nolines option turns this off. There is no way to force this option on. A zone of "-" produces a separator line in the output. (Be sure to end your command line options with a "--" first.) Invalid zones will produce unexpected results. Check /usr/share/zoneinfo for timezones. The default for the zones are set internally, then zones are read from ~/.timez-zones or the file specified with -z if it exists. If zones are specified on the command line, the -z argument has no effect. EOF } version() # {{{2 { echo "$BASENAME version $VERSION" } # }}}2 # End Functions. ##################################################### }}}1 # Initialize: ######################################################## {{{1 BASENAME="${0##*/}" ZONEFILE="$HOME/.timez-zones" RCS_VERSION='$Revision: 2.6 $' VERSION=${${=RCS_VERSION}[-2]} # Run before printing "Please wait..." or reading the zonefile: getargs $@ if [[ ! -z "$ZONEFILE" && -r "$ZONEFILE" ]] then zones=( "${(f)$(<$ZONEFILE)}" ) elif [[ ! -z "$ZONEFILE" && "$ZONEFILE" != "$HOME/.timez-zones" ]] then echo "$ZONEFILE doesn't exist or isn't readable." >&2 if [[ -r "$HOME/.timez-zones" ]] then echo "Defaulting to ~/.timez-zones." >&2 zones=( "${(f)$(<$HOME/.timez-zones)}" ) else echo "Defaulting to builtin zones." >&2 fi fi if [[ $opt_s != 1 ]] then [[ -t 1 ]] && echo "Please wait...\r\c" curses_init # Initialize colors and boxing. fi # End Initialize. #################################################### }}}1 # Run the body of the program: [[ $opt_s != 1 ]] && main || simple_main # vim: set ts=2 sw=2 ai nu tw=75 fo=croq2: # vim600:fdm=marker:fdc=5:fdl=1:cms=\ #\ %s: