Operating System - HP-UX
1819836 Members
3360 Online
109607 Solutions
New Discussion юеВ

Date variable minus 60 days - how to calculate?

 
SOLVED
Go to solution
Maureen Gunkel
Trusted Contributor

Date variable minus 60 days - how to calculate?

Please ignore the previous - hit the Enter key by mistake.
Anyhoo, I'm trying to manipulate the date a user inputs while running a shell script to subtract 60 days from it. I have a VERY klutzy way of doing it, but my way won't work with months <> 30 days (approx. half of them!). I don't know C or Perl, and am tring to do this in C-shell, perhaps with awk?

Thanks to anyone who can help!!
No matter where you go, there you are.
8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: Date variable minus 60 days - how to calculate?

There is a set of GNU Utilities that has a date utility that will do exactly what you want. I currently use it to do date calculations. It should handle months without a problem.

Try to following url to get the bundle of utilities: http://www.gnu.org/gnulist/production/shellutils.html

Patrick Wallek
Honored Contributor

Re: Date variable minus 60 days - how to calculate?

You can also try this url at the HPUX Software Porting Center for precompiled versions of the utilities. These are easier to install than having to compile the stuff you get from the gnu site.

http://hpux.cae.wisc.edu/hppd/hpux/Gnu/sh_utils-2.0/
Madhu Sudhan_1
Respected Contributor

Re: Date variable minus 60 days - how to calculate?

Maureen:

Here is the URL which talks about date comparisons using perl.

http://webmaster.indiana.edu/perl56/pod/perlfaq4.html#data:%20dates

Hope this helps.

......Madhu
Think Positive
Maureen Gunkel
Trusted Contributor

Re: Date variable minus 60 days - how to calculate?

Although all those URL's are very helpful, they don't address my need (I don't think). I'm not using the current date, I'm using a date input by the user. For example, I ask them what date, they enter '10/26/00', which is assigned to dte1, and I need to find out what $dte1 - 60 would be. I hope this is clearer.
No matter where you go, there you are.
Al Langen
Advisor
Solution

Re: Date variable minus 60 days - how to calculate?

Copy the lines after the *'s into a file and call it whatever.awk. In your script you might have:
read curdate
olddate=`echo $curdate | awk -f whatever.awk`

Hope this helps.

***************************************

BEGIN{ FS="/"
split( "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month," ");
split( "31 28 31 30 31 30 31 31 30 31 30 31", days, " ");
}
{
year = $3
leapyear = year % 4;
if( leapyear == 0 )
days[2] = 29
else
days[2] = 28;
doy = 0;
for ( i = 1; i< $1; i++ )
doy = doy + days[i]
doy = doy + $2;
if ( doy <= 60 )
{
doy = doy + 365;
year = year - 1;
leapyear = year %4;
if( leapyear == 0 )
{
days[2] = 29;
doy = doy + 1;
}
}
doy = doy - 60;
for ( i = 1; i <= 12; i++ )
{
doy = doy - days[i];
if ( doy <= 0 )
{
doy = doy + days[i];
break;
}
}
printf( "%d/%d/%dn", i, doy, year )
}
It's not the ups and downs in life, it's the jerks. A. E. Newman
Ralph Grothe
Honored Contributor

Re: Date variable minus 60 days - how to calculate?

If you are prepared to call an external script/prog from your script this can easily be done with a Perl scriplet (although you mentioned your dislike for Perl).
Of course, one could also use the ctime() standard C func and Co., maybe slightly less comfortably though.
I'm not particularily fond of the C-shell and thus didn't care much of what the C-shell has to offer in this respect.

What is the date format read as user input from your script (because it seems more of a formatting issue)?
Madness, thy name is system administration
Maureen Gunkel
Trusted Contributor

Re: Date variable minus 60 days - how to calculate?

Al -
Bee-yoo-ti-ful! I have a slight problem with the format of the date returned, but I can fix that. You've saved my bacon! Thanks again!
No matter where you go, there you are.
Al Langen
Advisor

Re: Date variable minus 60 days - how to calculate?

Maureen,

You might want to change the printf format in order to print leading 0's or not.

It's not the ups and downs in life, it's the jerks. A. E. Newman