ls: tune is_colored
[coreutils.git] / tests / date / date-debug.sh
blob061ef59c97d0b40679a2f48af9a9525d307e62e3
1 #!/bin/sh
2 # Test 'date --debug' option.
4 # Copyright (C) 2016-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ date
22 export LC_ALL=C
24 ## Ensure timezones are supported.
25 ## (NOTE: America/Belize timezone does not change on DST)
26 test "$(TZ=America/Belize date +%z)" = '-0600' \
27 || skip_ 'Timezones database not found'
29 date --debug >/dev/null 2>d_t_fmt.err || fail=1
30 d_t_fmt=$(sed -n "s/.*'\(.*\)'$/\1/p" < d_t_fmt.err) || framework_failure_
31 test -n "$d_t_fmt" || fail=1
34 ## Test 1: complex date string
36 in1='TZ="Asia/Tokyo" Sun, 90-12-11 + 3 days - 90 minutes'
38 cat<<EOF>exp1
39 date: parsed day part: Sun (day ordinal=0 number=0)
40 date: parsed date part: (Y-M-D) 0090-12-11
41 date: parsed relative part: +3 day(s)
42 date: parsed relative part: +3 day(s) -90 minutes
43 date: input timezone: TZ="Asia/Tokyo" in date string
44 date: warning: adjusting year value 90 to 1990
45 date: warning: using midnight as starting time: 00:00:00
46 date: warning: day (Sun) ignored when explicit dates are given
47 date: starting date/time: '(Y-M-D) 1990-12-11 00:00:00'
48 date: warning: when adding relative days, it is recommended to specify noon
49 date: after date adjustment (+0 years, +0 months, +3 days),
50 date: new date/time = '(Y-M-D) 1990-12-14 00:00:00'
51 date: '(Y-M-D) 1990-12-14 00:00:00' = 661100400 epoch-seconds
52 date: after time adjustment (+0 hours, -90 minutes, +0 seconds, +0 ns),
53 date: new time = 661095000 epoch-seconds
54 date: timezone: TZ="Asia/Tokyo" environment value
55 date: final: 661095000.000000000 (epoch-seconds)
56 date: final: (Y-M-D) 1990-12-13 13:30:00 (UTC)
57 date: final: (Y-M-D) 1990-12-13 22:30:00 (UTC+09)
58 date: output format: '%a %b %e %T %z %Y'
59 Thu Dec 13 07:30:00 -0600 1990
60 EOF
62 TZ=America/Belize date --debug -d "$in1" +'%a %b %e %T %z %Y' >out1 2>&1 ||
63 fail=1
65 compare exp1 out1 || fail=1
68 ## Test 2: Invalid date from Coreutils' FAQ
69 ## (with explicit timezone added)
70 in2='TZ="America/Edmonton" 2006-04-02 02:30:00'
71 cat<<EOF>exp2
72 date: parsed date part: (Y-M-D) 2006-04-02
73 date: parsed time part: 02:30:00
74 date: input timezone: TZ="America/Edmonton" in date string
75 date: using specified time as starting value: '02:30:00'
76 date: error: invalid date/time value:
77 date: user provided time: '(Y-M-D) 2006-04-02 02:30:00'
78 date: normalized time: '(Y-M-D) 2006-04-02 XX:XX:XX'
79 date: --
80 date: possible reasons:
81 date: nonexistent due to daylight-saving time;
82 date: numeric values overflow;
83 date: missing timezone
84 date: invalid date 'TZ="America/Edmonton" 2006-04-02 02:30:00'
85 EOF
87 # date should return 1 (error) for invalid date
88 returns_ 1 date --debug -d "$in2" >out2-t 2>&1 || fail=1
90 # The output line of "normalized time" can differ between systems
91 # (e.g. glibc vs musl) and should not be checked.
92 # See: https://lists.gnu.org/archive/html/coreutils/2019-05/msg00039.html
93 sed '/normalized time:/s/ [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/ XX:XX:XX/' \
94 out2-t > out2 || framework_failure_
96 compare exp2 out2 || fail=1
99 ## Test 3: timespec (input always UTC, output is TZ-dependent)
101 in3='@1'
102 cat<<EOF>exp3
103 date: parsed number of seconds part: number of seconds: 1
104 date: input timezone: '@timespec' - always UTC
105 date: timezone: TZ="America/Lima" environment value
106 date: final: 1.000000000 (epoch-seconds)
107 date: final: (Y-M-D) 1970-01-01 00:00:01 (UTC)
108 date: final: (Y-M-D) 1969-12-31 19:00:01 (UTC-05)
109 date: output format: '%a %b %e %T %z %Y'
110 Wed Dec 31 19:00:01 -0500 1969
113 TZ=America/Lima date --debug -d "$in3" +'%a %b %e %T %z %Y' >out3 2>&1 || fail=1
114 compare exp3 out3 || fail=1
117 ## Parsing a lone number.
118 ## Fixed in gnulib v0.1-1099-gf2d4b5c
119 ## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=f2d4b5caa
120 cat<<EOF>exp4
121 date: parsed number part: (Y-M-D) 2013-01-01
122 date: input timezone: TZ="UTC0" environment value or -u
123 date: warning: using midnight as starting time: 00:00:00
124 date: starting date/time: '(Y-M-D) 2013-01-01 00:00:00'
125 date: '(Y-M-D) 2013-01-01 00:00:00' = 1356998400 epoch-seconds
126 date: timezone: Universal Time
127 date: final: 1356998400.000000000 (epoch-seconds)
128 date: final: (Y-M-D) 2013-01-01 00:00:00 (UTC)
129 date: final: (Y-M-D) 2013-01-01 00:00:00 (UTC+00)
130 date: output format: '$d_t_fmt'
131 Tue Jan 1 00:00:00 UTC 2013
134 date -u --debug -d '20130101' >out4 2>&1 || fail=1
135 compare exp4 out4 || fail=1
139 ## Parsing a relative number after a timezone string
140 ## Fixed in gnulib v0.1-1100-g5c438e8
141 ## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=5c438e8ce7d
142 cat<<EOF>exp5
143 date: parsed date part: (Y-M-D) 2013-10-30
144 date: parsed time part: 00:00:00
145 date: parsed relative part: -8 day(s)
146 date: parsed zone part: UTC+00
147 date: input timezone: parsed date/time string (+00)
148 date: using specified time as starting value: '00:00:00'
149 date: starting date/time: '(Y-M-D) 2013-10-30 00:00:00 TZ=+00'
150 date: warning: when adding relative days, it is recommended to specify noon
151 date: after date adjustment (+0 years, +0 months, -8 days),
152 date: new date/time = '(Y-M-D) 2013-10-22 00:00:00 TZ=+00'
153 date: '(Y-M-D) 2013-10-22 00:00:00 TZ=+00' = 1382400000 epoch-seconds
154 date: timezone: Universal Time
155 date: final: 1382400000.000000000 (epoch-seconds)
156 date: final: (Y-M-D) 2013-10-22 00:00:00 (UTC)
157 date: final: (Y-M-D) 2013-10-22 00:00:00 (UTC+00)
158 date: output format: '%F'
159 2013-10-22
162 in5='2013-10-30 00:00:00 UTC -8 days'
163 date -u --debug +%F -d "$in5" >out5 2>&1 || fail=1
164 compare exp5 out5 || fail=1
167 ## Explicitly warn about unexpected day/month shifts.
168 ## added in gnulib v0.1-1101-gf14eff1
169 ## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=f14eff1b3cde2b
170 TOOLONG='it is recommended to specify the 15th of the months'
171 cat<<EOF>exp6
172 date: parsed date part: (Y-M-D) 2016-10-31
173 date: parsed relative part: -1 month(s)
174 date: input timezone: TZ="UTC0" environment value or -u
175 date: warning: using midnight as starting time: 00:00:00
176 date: starting date/time: '(Y-M-D) 2016-10-31 00:00:00'
177 date: warning: when adding relative months/years, $TOOLONG
178 date: after date adjustment (+0 years, -1 months, +0 days),
179 date: new date/time = '(Y-M-D) 2016-10-01 00:00:00'
180 date: warning: month/year adjustment resulted in shifted dates:
181 date: adjusted Y M D: 2016 09 31
182 date: normalized Y M D: 2016 10 01
183 date: '(Y-M-D) 2016-10-01 00:00:00' = 1475280000 epoch-seconds
184 date: timezone: Universal Time
185 date: final: 1475280000.000000000 (epoch-seconds)
186 date: final: (Y-M-D) 2016-10-01 00:00:00 (UTC)
187 date: final: (Y-M-D) 2016-10-01 00:00:00 (UTC+00)
188 date: output format: '$d_t_fmt'
189 Sat Oct 1 00:00:00 UTC 2016
192 date -u --debug -d '2016-10-31 - 1 month' >out6 2>&1 || fail=1
193 compare exp6 out6 || fail=1
197 ## Explicitly warn about crossing DST boundaries.
198 ## added in gnulib v0.1-1102-g30a55dd
199 ## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=30a55dd72dad2
200 TOOLONG2='it is recommended to specify the 15th of the months'
201 cat<<EOF>exp7
202 date: parsed date part: (Y-M-D) 2016-06-01
203 date: parsed local_zone part: isdst=1
204 date: parsed relative part: +6 month(s)
205 date: input timezone: TZ="America/New_York" environment value, dst
206 date: warning: using midnight as starting time: 00:00:00
207 date: starting date/time: '(Y-M-D) 2016-06-01 00:00:00'
208 date: warning: when adding relative months/years, $TOOLONG2
209 date: after date adjustment (+0 years, +6 months, +0 days),
210 date: new date/time = '(Y-M-D) 2016-11-30 23:00:00'
211 date: warning: daylight saving time changed after date adjustment
212 date: warning: month/year adjustment resulted in shifted dates:
213 date: adjusted Y M D: 2016 12 01
214 date: normalized Y M D: 2016 11 30
215 date: '(Y-M-D) 2016-11-30 23:00:00' = 1480564800 epoch-seconds
216 date: timezone: TZ="America/New_York" environment value
217 date: final: 1480564800.000000000 (epoch-seconds)
218 date: final: (Y-M-D) 2016-12-01 04:00:00 (UTC)
219 date: final: (Y-M-D) 2016-11-30 23:00:00 (UTC-05)
220 date: output format: '%F'
221 2016-11-30
224 in7='2016-06-01 EDT + 6 months'
225 TZ=America/New_York date --debug -d "$in7" +%F >out7 2>&1 || fail=1
226 compare exp7 out7 || fail=1
229 ## fix local timezone debug messages.
230 ## fixed in git v0.1-1103-gc56e7fb
231 ## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=c56e7fbb032
233 cat<<EOF>exp8_1
234 date: parsed date part: (Y-M-D) 2011-12-11
235 date: parsed local_zone part: isdst=0
236 date: input timezone: TZ="Europe/Helsinki" environment value
237 date: warning: using midnight as starting time: 00:00:00
238 date: starting date/time: '(Y-M-D) 2011-12-11 00:00:00'
239 date: '(Y-M-D) 2011-12-11 00:00:00' = 1323554400 epoch-seconds
240 date: timezone: TZ="Europe/Helsinki" environment value
241 date: final: 1323554400.000000000 (epoch-seconds)
242 date: final: (Y-M-D) 2011-12-10 22:00:00 (UTC)
243 date: final: (Y-M-D) 2011-12-11 00:00:00 (UTC+02)
244 date: output format: '$d_t_fmt'
245 Sun Dec 11 00:00:00 EET 2011
248 TZ=Europe/Helsinki date --debug -d '2011-12-11 EET' >out8_1 2>&1 || fail=1
249 compare exp8_1 out8_1 || fail=1
251 cat<<EOF>exp8_2
252 date: parsed date part: (Y-M-D) 2011-06-11
253 date: parsed local_zone part: isdst=1
254 date: input timezone: TZ="Europe/Helsinki" environment value, dst
255 date: warning: using midnight as starting time: 00:00:00
256 date: starting date/time: '(Y-M-D) 2011-06-11 00:00:00'
257 date: '(Y-M-D) 2011-06-11 00:00:00' = 1307739600 epoch-seconds
258 date: timezone: TZ="Europe/Helsinki" environment value
259 date: final: 1307739600.000000000 (epoch-seconds)
260 date: final: (Y-M-D) 2011-06-10 21:00:00 (UTC)
261 date: final: (Y-M-D) 2011-06-11 00:00:00 (UTC+03)
262 date: output format: '$d_t_fmt'
263 Sat Jun 11 00:00:00 EEST 2011
266 TZ=Europe/Helsinki date --debug -d '2011-06-11 EEST' >out8_2 2>&1 || fail=1
267 compare exp8_2 out8_2 || fail=1
271 ## fix debug message on lone year number (The "2011" part).
272 ## fixed in gnulib v0.1-1104-g15b8f30
273 ## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=15b8f3046a25
275 ## NOTE:
276 ## When the date 'Apr 11' is parsed, the year part will be the
277 ## current year. The expected output thus depends on the year
278 ## the test is being run. We'll use sed to change it to XXXX.
279 cat<<EOF>exp9
280 date: parsed date part: (Y-M-D) XXXX-04-11
281 date: parsed time part: 22:59:00
282 date: parsed number part: year: 2011
283 date: input timezone: TZ="UTC0" environment value or -u
284 date: using specified time as starting value: '22:59:00'
285 date: starting date/time: '(Y-M-D) 2011-04-11 22:59:00'
286 date: '(Y-M-D) 2011-04-11 22:59:00' = 1302562740 epoch-seconds
287 date: timezone: Universal Time
288 date: final: 1302562740.000000000 (epoch-seconds)
289 date: final: (Y-M-D) 2011-04-11 22:59:00 (UTC)
290 date: final: (Y-M-D) 2011-04-11 22:59:00 (UTC+00)
291 date: output format: '$d_t_fmt'
292 Mon Apr 11 22:59:00 UTC 2011
295 date -u --debug -d 'Apr 11 22:59:00 2011' >out9_t 2>&1 || fail=1
296 sed '1s/(Y-M-D) [0-9][0-9][0-9][0-9]-/(Y-M-D) XXXX-/' out9_t > out9 \
297 || framework_failure_
298 compare exp9 out9 || fail=1
301 # Diagnose discarded -d arguments
302 echo 'date: only using last of multiple -d options' > exp10 \
303 || framework_failure_
304 cat exp9 >> exp10 || framework_failure_
305 date -u --debug -d 'discard' -d 'Apr 11 22:59:00 2011' > out10_t 2>&1 || fail=1
306 sed '2s/(Y-M-D) [0-9][0-9][0-9][0-9]-/(Y-M-D) XXXX-/' out10_t >> out10 \
307 || framework_failure_
308 compare exp10 out10 || fail=1
311 Exit $fail