Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Date e.g. 05-OCT-03 back 7 days

Status
Not open for further replies.

jollyroger

Technical User
Mar 22, 2001
77
GB
Hello there
I am sure my problem is a common one, but I cannot find any reference to it within my OS (I was going to shell script it but....)
I have an SQLPLUS Script which would be called up from the command line / cron. This script will accept parameters and those will need to be in a set format.
Now the question is this. Today's date is easy, even yesterdays (as I have another script which records this in a file), but 7 days ago in the format DD-MON-YY?
What I need is the ability to:
Convert 05-OCT-03 to a number (Julian?)
Minus the number of days I want e.g. 7
Convert the resulting number back to the above format for a date. e.g. 28-SEP-03
Any idea's???
Best regards
Ben
 
Go via epoch time. Use something like Date::parse to convert your input date to an epoch time, add or subtract your increment in seconds and then convert back to whatever format you want using Time::Localtime and sprintf.

There are a few gotchas:

When converting to epoch time from just a date, set the unused time parts to noon. If not, keep it away from midnight or you might get bitten by daylight savings time.

Default the year to the current year if it is not in your input. Leap years can break things.

Good luck, "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
I wrote a simple script to do something similar. I wrote a script to convert between julian and regular dates.

It simply just takes a julian date as an argument and spits out the date in the form of MMDDYY. So in my scripts I use date +%j to figure out the current julian date, then I use expr $curdate - 7 to get seven days back, then I use the script to figure out the real date.

I have not modified it yet to account for leap years, but it does work.

Hope this helps!


#!/usr/bin/ksh

year=03
fin=0

jan=31
feb=59
mar=90
apr=120
may=151
jun=181
jul=212
aug=243
sep=273
oct=304
nov=334
dec=365

if [ $1 -lt $jan ];
then month=01
emonth=0;
elif [ $1 -eq $jan ];
then date=0131$year
fin=1;
elif [ $1 -lt $feb ];
then month=02
emonth=31;
elif [ $1 -eq $feb ];
then date=0228$year
fin=1;
elif [ $1 -lt $mar ];
then month=03
emonth=59;
elif [ $1 -eq $mar ];
then date=0331$year
fin=1;
elif [ $1 -lt $apr ];
then month=04
emonth=90;
elif [ $1 -eq $apr ];
then date=0430$year
fin=1;
elif [ $1 -lt $may ];
then month=05
emonth=120;
elif [ $1 -eq $may ];
then date=0531$year
fin=1;
elif [ $1 -lt $jun ];
then month=06
emonth=151;
elif [ $1 -eq $jun ];
then date=0630$year
fin=1;
elif [ $1 -lt $jul ];
then month=07
emonth=181;
elif [ $1 -eq $jul ];
then date=0731$year
fin=1;
elif [ $1 -lt $aug ];
then month=08
emonth=212;
elif [ $1 -eq $aug ];
then date=0831$year
fin=1;
elif [ $1 -lt $sep ];
then month=09
emonth=243;
elif [ $1 -eq $sep ];
then date=0930$year
fin=1;
elif [ $1 -lt $oct ];
then month=10
emonth=273;
elif [ $1 -eq $oct ];
then date=1031$year
fin=1;
elif [ $1 -lt $nov ];
then month=11
emonth=304;
elif [ $1 -eq $nov ];
then date=1130$year
fin=1;
elif [ $1 -lt $dec ];
then month=12
emonth=334;
elif [ $1 -eq $dec ];
then date=1231$year
fin=1;
fi

if [ $fin -eq 1 ];
then print $date
else
day=`expr $1 - $emonth`
if [ $day -lt 10 ];
then day=0$day;
fi
date=$month$day$year
print $date
fi

exit 0
 
You can modify the forth from last line to display your date however you want it. Also, you have to put the crrent year in at the top. That could easily be substitued by:

from:
year=03

to:
year=`date +%y`

Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top