Help needed with some date arithmetic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed with some date arithmetic
# 1  
Old 05-21-2012
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.

Last edited by elixir_sinari; 05-21-2012 at 05:44 AM..
# 2  
Old 05-21-2012
Hi, please also show a representative sample of input, desired output and attempts at a solution.
# 3  
Old 05-21-2012
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.
# 4  
Old 05-21-2012
Quote:
Originally Posted by Scrutinizer
Hi, please also show a representative sample of input, desired output and attempts at a solution.
Input (main.lst) could be something like this :
Code:
01062012
28052012
28052012
29052012
28052012
27052012

holidays.lst could be :
Code:
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... Smilie

---------- 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...

Code:
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


Last edited by elixir_sinari; 05-21-2012 at 09:39 AM..
# 5  
Old 05-21-2012
Maybe i can interest the gentlemen in reading our FAQ?

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 05-21-2012
Quote:
Originally Posted by bakunin
Maybe i can interest the gentlemen in reading our FAQ?

I hope this helps.

bakunin
mon erreur, monsieur....Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with date arithmetic please

Hello fellow forum members, I wrote below piece of code to calculate the date after a given date - date=$DATE_FINAL declare -a max_month=(0 31 28 31 30 31 30 31 31 30 31 30 31) eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!') (( year4=year%4 )) (( year100=year%100... (9 Replies)
Discussion started by: ektubbe
9 Replies

2. Shell Programming and Scripting

Help needed with date compare

Hello, I have this unix script which selects rows from DB where current time is greater than expired time (column). So this will give all the records that are expired 1 day ago, 2 days ago, 3 days ago, etc.. I need help modifying in such that it should give records that are only expired 1 day... (5 Replies)
Discussion started by: jakSun8
5 Replies

3. Post Here to Contact Site Administrators and Moderators

Broken link FAQ date arithmetic with shell

page unix com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html Date Arithmetic with the Shell has link of www samag com/documents/s=8284/sam0307b/0307b.htm which is no longer. Is this the correct place to post this?:confused: and I got message... (1 Reply)
Discussion started by: dgerman
1 Replies

4. Shell Programming and Scripting

Date and time Arithmetic

Hi, I need to process a file which contains below data. Usually the files contains both Start and Finish time. but for Few records, it contains only Start. For those records I need to add the finish line by adding 5 minutes to Start time. Started BBIDX Tue Jun 1 15:15:11 EDT 2010 292308... (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

5. Shell Programming and Scripting

How to perform arithmetic operation on date

Hi all, I would appreciate if anyone knows how to perform adding to date. As for normal date, i can easily plus with any number. But when it comes to month end say for example 28 Jun, i need to perform a plus with number 3, it will not return 1 Jul. Thanks in advance for your help. (4 Replies)
Discussion started by: agathaeleanor
4 Replies

6. Shell Programming and Scripting

Help needed-calculate previous date

Hi Friends, Need a command/script in unix which calculates previous date from current date. For ex: If current date= 01 Jan 2008, then output =31 Dec 2007 If current_date =01 Aug 2008 , then output= 31 July 2008 Please advice Regards, Suresh (3 Replies)
Discussion started by: sureshg_sampat
3 Replies

7. UNIX for Dummies Questions & Answers

Help needed with date

How can i assign a variable by the name of CUTDATE= today date - 90 days? i have something like this right now :- today=`date '+%Y%m%d'` #cutdate = this is where i am having problem. i need today - 90 days How can i accomplish this? After that i need to do delete the data which are more... (16 Replies)
Discussion started by: chris1234
16 Replies

8. Shell Programming and Scripting

Help Needed in Date Condition !!

I am writing an shell script , which reads a file with contents Example :: (( This Format is yyyy-MM-DD)) 2008-05-16 2008-05-17 2008-05-18 2008-05-19 2008-05-20 2008-05-21 when i run the shell script it has to check the current date and compare it with the todays date and give me... (24 Replies)
Discussion started by: ranga27
24 Replies

9. Shell Programming and Scripting

help needed in date format

i need to grep date in the format year-month-day,,,,,,, actually i need to grep those dates other than current date.......... can anyone help me in this...........i need a format of date which would grep previous date except current date (1 Reply)
Discussion started by: ali560045
1 Replies

10. Shell Programming and Scripting

Help needed on Date command

Hi, I am facing one problem with date command.Actually I want to use this command to get the last month,not the current month..OK,I can do current month - 1 and give special condition for january,But this time i need last month as strings like January,februaury,march etc... There is option... (5 Replies)
Discussion started by: nikunj
5 Replies
Login or Register to Ask a Question