GNU bug report logs - #30102
date -d 'next tuesday, 2 years ago' # Is something like that supported?

Previous Next

Package: coreutils;

Reported by: bug-coreutils <at> trodman.com

Date: Sun, 14 Jan 2018 00:05:01 UTC

Severity: normal

Tags: notabug

Done: Assaf Gordon <assafgordon <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 30102 in the body.
You can then email your comments to 30102 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#30102; Package coreutils. (Sun, 14 Jan 2018 00:05:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to bug-coreutils <at> trodman.com:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Sun, 14 Jan 2018 00:05:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: bug-coreutils <at> trodman.com
To: bug-coreutils <at> gnu.org
Subject: date -d 'next tuesday,
 2 years ago' # Is something like that supported?
Date: Sat, 13 Jan 2018 18:03:53 -0600
$ date --version
date (GNU coreutils) 8.27
--snip
$ info date -n 'Date input formats' |grep 'next tue'
* Relative items in date strings:: next tuesday, 2 years ago.

My goal is to find for example the date of the first Saturday after 1/7/2018
with a single date command.

--
thanks!




Information forwarded to bug-coreutils <at> gnu.org:
bug#30102; Package coreutils. (Sun, 14 Jan 2018 10:51:01 GMT) Full text and rfc822 format available.

Message #8 received at 30102 <at> debbugs.gnu.org (full text, mbox):

From: Assaf Gordon <assafgordon <at> gmail.com>
To: bug-coreutils <at> trodman.com, 30102 <at> debbugs.gnu.org
Subject: Re: bug#30102: date -d 'next tuesday, 2 years ago' # Is something
 like that supported?
Date: Sun, 14 Jan 2018 03:50:07 -0700
tag 30102 notabug
close 30102
stop

Hello,

On 2018-01-13 05:03 PM, bug-coreutils <at> trodman.com wrote:
> $ date --version
> date (GNU coreutils) 8.27

In version 8.27 the date command has an additional option called 
"--debug" which can help in understanding the date parsing.
I will use it in the output below.

> $ info date -n 'Date input formats' |grep 'next tue'
> * Relative items in date strings:: next tuesday, 2 years ago.

Date does accept "2 years ago next Tuesday".
However, it first calculates "next Tuesday" (which as of this writing is 
2018-Jan-16), and then subtracts two years, giving "Sat, Jan 16th, 
2016". Not sure if that is what you wanted.

===
$ date --debug -d 'next tuesday 2 years ago'
date: parsed day part: next/first Tue (day ordinal=1 number=2)
date: parsed relative part: -2 year(s)
date: input timezone: system default
date: warning: using midnight as starting time: 00:00:00
date: new start date: 'next/first Tue' is '(Y-M-D) 2018-01-16 00:00:00'
date: starting date/time: '(Y-M-D) 2018-01-16 00:00:00'
date: warning: when adding relative months/years, it is recommended to 
specify the 15th of the months
date: after date adjustment (-2 years, +0 months, +0 days),
date:     new date/time = '(Y-M-D) 2016-01-16 00:00:00'
date: '(Y-M-D) 2016-01-16 00:00:00' = 1452927600 epoch-seconds
date: timezone: system default
date: final: 1452927600.000000000 (epoch-seconds)
date: final: (Y-M-D) 2016-01-16 07:00:00 (UTC)
date: final: (Y-M-D) 2016-01-16 00:00:00 (UTC-07)
Sat Jan 16 00:00:00 MST 2016
===

> My goal is to find for example the date of the first Saturday after 1/7/2018
> with a single date command.

Of the top of my head I can't think of a single command that would work,
because 'date' ignores "next tuesday" when given an explicit date, as 
shown below (notice the warning):

===
date --debug -d "2018-01-07 next tuesday"
date: parsed date part: (Y-M-D) 2018-01-07
date: parsed day part: next/first Tue (day ordinal=1 number=2)
date: input timezone: system default
date: warning: using midnight as starting time: 00:00:00
date: warning: day (next/first Tue) ignored when explicit dates are given
date: starting date/time: '(Y-M-D) 2018-01-07 00:00:00'
date: '(Y-M-D) 2018-01-07 00:00:00' = 1515308400 epoch-seconds
date: timezone: system default
date: final: 1515308400.000000000 (epoch-seconds)
date: final: (Y-M-D) 2018-01-07 07:00:00 (UTC)
date: final: (Y-M-D) 2018-01-07 00:00:00 (UTC-07)
Sun Jan  7 00:00:00 MST 2018
===

However, it can be done with two commands:
first, find the day-of-week (DOW) of the desired date (2018-Jan-7 is 
Sunday). I will use the "%w" format (see 'date --help'):
     %w   day of week (0..6); 0 is Sunday
Then, calculate how many days we need to add to get to saturday (6 
days), and show the date of '2018-Jan-7 + 6 days':

===
$ FROM="2018-01-07"
$ DOW=$(date -d "$FROM" +%w)
$ ADDDAYS=$((6-DOW))
$ date -d "$FROM + $ADDDAYS days"
Sat Jan 13 00:00:00 MST 2018
===



Lastly,
You wrote "after 1/7/2018" so I assume you wanted a specific date.
But if you just want "next saturday" from today (or from last week), 
then date 'just works':

===
$ date
Sun Jan 14 03:48:03 MST 2018

$ date -d 'next saturday'
Sat Jan 20 00:00:00 MST 2018

$ date -d 'last week next saturday'
Sat Jan 13 00:00:00 MST 2018
===



I'm marking this as "not a bug" and closing it because it is a question 
and not a bug - but general discussion is very welcomed to continue,
simply by replying to this thread.



regards,
 - assaf






