1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	(( Errors+=1 ))
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31typeset -T Time_t=(
32	integer .=-1
33	_='%F+%H:%M'
34	get()
35	{
36		if      (( _ < 0 ))
37		then	.sh.value=${ printf "%(${_._})T" now ;}
38		else	.sh.value=${ printf "%(${_._})T" "#$((_))" ;}
39		fi
40	}
41	set()
42	{
43		.sh.value=${ printf "%(%#)T" "${.sh.value}";}
44	}
45)
46
47d=$(printf "%(%F+%H:%M)T" now)
48integer s=$(printf "%(%#)T" "$d")
49Time_t t=$d
50[[ $t == "$d" ]] || err_exit 'printf %T and Time_t are different'
51(( t == s )) || err_exit 'numerical  Time_t not correct'
52t._='%#'
53[[ $t == $s ]] || err_exit 'setting _ to %# not getting correct results'
54unset t
55Time_t tt=(yesterday today tomorrow)
56tt[3]=2pm
57[[ ${!tt[@]} == '0 1 2 3' ]] || err_exit 'indexed array subscript names not correct'
58[[ ${tt[0]} == *+00:00 ]] || err_exit 'tt[0] is not yesterday'
59[[ ${tt[1]} == *+00:00 ]] || err_exit 'tt[1] is not today'
60[[ ${tt[2]} == *+00:00 ]] || err_exit 'tt[2] is not tomorrow'
61[[ ${tt[3]} == *+14:00 ]] || err_exit 'tt[0] is not 2pm'
62unset tt
63Time_t tt=('2008-08-11+00:00:00,yesterday' '2008-08-11+00:00:00,today' '2008-08-11+00:00:00,tomorrow')
64tt[3]=9am
65tt[4]=5pm
66(( (tt[1] - tt[0] ) == 24*3600 )) || err_exit  'today-yesterday not one day'
67(( (tt[2] - tt[1] ) == 24*3600 )) || err_exit  'tomorrow-today not one day'
68(( (tt[4] - tt[3] ) == 8*3600 )) || err_exit  '9am .. 5pm is not 8 hours'
69unset tt
70Time_t tt=([yesterday]='2008-08-11+00:00:00,yesterday' [today]='2008-08-11+00:00:00,today' [tomorrow]='2008-08-11+00:00:00,tomorrow')
71tt[2pm]='2008-08-11+00:00:00,2pm'
72[[ ${tt[yesterday]} == *+00:00 ]] || err_exit 'tt[yesterday] is not yesterday'
73[[ ${tt[today]} == *+00:00 ]] || err_exit 'tt[today] is not today'
74[[ ${tt[tomorrow]} == *+00:00 ]] || err_exit 'tt[tomorrow] is not tomorrow'
75[[ ${tt[2pm]} == *+14:00 ]] || err_exit 'tt[2pm] is not 2pm'
76(( (tt[today] - tt[yesterday] ) == 24*3600 )) || err_exit  'today-yesterday not one day'
77(( (tt[tomorrow] - tt[today] ) == 24*3600 )) || err_exit  'tomorrow-today not one day'
78(( (tt[2pm] - tt[today] ) == 14*3600 )) || err_exit  '2pm is not 14 hours'
79unset tt
80exit $Errors
81