1833875 Members
1896 Online
110063 Solutions
New Discussion

Date question

 
SOLVED
Go to solution
Kris Spander
Advisor

Date question

Hello,

I have a problem that ought to be very simple. I can't find a utility that will let me calculate the date one week from some date. For example, if I have a date of 01-06-2001, how can I get 08-06-2001? This example is easy but it becomes more difficult at the end of a month or the end of a year. My dates need to be dd-mm-yyyy. I've already read the date man pages so please don't ask me to do that.

Thanks in advance for any help.

Kris
Dilbert is my hero.
6 REPLIES 6
Michael Tully
Honored Contributor

Re: Date question

Hi,

I'm not taking credit for this, but Sir A. Clay Stephenson's date hammer will get most people's vote in here. Here is one link which has it. My suggestion is to do a search from over here <-- on 'caljd.sh' on you'll find many examples as well!

http://forums.itrc.hp.com/cm/QuestionAnswer/1,11866,0x06ed660142b2d5118ff10090279cd0f9,00.html

Cheers
~Michael~
Anyone for a Mutiny ?
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Date question

Hi Kris:

I ought to tell you to use that little search button but since you are a new Forums member, I'll make nice and I will admit that your request is a little tricky. It's time to introduce you to my date hammer (now where's your date nail), 'caljd.sh'. This guy takes calendar dates and turns them into Julian Days (~ number of days since 4713BCE) and also does the reverse.

This should fix you:
-------------------------------------------
#!/usr/bin/sh

while [ $# -ge 1 ]
do
DT=${1}
DT7=$(caljd.sh -e -S "-" $(caljd.sh -n 7 -e $(echo "${DT}" | awk 'BEGIN {FS="-"} print $1,$2,$3}')))
echo "${DT7}"
shift
done
exit 0

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

Note the "\" line continuation characters. If I haven't made a stupid typo that should work,
processing each argument supplied to this script. You will need to use the attached script 'caljd.sh' and make sure that it is in your PATH. If you are completely confused (and you should be at this point), after you install caljd.sh, execute caljd.sh -u and it will give full usage. Let me know if I messed up all this typing because there were enough ('s and $'s to confuse anybody. In case you are wondering where the one week came in, that's the -n 7, the -e says dd/mm/yyyy format and the -S "-" says use "-" to separate the fields.

Regards, Clay

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

Re: Date question

Hi again Kris:

Forget my "Note the '\' line continuation character" comments. They were there when I posted but it looks like they were magically removed when I submitted. I should also tell you that there is a Perl version, caljd.pl that you should be able to retrieve with a search. The arguments are exactly the same, you simply substitute caljd.pl for caljd.sh. It's your choice.

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

Re: Date question

Hi (hopefully last time) Kris:

I'm an idiot. I missed a '{' just before the 'print' statement in the awk part of this script. It should be:
awk 'BEGIN {FS="-"}{print $1,$2,$3}'

The rest looks ok but I could have easily missed something else.

If it ain't broke, I can fix that.
Kris Spander
Advisor

Re: Date question

Thanks to both of you. I love sitting down at my desk and having an answer first thing in the morning!

Clay, your corrected script worked the first time. I had no idea what all of it was doing so I ran each part of it starting from the right and working to the left to understand what was happening. I'm still confused but it works!

It took me a while to realize that $(awk) was the same thing as `awk`. Did you really just type those commands in and hit SUBMIT or did you cut and paste from an existing script?

Thanks very much,
Kris
Dilbert is my hero.
A. Clay Stephenson
Acclaimed Contributor

Re: Date question

Hi Kris:

I just typed it in otherwise I probably wouldn't have missed that stupid '{'. I felt really dumb because I looked at that line for 30 seconds before I hit the button. And yes the $(command) syntax is now preferred over the older `command` syntax especially in cases where the $(commnds) are heavily nested.

No more points please, Clay
If it ain't broke, I can fix that.