Help needed with some date arithmetic

I have a file (main.lst) containing a list of dates in DDMMYYYY format. The dates will mostly be the same but it is possible to have multiple dates and these need not be in chronological order. I have another file containing another list of dates (holidays.lst).

The task is to get the latest date from main.lst. Check if that day is a Sunday or exists in "holidays.lst". If yes, I need to add 1 to the date and perform the above check again. This loop will break once I get a date (procdate) which is neither a Sunday nor a holiday.

Then, I need to add 1 to the "procdate" and perform the above check again. This will yield the final date (finaldate).This is a separate loop having an exit condition similar to the previous loop.

E.g., if procdate from loop 1 happens to be Saturday, then finaldate ought to be Monday (assuming that that particular Monday is not in holidays.lst).

I am using ksh on AIX 5.3 with perl 5.8.8 installed.

Any pointers to the date arithmetic highly appreciated.

Hi, please also show a representative sample of input, desired output and attempts at a solution.

  1. To compare two dates, it'd be better to have them both in YYYYMMDD format. So that its easier for numeric comparison. For e.g., 20120521 is numerically greater than 20120517.

  2. Try Perl's Time::Local module's 'timelocal' routine to convert date into seconds from epoch. This will be suitable for adding 1 day (86400 seconds) to a given date.

  3. Perl's 'strftime' routine from POSIX package is recommended for converting a date into required format.

  4. If you've GNU date, manipulation would be a lot easier.

Input (main.lst) could be something like this :

01062012
28052012
28052012
29052012
28052012
27052012

holidays.lst could be :

01062012

As balajesuri pointed out, I had already figured that I would first need to USE these dates as YYYYMMDD for numeric comparison/sort.

So, latest date in this case would be 01062012. This day happens to be a Friday but is in "holidays.lst". So, I add 1 to the date and get 02062012. This is a Saturday and not in the list. And that's my procdate.

In the second loop, I add 1 to procdate (no checking first). That gives me 03062012. Now this is a Sunday. So, again I add 1 to 03062012 to get 04062012. This is not a Sunday and not in the list. That's my finaldate.

Since I need to find the day of the week for a particular date, I feel this could be done with a combination of cal and awk (to get the field number). But, I am a total newbie with awk....

Hope that helps you to help me, Scrutinizer... :slight_smile:

---------- Post updated at 02:08 AM ---------- Previous update was at 02:06 AM ----------

And I don't have GNU date...

and no printf supporting 'T' format.... that would have been the icing if I had the cake (GNU date)...

---------- Post updated at 03:42 AM ---------- Previous update was at 02:08 AM ----------

Got it...

I can get the next date by using perl's Add_Delta_Days function (Date::Calc module)...

and Sundays can be checked for using the following snippet...

tempdate=03062012
dy=$(echo $tempdate|cut -c 1-2)
mon=$(echo $tempdate|cut -c 3-4)
yr=$(echo $tempdate|cut -c 5-8)
if cal $mon $yr|grep -q "^$(echo $dy|sed 's/^0/ /g')"
then
 echo Sunday
else
 echo Not a Sunday
fi

Maybe i can interest the gentlemen in reading our FAQ?

I hope this helps.

bakunin

1 Like

mon erreur, monsieur....:slight_smile: