xref: /freebsd/bin/date/tests/format_string_test.sh (revision e63d20b70ee1dbee9b075f29de6f30cdcfe1abe1)
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
33atf_test_case flag_r_file_test
34flag_r_file_test_body()
35{
36	local file
37
38	file="./testfile"
39	touch "$file"
40	atf_check -o "inline:$(stat -f '%9Fm' "$file")\n" \
41		date -r "$file" +%s.%N
42}
43
44format_string_test()
45{
46	local desc exp_output_1 exp_output_2 flag
47
48	desc=${1}
49	flag=${2}
50	exp_output_1=${3}
51	exp_output_2=${4}
52
53	atf_test_case ${desc}_test
54	eval "
55${desc}_test_body() {
56	check ${flag} '${exp_output_1}' '${exp_output_2}';
57}"
58	atf_add_test_case ${desc}_test
59}
60
61iso8601_check()
62{
63	local arg flags exp_output_1 exp_output_2
64
65	arg="${1}"
66	flags="${2}"
67	exp_output_1="${3}"
68	exp_output_2="${4}"
69
70	atf_check -o "inline:${exp_output_1}\n" \
71	    date $flags -r ${TEST1} "-I${arg}"
72	atf_check -o "inline:${exp_output_2}\n" \
73	    date $flags -r ${TEST2} "-I${arg}"
74}
75
76iso8601_string_test()
77{
78	local desc arg exp_output_1 exp_output_2 flags
79
80	desc="${1}"
81	arg="${2}"
82	flags="${3}"
83	exp_output_1="${4}"
84	exp_output_2="${5}"
85
86	atf_test_case iso8601_${desc}_test
87	eval "
88iso8601_${desc}_test_body() {
89	iso8601_check '${arg}' '${flags}' '${exp_output_1}' '${exp_output_2}'
90}"
91	atf_add_test_case iso8601_${desc}_test
92
93	if [ -z "$flags" ]; then
94	    atf_test_case iso8601_${desc}_parity
95	    eval "
96iso8601_${desc}_parity_body() {
97	local exp1 exp2
98
99	atf_require_prog gdate
100
101	exp1=\"\$(gdate --date '@${TEST1}' '-I${arg}')\"
102	exp2=\"\$(gdate --date '@${TEST2}' '-I${arg}')\"
103
104	iso8601_check '${arg}' '' \"\${exp1}\" \"\${exp2}\"
105}"
106	    atf_add_test_case iso8601_${desc}_parity
107	fi
108}
109
110atf_init_test_cases()
111{
112	atf_add_test_case flag_r_file_test
113
114	format_string_test A A Saturday Monday
115	format_string_test a a Sat Mon
116	format_string_test B B February November
117	format_string_test b b Feb Nov
118	format_string_test C C 19 20
119	format_string_test c c "Sat Feb  7 07:04:03 1970" "Mon Nov 12 21:20:00 2001"
120	format_string_test D D 02/07/70 11/12/01
121	format_string_test d d 07 12
122	format_string_test e e " 7" 12
123	format_string_test F F "1970-02-07" "2001-11-12"
124	format_string_test G G 1970 2001
125	format_string_test g g 70 01
126	format_string_test H H 07 21
127	format_string_test h h Feb Nov
128	format_string_test I I 07 09
129	format_string_test j j 038 316
130	format_string_test k k " 7" 21
131	format_string_test l l " 7" " 9"
132	format_string_test M M 04 20
133	format_string_test m m 02 11
134	format_string_test N N 000000000 000000000
135	format_string_test p p AM PM
136	format_string_test R R 07:04 21:20
137	format_string_test r r "07:04:03 AM" "09:20:00 PM"
138	format_string_test S S 03 00
139	format_string_test s s ${TEST1} ${TEST2}
140	format_string_test U U 05 45
141	format_string_test u u 6 1
142	format_string_test V V 06 46
143	format_string_test v v " 7-Feb-1970" "12-Nov-2001"
144	format_string_test W W 05 46
145	format_string_test w w 6 1
146	format_string_test X X "07:04:03" "21:20:00"
147	format_string_test x x "02/07/70" "11/12/01"
148	format_string_test Y Y 1970 2001
149	format_string_test y y 70 01
150	format_string_test Z Z UTC UTC
151	format_string_test z z +0000 +0000
152	format_string_test percent % % %
153	format_string_test plus + "Sat Feb  7 07:04:03 UTC 1970" "Mon Nov 12 21:20:00 UTC 2001"
154
155	iso8601_string_test default "" "" "1970-02-07" "2001-11-12"
156	iso8601_string_test date date "" "1970-02-07" "2001-11-12"
157	iso8601_string_test hours hours "" "1970-02-07T07+00:00" "2001-11-12T21+00:00"
158	iso8601_string_test minutes minutes "" "1970-02-07T07:04+00:00" "2001-11-12T21:20+00:00"
159	iso8601_string_test seconds seconds "" "1970-02-07T07:04:03+00:00" "2001-11-12T21:20:00+00:00"
160	iso8601_string_test ns ns "" "1970-02-07T07:04:03,000000000+00:00" "2001-11-12T21:20:00,000000000+00:00"
161}
162