Information forwarded to bug-coreutils <at> gnu.org:
bug#30102; Package coreutils. (Sun, 14 Jan 2018 12:05:02 GMT) Full text and rfc822 format available.

Message #11 received at submit <at> debbugs.gnu.org (full text, mbox):

From: bug-coreutils <at> trodman.com
To: bug-coreutils <at> gnu.org
Subject: Re: bug#30102: date -d 'next tuesday,
 2 years ago' # Is something like that supported?
Date: Sun, 14 Jan 2018 06:04:39 -0600
Hi Assaf:

On Sun 1/14/18 3:50 -0700 Assaf Gordon wrote:
> On 2018-01-13 05:03 PM, bug-coreutils <at> trodman.com wrote:
> In version 8.27 the date command has an additional option called 
> "--debug" which can help in understanding the date parsing.

Thanks/good to know.

> I will use it in the output below.
> 
> > $ info date -n 'Date input formats' |grep 'next tue'
> > * Relative items in date strings:: next tuesday, 2 years ago.
> 
> Date does accept "2 years ago next Tuesday".
> However, it first calculates "next Tuesday" (which as of this writing is 
> 2018-Jan-16), and then subtracts two years, giving "Sat, Jan 16th, 
> 2016". Not sure if that is what you wanted.
> 
> ===
> $ date --debug -d 'next tuesday 2 years ago'
> date: parsed day part: next/first Tue (day ordinal=1 number=2)
> date: parsed relative part: -2 year(s)
> date: input timezone: system default
> date: warning: using midnight as starting time: 00:00:00
> date: new start date: 'next/first Tue' is '(Y-M-D) 2018-01-16 00:00:00'
> date: starting date/time: '(Y-M-D) 2018-01-16 00:00:00'
> date: warning: when adding relative months/years, it is recommended to 
> specify the 15th of the months
> date: after date adjustment (-2 years, +0 months, +0 days),
> date:     new date/time = '(Y-M-D) 2016-01-16 00:00:00'
> date: '(Y-M-D) 2016-01-16 00:00:00' = 1452927600 epoch-seconds
> date: timezone: system default
> date: final: 1452927600.000000000 (epoch-seconds)
> date: final: (Y-M-D) 2016-01-16 07:00:00 (UTC)
> date: final: (Y-M-D) 2016-01-16 00:00:00 (UTC-07)
> Sat Jan 16 00:00:00 MST 2016
> ===
> 
> > My goal is to find for example the date of the first Saturday after 1/7/2018
> > with a single date command.
> 
> Of the top of my head I can't think of a single command that would work,
> because 'date' ignores "next tuesday" when given an explicit date, as 
> shown below (notice the warning):

OK, thanks for carefully reading my goal!

> ===
> date --debug -d "2018-01-07 next tuesday"
> date: parsed date part: (Y-M-D) 2018-01-07
> date: parsed day part: next/first Tue (day ordinal=1 number=2)
> date: input timezone: system default
> date: warning: using midnight as starting time: 00:00:00
> date: warning: day (next/first Tue) ignored when explicit dates are given
> date: starting date/time: '(Y-M-D) 2018-01-07 00:00:00'
> date: '(Y-M-D) 2018-01-07 00:00:00' = 1515308400 epoch-seconds
> date: timezone: system default
> date: final: 1515308400.000000000 (epoch-seconds)
> date: final: (Y-M-D) 2018-01-07 07:00:00 (UTC)
> date: final: (Y-M-D) 2018-01-07 00:00:00 (UTC-07)
> Sun Jan  7 00:00:00 MST 2018
> ===
> 
> However, it can be done with two commands:
> first, find the day-of-week (DOW) of the desired date (2018-Jan-7 is 
> Sunday). I will use the "%w" format (see 'date --help'):
>       %w   day of week (0..6); 0 is Sunday
> Then, calculate how many days we need to add to get to saturday (6 
> days), and show the date of '2018-Jan-7 + 6 days':
> 
> ===
> $ FROM="2018-01-07"
> $ DOW=$(date -d "$FROM" +%w)
> $ ADDDAYS=$((6-DOW))
> $ date -d "$FROM + $ADDDAYS days"
> Sat Jan 13 00:00:00 MST 2018
> ===

Thanks, above is the general approach I plan to use; I'm pretty clear about soln.  I plan to make
a bash function that will give for example: date of 2nd Sat, 3rd Wed or 1st Tue for a given month --
useful for reoccurring meetings.

> Lastly,
> You wrote "after 1/7/2018" so I assume you wanted a specific date.
> But if you just want "next saturday" from today (or from last week), 
> then date 'just works':
> 
> ===
> $ date
> Sun Jan 14 03:48:03 MST 2018
> 
> $ date -d 'next saturday'
> Sat Jan 20 00:00:00 MST 2018
> 
> $ date -d 'last week next saturday'
> Sat Jan 13 00:00:00 MST 2018
> ===
> 
> 
> I'm marking this as "not a bug" and closing it because it is a question 
> and not a bug - but general discussion is very welcomed to continue,
> simply by replying to this thread.

Makes sense.

--
If we could convince 'GNU date' that the system date
was any specified value, then my issue would be solved.  Is there a way
to do this, w/o changing the host's clock?

--
regards,
Tom
Milwaukee WI




Added tag(s) notabug. Request was from Assaf Gordon <assafgordon <at> gmail.com> to control <at> debbugs.gnu.org. (Tue, 30 Oct 2018 02:33:01 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 30102 <at> debbugs.gnu.org and bug-coreutils <at> trodman.com Request was from Assaf Gordon <assafgordon <at> gmail.com> to control <at> debbugs.gnu.org. (Tue, 30 Oct 2018 02:33:01 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Tue, 27 Nov 2018 12:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 5 years and 123 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.