severity 589909 wishlist thanks Tollef Fog Heen wrote: > It seems like date(1) is somewhat confused about what «this week» means: > > : tfheen@qurzaw ~ > LC_MESSAGES=en_US.UTF-8 date > Thu Jul 22 09:37:31 CEST 2010 > : tfheen@qurzaw ~ > LC_MESSAGES=en_US.UTF-8 date -d 'Monday this week' > Mon Jul 26 00:00:00 CEST 2010 > > I would quite clearly argue that «Monday this week» is the 19th, not the > 26nd.
The date parsing authors fell into the common trap of trying to write
a program to understand human language but doing so without a human.
There are so many traps!
The clue as to what is happening comes from the documentation where it
talks about "now" and "today" and "this".
The strings `now' or `today' are relative items corresponding to
zero-valued time displacement, these strings come from the fact a
zero-valued time displacement represents the current time when not
otherwise changed by previous items. They may be used to stress other
items, like in `12:00 today'. The string `this' also has the meaning
of a zero-valued time displacement, but is preferred in date strings
like `this thursday'.
So "now", "today" and "this" have all been defined as zero time items.
They are more of a sugar coating than anything that has any serious
meaning since the current time is the default. Saying "now" is like
not saying anything at all. Saying "this" is mostly the same. And
"week" has not been defined in this context. Nor has "month" or
"year". Those have only been defined when adding or subtracting time
such as "+2 weeks" which is the same as "+14 days". That is why "this
week" (e.g. +0 weeks) is the same as "now" or "today".
The "this" is a zero time specifier so "this week" or "this year" is
like "today". Like "+0 weeks".
$ date -d "now"
Wed Sep 21 16:12:57 MDT 2011
$ date -d "today"
Wed Sep 21 16:12:57 MDT 2011
$ date -d "this week"
Wed Sep 21 16:12:57 MDT 2011
$ date -d "this year"
Wed Sep 21 16:12:57 MDT 2011
$ date -d "+0 weeks"
Wed Sep 21 16:12:57 MDT 2011
But "last" and "next" work, along with "first", and others, to apply a
modifier. So "next week" and "last week" work. Like "+3 days" or "+2
years". Those are non-zero multipliers and have a non-zero resulting
time modification value.
> It also seems like I can't specify the week:
>
> : tfheen@qurzaw ~ > LC_MESSAGES=en_US.UTF-8 date -d 'Monday in week 29'
> date: invalid date `Monday in week 29'
Nope. Blame the original authors for not having the vision and
implementation to be a better human parser. :-)
> Does this mean there's no good way to find out what date the Monday in
> the current week falls on?
There is a way and that is why I am writing this response, to record a
workaround that will enable this operation.
There isn't a way to /contain/ a date to be between two bookends. But
it is possible to determine Monday of the current week.
Picking a day of the week such as Monday picks the next Monday in the
future. Therefore to find the date of the one in this week you first
need to specify a date in the week before this week. Use "last week"
to set the current time under consideration and then move forward to
the next Monday. Let me build it up by parts.
$ date -R
Wed, 21 Sep 2011 16:20:48 -0600
$ date -R -d 'Monday'
Mon, 26 Sep 2011 00:00:00 -0600
$ date -R -d 'next Monday'
Mon, 26 Sep 2011 00:00:00 -0600
$ date -R -d 'last week'
Wed, 14 Sep 2011 16:22:36 -0600
$ date -R -d 'last week Monday'
Mon, 19 Sep 2011 00:00:00 -0600
$ date -R -d 'last week next Monday'
Mon, 19 Sep 2011 00:00:00 -0600
Or in more long form but perhaps more calculation like:
$ date -R -d 'now - 1 week + Monday'
Mon, 19 Sep 2011 00:00:00 -0600
And usually with dates it is best to work with them around noon and
avoid likely times for Daylight Saving Time changes. Always specify a
time to avoid a DST change.
$ date -R -d '1 week ago next Monday 12:00'
Mon, 19 Sep 2011 12:00:00 -0600
By the way this is a good time to plug the FAQ which has some examples
of working around DST by working at noon. Along with some other
useful tidbits.
http://www.gnu.org/software/coreutils/faq/#The-date-command-is-not-working-right_002e
Bob
signature.asc
Description: Digital signature

