xref: /illumos-gate/usr/src/contrib/ast/src/cmd/ksh93/tests/timetype.sh (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2011 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
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 != Time_t -- expected '$d', got '$t'"
51(( t == s )) || err_exit "numeric Time_t failed -- expected '$s', got '$t'"
52t._='%#'
53[[ $t == $s ]] || err_exit "t._='%#' failed -- expected '$s', got '$t'"
54unset t
55Time_t tt=(yesterday today tomorrow)
56tt[3]=2pm
57[[ ${!tt[@]} == '0 1 2 3' ]] || err_exit "indexed array subscript names failed -- expected '0 1 2 3', got '${!tt[@]}'"
58[[ ${tt[0]} == *+00:00 ]] || err_exit "tt[0] failed -- expected 00:00, got '${tt[0]##*+}'"
59[[ ${tt[1]} == *+00:00 ]] || err_exit "tt[1] failed -- expected 00:00, got '${tt[1]##*+}'"
60[[ ${tt[2]} == *+00:00 ]] || err_exit "tt[2] failed -- expected 00:00, got '${tt[2]##*+}'"
61[[ ${tt[3]} == *+14:00 ]] || err_exit "tt[3] failed -- expected 14:00, got '${tt[3]##*+}'"
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='$((tt[1] - tt[0]))' != 1 day"
67(( (tt[2] - tt[1]) == 24*3600 )) || err_exit "tomorrow-today='$((tt[2] - tt[1]))' != 1 day"
68(( (tt[4] - tt[3]) ==  8*3600 )) || err_exit "9am..5pm='$((tt[4] - tt[3]))' != 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] failed -- expected 00:00, got '${tt[yesterday]##*+}'"
73[[ ${tt[today]} == *+00:00 ]] || err_exit "tt[today] failed -- expected 00:00, got '${tt[today]##*+}'"
74[[ ${tt[tomorrow]} == *+00:00 ]] || err_exit "tt[tomorrow] failed -- expected 00:00, got '${tt[tomorrow]##*+}'"
75[[ ${tt[2pm]} == *+14:00 ]] || err_exit "tt[2pm] failed -- expected 14:00, got '${tt[2pm]##*+}'"
76(( (tt[today] - tt[yesterday] ) == 24*3600 )) || err_exit "tt[today]-tt[yesterday] failed -- expected 24*3600, got $(((tt[today]-tt[yesterday])/3600.0))*3600"
77(( (tt[tomorrow] - tt[today] ) == 24*3600 )) || err_exit "tt[tomorrow]-tt[today] failed -- expected 24*3600, got $(((tt[tomorrow]-tt[today])/3600.0))*3600"
78(( (tt[2pm] - tt[today] ) == 14*3600 )) || err_exit "tt[2pm]-tt[today] failed -- expected 14*3600, got $(((tt[2pm]-tt[today])/3600.0))*3600"
79unset tt
80
81exit $((Errors<125?Errors:125))
82