Subject: bug#26101: Counterproductive calculation order in date

From: Eric Blake <[email-masked]>
Date: Wed, 15 Mar 2017 07:44:43 -0500

On 03/15/2017 07:23 AM, Ulf Zibis wrote:

> 
> A more simple example without touch:
> $ date +%F
> 2017-03-15
> $ date -d "-20 day" +%F
> 2017-02-23
> $ date -d "-20 day -2 month" +%F
> 2016-12-26
> $ date -d "-2 month -20 day" +%F
> 2016-12-26
> 
> In the 2nd example I would expect:
> 2016-12-23

Why?  Both operations are computing: Subtract 20 days, and subtract 2
months (equivalent to 60 days).  Subtraction is commutative, it doesn't
matter which one you subtract first, if you are doing two subtractions.

Maybe you are confused on how date implements "subtract a month".  It
does NOT do "subtract 28, 29, 30, or 31 days as appropriate", but rather
does "subtract 30 days, for lack of anything better to do".  Maybe it
could, but someone would have to write the patch.  Relative dates are
HARD to compute, which is why --debug included this warning:

> date: starting date/time: '(Y-M-D) 1970-01-01 00:00:00 TZ=-06:00'
> date: warning: when adding relative months/years, it is recommended to
> specify the 15th of the months 

It sounds like you are trying to do a piecemeal computation, where you
compute "what month was it two months ago (using the trick of starting
from the 15th of this month, and only outputting the month), and then
subtract 2 days from the given day within the month".  To do piecemeal
computations like that requires piecemeal date invocations, unless you
can propose a patch to the date parser engine to make
additions/subtractions of months smarter based on the order that
relative operations appear in the date string, and prove that it won't
break existing users that have come to rely on the currently-documented
behavior of "month == 30 days".

> 
> So I see several points for enhancement:
> - Allow @seconds in -d option, even for touch.
> - Document the '@' syntax not only as example.
> - For date document if combination of -r -d (and others) is allowed.

Not currently allowed, but maybe it should be.

> - For touch document if combination of -r -d (and others) is allowed.

Yes, it's allowed, but a documentation improvement would not hurt.

> - For date allow combination of -r -d (maybe even more).
> - Calculate terms of -d option from left to right.

Already done (but doesn't matter, if it is commutative) - so apparently
what you are REALLY asking for is a smarter "-1 month" that subtracts a
variable number of days (28-31) rather than a fixed number of days (30).

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org