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