xref: /freebsd/bin/date/tests/format_string_test.sh (revision 5c1d97100348ef19878fa14671a9b70f3d963ed4)
1#
2# Regression tests for date(1)
3#
4# Submitted by Edwin Groothuis <edwin@FreeBSD.org>
5#
6# $FreeBSD$
7#
8
9#
10# These two date/times have been chosen carefully -- they
11# create both the single digit and double/multidigit version of
12# the values.
13#
14# To create a new one, make sure you are using the UTC timezone!
15#
16
17TEST1=3222243		# 1970-02-07 07:04:03
18TEST2=1005600000	# 2001-11-12 21:11:12
19
20check()
21{
22	local format_string exp_output_1 exp_output_2
23
24	format_string=${1}
25	exp_output_1=${2}
26	exp_output_2=${3}
27
28	atf_check -o "inline:${exp_output_1}\n" \
29	    date -r ${TEST1} +%${format_string}
30	atf_check -o "inline:${exp_output_2}\n" \
31	    date -r ${TEST2} +%${format_string}
32}
33
34format_string_test()
35{
36	local desc exp_output_1 exp_output_2 flag
37
38	desc=${1}
39	flag=${2}
40	exp_output_1=${3}
41	exp_output_2=${4}
42
43	atf_test_case ${desc}_test
44	eval "
45${desc}_test_body() {
46	check ${flag} '${exp_output_1}' '${exp_output_2}';
47}"
48	atf_add_test_case ${desc}_test
49}
50
51iso8601_check()
52{
53	local arg flags exp_output_1 exp_output_2
54
55	arg="${1}"
56	flags="${2}"
57	exp_output_1="${3}"
58	exp_output_2="${4}"
59
60	atf_check -o "inline:${exp_output_1}\n" \
61	    date $flags -r ${TEST1} "-I${arg}"
62	atf_check -o "inline:${exp_output_2}\n" \
63	    date $flags -r ${TEST2} "-I${arg}"
64}
65
66iso8601_string_test()
67{
68	local desc arg exp_output_1 exp_output_2 flags
69
70	desc="${1}"
71	arg="${2}"
72	flags="${3}"
73	exp_output_1="${4}"
74	exp_output_2="${5}"
75
76	atf_test_case iso8601_${desc}_test
77	eval "
78iso8601_${desc}_test_body() {
79	iso8601_check '${arg}' '${flags}' '${exp_output_1}' '${exp_output_2}'
80}"
81	atf_add_test_case iso8601_${desc}_test
82
83	if [ -z "$flags" ]; then
84	    atf_test_case iso8601_${desc}_parity
85	    eval "
86iso8601_${desc}_parity_body() {
87	local exp1 exp2
88
89	atf_require_prog gdate
90
91	exp1=\"\$(gdate --date '@${TEST1}' '-I${arg}')\"
92	exp2=\"\$(gdate --date '@${TEST2}' '-I${arg}')\"
93
94	iso8601_check '${arg}' '' \"\${exp1}\" \"\${exp2}\"
95}"
96	    atf_add_test_case iso8601_${desc}_parity
97	fi
98}
99
100atf_init_test_cases()
101{
102	format_string_test A A Saturday Monday
103	format_string_test a a Sat Mon
104	format_string_test B B February November
105	format_string_test b b Feb Nov
106	format_string_test C C 19 20
107	format_string_test c c "Sat Feb  7 07:04:03 1970" "Mon Nov 12 21:20:00 2001"
108	format_string_test D D 02/07/70 11/12/01
109	format_string_test d d 07 12
110	format_string_test e e " 7" 12
111	format_string_test F F "1970-02-07" "2001-11-12"
112	format_string_test G G 1970 2001
113	format_string_test g g 70 01
114	format_string_test H H 07 21
115	format_string_test h h Feb Nov
116	format_string_test I I 07 09
117	format_string_test j j 038 316
118	format_string_test k k " 7" 21
119	format_string_test l l " 7" " 9"
120	format_string_test M M 04 20
121	format_string_test m m 02 11
122	format_string_test p p AM PM
123	format_string_test R R 07:04 21:20
124	format_string_test r r "07:04:03 AM" "09:20:00 PM"
125	format_string_test S S 03 00
126	format_string_test s s ${TEST1} ${TEST2}
127	format_string_test U U 05 45
128	format_string_test u u 6 1
129	format_string_test V V 06 46
130	format_string_test v v " 7-Feb-1970" "12-Nov-2001"
131	format_string_test W W 05 46
132	format_string_test w w 6 1
133	format_string_test X X "07:04:03" "21:20:00"
134	format_string_test x x "02/07/70" "11/12/01"
135	format_string_test Y Y 1970 2001
136	format_string_test y y 70 01
137	format_string_test Z Z UTC UTC
138	format_string_test z z +0000 +0000
139	format_string_test percent % % %
140	format_string_test plus + "Sat Feb  7 07:04:03 UTC 1970" "Mon Nov 12 21:20:00 UTC 2001"
141
142	iso8601_string_test default "" "" "1970-02-07" "2001-11-12"
143	iso8601_string_test date date "" "1970-02-07" "2001-11-12"
144	iso8601_string_test hours hours "" "1970-02-07T07+00:00" "2001-11-12T21+00:00"
145	iso8601_string_test minutes minutes "" "1970-02-07T07:04+00:00" "2001-11-12T21:20+00:00"
146	iso8601_string_test seconds seconds "" "1970-02-07T07:04:03+00:00" "2001-11-12T21:20:00+00:00"
147	# BSD date(1) does not support fractional seconds at this time.
148	#iso8601_string_test ns ns "" "1970-02-07T07:04:03,000000000+00:00" "2001-11-12T21:20:00,000000000+00:00"
149}
150