I need to subtract two days from a date in a loop in a bash script, but it seems at some point in the past it fails. There's some kind of numeric limitation in there, I suspect. What kind of workaround could I use? I'm using bash 5.1 on Linux Mint 21 (basically Ubuntu 22.04 LTS).
Resolution: date
can go pound sand, for reasons explained in the answer. I did the entire thing in Python instead.
jcastro@localhost:~$ date --date="2025-03-01 - 2 days" +"%Y-%m-%d"
2025-02-27
jcastro@localhost:~$ date --date="2018-11-06 - 2 days" +"%Y-%m-%d"
2018-11-04
jcastro@localhost:~$ date --date="2018-11-04 - 2 days" +"%Y-%m-%d"
date: invalid date '2018-11-04 - 2 days'
It seems that November 4, 2018, specifically, is an invalid date.
jcastro@jclvdell:~$ date --date="2018-11-02"
Fri Nov 2 00:00:00 -03 2018
jcastro@jclvdell:~$ date --date="2018-11-03"
Sat Nov 3 00:00:00 -03 2018
jcastro@jclvdell:~$ date --date="2018-11-04"
date: invalid date '2018-11-04'
jcastro@jclvdell:~$ date --date="2018-11-05"
Mon Nov 5 00:00:00 -02 2018
jcastro@jclvdell:~$ date --date="2018-11-06"
Tue Nov 6 00:00:00 -02 2018
additional info
I'm running on latest RHEL 9.5:
$ bash --version
GNU bash, version 5.1.8(1)-release (x86_64-redhat-linux-gnu)
$ date --version
date (GNU coreutils) 8.32
I need to subtract two days from a date in a loop in a bash script, but it seems at some point in the past it fails. There's some kind of numeric limitation in there, I suspect. What kind of workaround could I use? I'm using bash 5.1 on Linux Mint 21 (basically Ubuntu 22.04 LTS).
Resolution: date
can go pound sand, for reasons explained in the answer. I did the entire thing in Python instead.
jcastro@localhost:~$ date --date="2025-03-01 - 2 days" +"%Y-%m-%d"
2025-02-27
jcastro@localhost:~$ date --date="2018-11-06 - 2 days" +"%Y-%m-%d"
2018-11-04
jcastro@localhost:~$ date --date="2018-11-04 - 2 days" +"%Y-%m-%d"
date: invalid date '2018-11-04 - 2 days'
It seems that November 4, 2018, specifically, is an invalid date.
jcastro@jclvdell:~$ date --date="2018-11-02"
Fri Nov 2 00:00:00 -03 2018
jcastro@jclvdell:~$ date --date="2018-11-03"
Sat Nov 3 00:00:00 -03 2018
jcastro@jclvdell:~$ date --date="2018-11-04"
date: invalid date '2018-11-04'
jcastro@jclvdell:~$ date --date="2018-11-05"
Mon Nov 5 00:00:00 -02 2018
jcastro@jclvdell:~$ date --date="2018-11-06"
Tue Nov 6 00:00:00 -02 2018
additional info
I'm running on latest RHEL 9.5:
$ bash --version
GNU bash, version 5.1.8(1)-release (x86_64-redhat-linux-gnu)
$ date --version
date (GNU coreutils) 8.32
Share
Improve this question
edited Mar 24 at 17:52
JCCyC
asked Mar 18 at 13:05
JCCyCJCCyC
16.8k11 gold badges50 silver badges77 bronze badges
9
|
Show 4 more comments
1 Answer
Reset to default 8@tjm3772 and @jasonharper were right on the money. It was a daylight savings time issue. The date
command interprets "2018-11-04" not as a date, but as an instant in time, "2018-11-04 00:00:00" -- and that instant in time really did not exist in Brazil because that's when the clocks went forward. DST has been no longer used in Brazil from 2019 onward and that's why everything went smoothly from the current date back to that point.
jcastro@localhost:~$ date --debug --date="2018-11-04"
date: parsed date part: (Y-M-D) 2018-11-04
date: input timezone: system default
date: warning: using midnight as starting time: 00:00:00
date: error: invalid date/time value:
date: user provided time: '(Y-M-D) 2018-11-04 00:00:00'
date: normalized time: '(Y-M-D) 2018-11-04 01:00:00'
date: --
date: possible reasons:
date: non-existing due to daylight-saving time;
date: numeric values overflow;
date: missing timezone
date: invalid date '2018-11-04'
date --date="2018-11-04 - 2 days" +"%Y-%m-%d" 2018-11-02
– U880D Commented Mar 18 at 13:10--debug
to your command line, to get a more detailed error message. The only way I could duplicate the error was to use a different Unicode dash character in the date string, rather than the normal ASCII dash. – jasonharper Commented Mar 18 at 13:15date --version
) – tjm3772 Commented Mar 18 at 13:18date
version matters, maybe thetzinfo
version matters, but bash itself has nothing to do with those things. – Charles Duffy Commented Mar 18 at 13:25