1#!/bin/sh
2# Test 'date --debug' option.
3
4# Copyright (C) 2016-2018 Free Software Foundation, Inc.
5
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.
10
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.
15
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/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ date
21
22export LC_ALL=C
23
24## Ensure timezones are supported.
25## (NOTE: America/Belize timezone does not change on DST)
26test "$(TZ=America/Belize date +%z)" = '-0600' \
27    || skip_ 'Timezones database not found'
28
29
30##
31## Test 1: complex date string
32##
33in1='TZ="Asia/Tokyo" Sun, 90-12-11 + 3 days - 90 minutes'
34
35cat<<EOF>exp1
36date: parsed day part: Sun (day ordinal=0 number=0)
37date: parsed date part: (Y-M-D) 0090-12-11
38date: parsed relative part: +3 day(s)
39date: parsed relative part: +3 day(s) -90 minutes
40date: input timezone: TZ="Asia/Tokyo" in date string
41date: warning: adjusting year value 90 to 1990
42date: warning: using midnight as starting time: 00:00:00
43date: warning: day (Sun) ignored when explicit dates are given
44date: starting date/time: '(Y-M-D) 1990-12-11 00:00:00'
45date: warning: when adding relative days, it is recommended to specify noon
46date: after date adjustment (+0 years, +0 months, +3 days),
47date:     new date/time = '(Y-M-D) 1990-12-14 00:00:00'
48date: '(Y-M-D) 1990-12-14 00:00:00' = 661100400 epoch-seconds
49date: after time adjustment (+0 hours, -90 minutes, +0 seconds, +0 ns),
50date:     new time = 661095000 epoch-seconds
51date: timezone: TZ="Asia/Tokyo" environment value
52date: final: 661095000.000000000 (epoch-seconds)
53date: final: (Y-M-D) 1990-12-13 13:30:00 (UTC)
54date: final: (Y-M-D) 1990-12-13 22:30:00 (UTC+09)
55Thu Dec 13 07:30:00 -0600 1990
56EOF
57
58TZ=America/Belize date --debug -d "$in1" +'%a %b %e %T %z %Y' >out1 2>&1 ||
59  fail=1
60
61compare exp1 out1 || fail=1
62
63##
64## Test 2: Invalid date from Coreutils' FAQ
65##         (with explicit timezone added)
66in2='TZ="America/Edmonton" 2006-04-02 02:30:00'
67cat<<EOF>exp2
68date: parsed date part: (Y-M-D) 2006-04-02
69date: parsed time part: 02:30:00
70date: input timezone: TZ="America/Edmonton" in date string
71date: using specified time as starting value: '02:30:00'
72date: error: invalid date/time value:
73date:     user provided time: '(Y-M-D) 2006-04-02 02:30:00'
74date:        normalized time: '(Y-M-D) 2006-04-02 03:30:00'
75date:                                             --
76date:      possible reasons:
77date:        non-existing due to daylight-saving time;
78date:        numeric values overflow;
79date:        missing timezone
80date: invalid date 'TZ="America/Edmonton" 2006-04-02 02:30:00'
81EOF
82
83# date should return 1 (error) for invalid date
84returns_ 1 date --debug -d "$in2" >out2 2>&1 || fail=1
85compare exp2 out2 || fail=1
86
87##
88## Test 3: timespec (input always UTC, output is TZ-dependent)
89##
90in3='@1'
91cat<<EOF>exp3
92date: parsed number of seconds part: number of seconds: 1
93date: input timezone: '@timespec' - always UTC
94date: timezone: TZ="America/Lima" environment value
95date: final: 1.000000000 (epoch-seconds)
96date: final: (Y-M-D) 1970-01-01 00:00:01 (UTC)
97date: final: (Y-M-D) 1969-12-31 19:00:01 (UTC-05)
98Wed Dec 31 19:00:01 -0500 1969
99EOF
100
101TZ=America/Lima date --debug -d "$in3" +'%a %b %e %T %z %Y' >out3 2>&1 || fail=1
102compare exp3 out3 || fail=1
103
104##
105## Parsing a lone number.
106## Fixed in gnulib v0.1-1099-gf2d4b5c
107## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=f2d4b5caa
108cat<<EOF>exp4
109date: parsed number part: (Y-M-D) 2013-01-01
110date: input timezone: TZ="UTC0" environment value or -u
111date: warning: using midnight as starting time: 00:00:00
112date: starting date/time: '(Y-M-D) 2013-01-01 00:00:00'
113date: '(Y-M-D) 2013-01-01 00:00:00' = 1356998400 epoch-seconds
114date: timezone: Universal Time
115date: final: 1356998400.000000000 (epoch-seconds)
116date: final: (Y-M-D) 2013-01-01 00:00:00 (UTC)
117date: final: (Y-M-D) 2013-01-01 00:00:00 (UTC+00)
118Tue Jan  1 00:00:00 UTC 2013
119EOF
120
121date -u --debug -d '20130101' >out4 2>&1 || fail=1
122compare exp4 out4 || fail=1
123
124
125##
126## Parsing a relative number after a timezone string
127## Fixed in gnulib v0.1-1100-g5c438e8
128## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=5c438e8ce7d
129cat<<EOF>exp5
130date: parsed date part: (Y-M-D) 2013-10-30
131date: parsed time part: 00:00:00
132date: parsed relative part: -8 day(s)
133date: parsed zone part: UTC+00
134date: input timezone: parsed date/time string (+00)
135date: using specified time as starting value: '00:00:00'
136date: starting date/time: '(Y-M-D) 2013-10-30 00:00:00 TZ=+00'
137date: warning: when adding relative days, it is recommended to specify noon
138date: after date adjustment (+0 years, +0 months, -8 days),
139date:     new date/time = '(Y-M-D) 2013-10-22 00:00:00 TZ=+00'
140date: '(Y-M-D) 2013-10-22 00:00:00 TZ=+00' = 1382400000 epoch-seconds
141date: timezone: Universal Time
142date: final: 1382400000.000000000 (epoch-seconds)
143date: final: (Y-M-D) 2013-10-22 00:00:00 (UTC)
144date: final: (Y-M-D) 2013-10-22 00:00:00 (UTC+00)
1452013-10-22
146EOF
147
148in5='2013-10-30 00:00:00 UTC -8 days'
149date -u --debug +%F -d "$in5" >out5 2>&1 || fail=1
150compare exp5 out5 || fail=1
151
152##
153## Explicitly warn about unexpected day/month shifts.
154## added in gnulib v0.1-1101-gf14eff1
155## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=f14eff1b3cde2b
156TOOLONG='it is recommended to specify the 15th of the months'
157cat<<EOF>exp6
158date: parsed date part: (Y-M-D) 2016-10-31
159date: parsed relative part: -1 month(s)
160date: input timezone: TZ="UTC0" environment value or -u
161date: warning: using midnight as starting time: 00:00:00
162date: starting date/time: '(Y-M-D) 2016-10-31 00:00:00'
163date: warning: when adding relative months/years, $TOOLONG
164date: after date adjustment (+0 years, -1 months, +0 days),
165date:     new date/time = '(Y-M-D) 2016-10-01 00:00:00'
166date: warning: month/year adjustment resulted in shifted dates:
167date:      adjusted Y M D: 2016 09 31
168date:    normalized Y M D: 2016 10 01
169date: '(Y-M-D) 2016-10-01 00:00:00' = 1475280000 epoch-seconds
170date: timezone: Universal Time
171date: final: 1475280000.000000000 (epoch-seconds)
172date: final: (Y-M-D) 2016-10-01 00:00:00 (UTC)
173date: final: (Y-M-D) 2016-10-01 00:00:00 (UTC+00)
174Sat Oct  1 00:00:00 UTC 2016
175EOF
176
177date -u --debug -d '2016-10-31 - 1 month' >out6 2>&1 || fail=1
178compare exp6 out6 || fail=1
179
180
181##
182## Explicitly warn about crossing DST boundaries.
183## added in gnulib v0.1-1102-g30a55dd
184## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=30a55dd72dad2
185TOOLONG2='it is recommended to specify the 15th of the months'
186cat<<EOF>exp7
187date: parsed date part: (Y-M-D) 2016-06-01
188date: parsed local_zone part: isdst=1
189date: parsed relative part: +6 month(s)
190date: input timezone: TZ="America/New_York" environment value, dst
191date: warning: using midnight as starting time: 00:00:00
192date: starting date/time: '(Y-M-D) 2016-06-01 00:00:00'
193date: warning: when adding relative months/years, $TOOLONG2
194date: after date adjustment (+0 years, +6 months, +0 days),
195date:     new date/time = '(Y-M-D) 2016-11-30 23:00:00'
196date: warning: daylight saving time changed after date adjustment
197date: warning: month/year adjustment resulted in shifted dates:
198date:      adjusted Y M D: 2016 12 01
199date:    normalized Y M D: 2016 11 30
200date: '(Y-M-D) 2016-11-30 23:00:00' = 1480564800 epoch-seconds
201date: timezone: TZ="America/New_York" environment value
202date: final: 1480564800.000000000 (epoch-seconds)
203date: final: (Y-M-D) 2016-12-01 04:00:00 (UTC)
204date: final: (Y-M-D) 2016-11-30 23:00:00 (UTC-05)
2052016-11-30
206EOF
207
208in7='2016-06-01 EDT + 6 months'
209TZ=America/New_York date --debug -d "$in7" +%F >out7 2>&1 || fail=1
210compare exp7 out7 || fail=1
211
212
213## fix local timezone debug messages.
214## fixed in git v0.1-1103-gc56e7fb
215## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=c56e7fbb032
216
217cat<<EOF>exp8_1
218date: parsed date part: (Y-M-D) 2011-12-11
219date: parsed local_zone part: isdst=0
220date: input timezone: TZ="Europe/Helsinki" environment value
221date: warning: using midnight as starting time: 00:00:00
222date: starting date/time: '(Y-M-D) 2011-12-11 00:00:00'
223date: '(Y-M-D) 2011-12-11 00:00:00' = 1323554400 epoch-seconds
224date: timezone: TZ="Europe/Helsinki" environment value
225date: final: 1323554400.000000000 (epoch-seconds)
226date: final: (Y-M-D) 2011-12-10 22:00:00 (UTC)
227date: final: (Y-M-D) 2011-12-11 00:00:00 (UTC+02)
228Sun Dec 11 00:00:00 EET 2011
229EOF
230
231TZ=Europe/Helsinki date --debug -d '2011-12-11 EET' >out8_1 2>&1 || fail=1
232compare exp8_1 out8_1 || fail=1
233
234cat<<EOF>exp8_2
235date: parsed date part: (Y-M-D) 2011-06-11
236date: parsed local_zone part: isdst=1
237date: input timezone: TZ="Europe/Helsinki" environment value, dst
238date: warning: using midnight as starting time: 00:00:00
239date: starting date/time: '(Y-M-D) 2011-06-11 00:00:00'
240date: '(Y-M-D) 2011-06-11 00:00:00' = 1307739600 epoch-seconds
241date: timezone: TZ="Europe/Helsinki" environment value
242date: final: 1307739600.000000000 (epoch-seconds)
243date: final: (Y-M-D) 2011-06-10 21:00:00 (UTC)
244date: final: (Y-M-D) 2011-06-11 00:00:00 (UTC+03)
245Sat Jun 11 00:00:00 EEST 2011
246EOF
247
248TZ=Europe/Helsinki date --debug -d '2011-06-11 EEST' >out8_2 2>&1 || fail=1
249compare exp8_2 out8_2 || fail=1
250
251
252
253## fix debug message on lone year number (The "2011" part).
254## fixed in gnulib v0.1-1104-g15b8f30
255## https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=15b8f3046a25
256##
257## NOTE:
258## When the date 'Apr 11' is parsed, the year part will be the
259## current year. The expected output thus depends on the year
260## the test is being run. We'll use sed to change it to XXXX.
261cat<<EOF>exp9
262date: parsed date part: (Y-M-D) XXXX-04-11
263date: parsed time part: 22:59:00
264date: parsed number part: year: 2011
265date: input timezone: TZ="UTC0" environment value or -u
266date: using specified time as starting value: '22:59:00'
267date: starting date/time: '(Y-M-D) 2011-04-11 22:59:00'
268date: '(Y-M-D) 2011-04-11 22:59:00' = 1302562740 epoch-seconds
269date: timezone: Universal Time
270date: final: 1302562740.000000000 (epoch-seconds)
271date: final: (Y-M-D) 2011-04-11 22:59:00 (UTC)
272date: final: (Y-M-D) 2011-04-11 22:59:00 (UTC+00)
273Mon Apr 11 22:59:00 UTC 2011
274EOF
275
276date -u --debug -d 'Apr 11 22:59:00 2011' >out9_t 2>&1 || fail=1
277sed '1s/(Y-M-D) [0-9][0-9][0-9][0-9]-/(Y-M-D) XXXX-/' out9_t > out9 \
278    || framework_failure_
279compare exp9 out9 || fail=1
280
281
282Exit $fail
283