Subject: Re: bug#11057: Having an issue with the date program

From: Eric Blake <[email-masked]>
Date: Wed, 21 Mar 2012 12:10:43 -0600

tag 11057 notabug
thanks

On 03/21/2012 11:26 AM, Morin, Rick wrote:
> Hello,
> 
> I'm having an interesting time trying to get the following code to work using your coreutils date program.  I'm trying to get the date for Wednesday of the current week.  This is running in a Korn shell on an AIX 5.3 (AIX 64) platform.
> 
> MDOW1=$(${SBIN}/date +%w)

Why are you basing things on %w (0-6, 1 is Monday), when you could
instead be using %u (1-7, 1 is Monday)?

Your script snippet didn't show what $manYY and friends were set to, but
I suspect you were manually setting things to various dates to see how
your script behaved.

> 
> Any assistance you can provide would be greatly appreciated.  The target date is day 3 of the current week, with Monday being the 1.  Just to clarify, on Sunday, I am looking for the previous Wednesday date.

If I understand what you are asking, you want:

On Sunday, generate the date of the prior Wednesday (previous week).
On Monday-Tuesday, generate the date of the next Wednesday (current week).
On Wednesday, generate the current date.
On Thursday-Saturday, generate the date of the prior Wednesday (current
week).

More concretely, it sounds like using March 1-7, 2012 as our sample
inputs, you want the following outputs:

Mar 1 (Thu) => Feb 29
Mar 2 (Fri) => Feb 29
Mar 3 (Sat) => Feb 29
Mar 4 (Sun) => Feb 29
Mar 5 (Mon) => Mar 7
Mar 6 (Tue) => Mar 7
Mar 7 (Wed) => Mar 7

Also, you should remember that when adding or subtracting days, you must
take into account daylight savings transitions.  I'd recommend basing
your computations off of noon, rather than midnight, so that even if you
end up at 11am or 1pm due to your calculations changing by multiples of
24 hours but crossing a day with 23 or 25 hours, you still get the right
date.

$ for i in `seq 7`; do
 offset=$(date -d"Mar $i, 2012" +%u)
 date -d"12pm Mar $i, 2012 +3 days -$offset day"
done
Wed Feb 29 12:00:00 MST 2012
Wed Feb 29 12:00:00 MST 2012
Wed Feb 29 12:00:00 MST 2012
Wed Feb 29 12:00:00 MST 2012
Wed Mar  7 12:00:00 MST 2012
Wed Mar  7 12:00:00 MST 2012
Wed Mar  7 12:00:00 MST 2012

Since it looks like I can generate what you asked for, I'm closing this
as not a bug.  You can continue to ask further questions on this thread,
but I don't see anything broken in the current coreutils implementation.

> This message w/attachments (message) is intended solely for the use of the intended recipient(s)

Disclaimers like this are unenforceable on publicly-archived lists.  It
is considered better netiquette to send from a personal account, rather
than an employer's account.

-- 
Eric Blake   [email-masked]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org