On Tue 06 Feb 2018 at 13:48:19 (+0100), Vincent Lefevre wrote: > On 2018-02-05 09:39:12 -0500, Greg Wooledge wrote: > > (*) One specific shell script use case was "Get the last date of a given > > month." Now, obviously you can just set up an array of hard-coded month > > ending dates, and then write a function to determine whether the current > > year is a leap year for the February case. But if you want to do it with > > GNU date -d, then you have to guess a magic incantation that actually > > works. Usually by trial and error. > > > > Anyway, here's what I came up with: > > > > lastday() { > > date +%Y-%m-%d -d "$1 1 day ago + 1 month" > > } > > But the exact meaning of "month" seems undocumented, which may > silently break in a future version (e.g. possibly as a consequence > of a bug fix). So this is a good example of what you should *not* > do. > > This is completely crazy: > > zira% date +%Y-%m-%d -d '2003-09-01 1 day ago + 1 month' > 2003-09-30
Don't say you weren't warned; I demonstrated the --debug flag (in https://lists.debian.org/debian-user/2018/02/msg00101.html) which gives, for your example: $ date +%Y-%m-%d -d '2003-09-01 1 day ago + 1 month' --debug date: parsed date part: (Y-M-D) 2003-09-01 date: parsed relative part: -1 day(s) date: parsed relative part: +1 month(s) -1 day(s) date: input timezone: -06:00 (set from TZ="US/Central" environment value) date: warning: using midnight as starting time: 00:00:00 date: starting date/time: '(Y-M-D) 2003-09-01 00:00:00 TZ=-06:00' date: warning: when adding relative months/years, it is recommended to specify the 15th of the months date: warning: when adding relative days, it is recommended to specify 12:00pm date: after date adjustment (+0 years, +1 months, -1 days), date: new date/time = '(Y-M-D) 2003-09-30 00:00:00 TZ=-06:00' date: '(Y-M-D) 2003-09-30 00:00:00 TZ=-06:00' = 1064898000 epoch-seconds date: output timezone: -06:00 (set from TZ="US/Central" environment value) date: final: 1064898000.000000000 (epoch-seconds) date: final: (Y-M-D) 2003-09-30 05:00:00 (UTC0) date: final: (Y-M-D) 2003-09-30 00:00:00 (output timezone TZ=-06:00) 2003-09-30 $ Most of the examples were begging for off-by-one errors. Cheers, David.