]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
tests: date: check date-string parsing corner cases
authorSylvestre Ledru <sylvestre@debian.org>
Mon, 8 Jun 2026 19:43:00 +0000 (21:43 +0200)
committerPádraig Brady <P@draigBrady.com>
Tue, 9 Jun 2026 12:42:00 +0000 (13:42 +0100)
* tests/date/date.pl: Add tests covering a leading day-of-week name,
out-of-range zone minutes, two timezone tokens, a bare sign operand,
a trailing token after an @epoch operand, operand quoting in
diagnostics, and a '+N unit' suffix following a complete time.
* tests/date/date-tz.sh: Add tests covering daylight-saving gap,
and overlap local times.
Closes https://github.com/coreutils/coreutils/pull/284

tests/date/date-tz.sh
tests/date/date.pl

index fb8b657801715ae6f7c6271669af481b61ba9daa..25134ba33318c9205ae0f32a87e536e349bd4c11 100755 (executable)
@@ -39,4 +39,21 @@ TZ='Europe/Berlin' date -d "1970-01-01 UTC $secs seconds" +%s > out || fail=1
 echo "$secs" > exp
 compare exp out || fail=1
 
+# If TZ database available (Belize doesn't have DST)
+if test "$(TZ=America/Belize date +%z)" = '-0600'; then
+
+  # A nonexistent local time in the spring-forward gap is rejected.
+  returns_ 1 env \
+   TZ=America/New_York date -d '2024-03-10 02:30' +%T 2>err || fail=1
+  printf "date: invalid date '2024-03-10 02:30'\n" > exp || framework_failure_
+  compare exp err || fail=1
+
+  # An ambiguous local time in the fall-back overlap takes the earlier,
+  # still-DST offset (+0100 here, not +0200).
+  TZ=Europe/Paris date -d '2024-10-27 02:30:00' '+%Y-%m-%dT%T%z' > out || fail=1
+  printf "2024-10-27T02:30:00+0100\n" > exp || framework_failure_
+  compare exp out || fail=1
+
+fi
+
 Exit $fail
index 187c7c6e95c41ad61ff21b69abc011889cd20b4f..1941b99a31cc9dd8cd1f784448e1fac26acfc634 100755 (executable)
@@ -401,6 +401,44 @@ my @Tests =
       {EXIT => 1},
      ],
 
+     # A leading day-of-week name must not shift an explicit calendar date.
+     ['dow-prefix', "-d 'Mon, 2024-06-15 12:00:00' '+%Y-%m-%dT%T'",
+      {OUT=>"2024-06-15T12:00:00"}],
+
+     # Out-of-range zone minutes carry into the hours (99' == 1h39').
+     ['zone-min-99', "-d '2024-06-15 12:00 +05:99' '+%Y-%m-%dT%T%z'",
+      {OUT=>"2024-06-15T05:21:00+0000"}],
+
+     # Two timezone tokens in one operand is an error.
+     ['double-zone', "-d '2024-06-15 12:00 EST PST' '+%T'",
+      {ERR=>"date: invalid date '2024-06-15 12:00 EST PST'\n"},
+      {EXIT=>1}],
+
+     # A bare sign is the empty relative offset: midnight today.
+     ['bare-plus', "-d '+' '+%H:%M:%S'", {OUT=>"00:00:00"}],
+
+     # A trailing token after an \@epoch operand is an error.
+     ['epoch-trailing', "-d '\@5 UTC' '+%T'",
+      {ERR=>"date: invalid date '\@5 UTC'\n"},
+      {EXIT=>1}],
+
+     # The diagnostic reproduces the operand verbatim, surrounding blanks
+     # included.
+     ['quote-blanks', "-d '  noon  '",
+      {ERR=>"date: invalid date '  noon  '\n"},
+      {EXIT=>1}],
+
+     # Non-ASCII bytes in a rejected operand are octal-escaped (U+2212 here).
+     ['quote-nonascii', "-d '\xe2\x88\x921 day'",
+      {ERR=>"date: invalid date '\\342\\210\\2221 day'\n"},
+      {EXIT=>1}],
+
+     # '+N unit' after a complete HH:MM:SS time is not a relative addition.
+     # One needs to use "HH:MM:SS today" so that the relative portion
+     # is not interpreted as a timezone.
+     ['plus-unit-noadd', "-d '2024-06-15 12:00:00 +1 hour' '+%Y-%m-%dT%T%z'",
+      {OUT=>"2024-06-15T12:00:00+0000"}],
+
     );
 
 $limits->{TIME_T_MAX} == $limits->{INTMAX_MAX}