1838671 Members
5987 Online
110128 Solutions
New Discussion

Date Calculations

 
SOLVED
Go to solution
Derek Card
Advisor

Date Calculations

Hello Forumers,

Thanks to some assistance from you guys, I have been able to join some files to produce output data like this:

1009 1-Jan-1999 12-Jun-1999
2063 24-Oct-2001 12-Dec-2001
3741 12-Feb-2002 ----------

The first field is a unique ID No. The second field is a start date and the third field is an ending date. What I need to do is find those lines which have the dashed ending date field. They are still active. I think I can do that but the hard part is to then determine if the starting date is about to expire. I want to issue a warning 1 week before the 90 day period is up. Basically, I need something that will work if today's date - starting date >= 83 days. I want to output the ID no. and the days left on one line.

I am trying to replace several C programs with scripts. I've looked at the date command but I am concerned about the calculations where the year changes.

Thanks in advance for any and all help,
Derek Card
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Date Calculations

Hi Derek:

Whenever I see date calculations, I perk up. This actually seems easy with my date sledgehammer, caljd.sh.

#!/usr/bin/sh

TODAY=$(caljd.sh)
MAXDAYS=83
while read ID START_DATE END_DATE
do
X=$(echo "${END_DATE}" | tr -d "-")
if [[ -z "${X}" ]]
then
JD_START_DATE=$(caljd.sh -e -i -S '-' -c ${START_DATE})
DAYS=$((${TODAY} - ${JD_START_DATE}))
if [[ ${DAYS} -ge ${MAXDAYS} ]]
then
echo "${ID}\t${DAYS}"
fi
fi
done
exit 0

--------------------------------------

You would use it like:
expire.sh < myfile > newfile

If I haven't made a typo that should do it.

Here's caljd.sh. caljd.sh -u will give full usage including a number of examples.



If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Date Calculations

Hi again:

You can also use the Perl version, caljd.pl. The arguments are exactly the same. In looking over my earlier example, I realized that you could put a grep "--------" in front of it and make it faster.

Here's caljd.pl.
If it ain't broke, I can fix that.
Derek Card
Advisor

Re: Date Calculations

Hi Clay,

Thanks but there seems to be something wrong with your script. I had already searched and found caljd.sh. I keep getting an error message: /usr/bin/caljd.sh Invalid args. Any ideas?

Derek
A. Clay Stephenson
Acclaimed Contributor

Re: Date Calculations

Derek,

I don't see anything wrong but I just typed this guy in 'off the cuff' so I could have very well missed a pesky '{' or '(' but it's not jumping out at me.

You mention that you did a search. As a guess, are you using a version < 2.1? The -c, -i, and -I options to allow it to recognize dates like 1-January-2002 were not added until Version 2.1. I hope it's that; otherwise, it's my advanced one-finger hunt-and-peck typing technique but again I don't see that in my earlier posting.

Regards, Clay
If it ain't broke, I can fix that.
Martin Johnson
Honored Contributor

Re: Date Calculations

Clay,

I don't see any typos either. That is usual, you almost always have at least one. :-) Maybe you are just getting more sneaky.

:-)
Marty
Derek Card
Advisor

Re: Date Calculations

Clay, dude you are good. I had downloaded an earlier version (2.05). As soon as I used the version that you attached, the script worked like a champ!

Thanks again,
Derek