1753452 Members
6047 Online
108794 Solutions
New Discussion юеВ

Re: Need help ASAP

 
SOLVED
Go to solution
OFC_EDM
Respected Contributor

Need help ASAP

ok this really isn't the most critical of issues except I need to get this figured out ASAP to make my boss happy.

I need to store in a varable the date for
2 hours ago

Now this seemed simple. Except I started thinking about certain conditions:
1) What if it's 1 am? The date two hours ago is from the previous day. I can't just take the hour and subract 2.
2) leap year
3) what if it's 1 am on jan 1st; same as issue 1) but the year is also affected.

anyone have a solution to this?

I'm trying to accomplish this is regular shell (posix) scripting utilizing utilities like sed /awk and any built-in posix functions.

But if perl or something else is the answer then I'll just have to go that route.

Any and all help appreciated.

Cheers,
Kevin
The Devil is in the detail.
9 REPLIES 9
Steven E. Protter
Exalted Contributor

Re: Need help ASAP

Shalom Kevin

http://hpux.ws/merijn/caljd-2.23.sh

http://hpux.ws/merijn/caljd-2.2.pl

Both handle date calculations that you describe with reasonable efficiency.

Maintained by our fearless point leader, A. Clay Stephenson.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Need help ASAP

TM=$(perl -e 'print scalar localtime(time - 7200)')
echo "Two hours ago was ${TM}"

You can also extract individual fields from Perl's localtime function, if you like.
If it ain't broke, I can fix that.
OFC_EDM
Respected Contributor

Re: Need help ASAP

Interesting Mr Stephenson.

Your command works but I get the following errors: (script posted below)
./2hoursago.sh
Two hours ago was Thu Mar 9 08:43:45 2006
./2hoursago.sh[7]: /: Execute permission denied.
./2hoursago.sh[8]: /: Execute permission denied.
./2hoursago.sh[9]: /: Execute permission denied.
./2hoursago.sh[10]: /: Execute permission denied.
./2hoursago.sh[11]: /: Execute permission denied.


Here's my script:

#!/usr/bin/sh
TM=$(perl -e 'print scalar localtime(time - 7200)')
echo "Two hours ago was ${TM}"

any ideas?

The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: Need help ASAP

Fixed it and it now works.
There were some odd characters which found their way into my script.

THANK YOU
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: Need help ASAP

Thank you both for your replies.

I realized I must now learn perl. For that was way too easy.

Thank you Mr. Protter for the other links as well. They will be very useful.
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: Need help ASAP

Just as a thank you for the help and to save someone some time here's the simple code to see the different times:

#!/usr/bin/sh

TM2h=$(perl -e 'print scalar localtime(time - 7200)') # 7200 seconds = 2 hours
TM8h=$(perl -e 'print scalar localtime(time - 28800)') # 28800 seconds = 8 hours
TM24h=$(perl -e 'print scalar localtime(time - 86400)') # 86400 seconds = 24 hours
TM1w=$(perl -e 'print scalar localtime(time - 604800)') # 604800 seconds = 1 week

echo " 2 hours ago was ${TM2h}"
echo " 8 hours ago was ${TM8h}"
echo "24 hours ago was ${TM24h}"
echo " 1 week ago was ${TM1w}"
The Devil is in the detail.
Senthil Kumar .A_1
Honored Contributor

Re: Need help ASAP

Hi Kevin,

Thanks for the query, this has given me some opportunity me do some c programming/sys programming. Last done during my college days in the year 2000.

Just write a c program..

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


#include ;
#include ;
int main () {
time_t t,a;
t=time(&t);
a=t-172800;
printf("%s\n",ctime(&a));
}


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

Just compile it and execute the ./a.out

,voila it will always print the the day which is 2 days before.

172800 is 42 hours in seconds.

Regards,
Senthil Kumar .A

Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Senthil Kumar .A_1
Honored Contributor

Re: Need help ASAP

Sorry 2 hours is it,i thought 2 days..

then subract just.. 7200

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


#include ;
#include ;
int main () {
time_t t,a;
t=time(&t);
a=t-7200;
printf("%s\n",ctime(&a));
}


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

Here u do not require to worry about ..leap years,time zone, months etc..

Regards,
Senthil Kumar .A
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Senthil Kumar .A_1
Honored Contributor

Re: Need help ASAP

Hi guys,

This has given me a reason to learn perl. Hats off to Clay stevenson. WOW that perl command was too cool . I did not see others post when I was working with my c program. Sorry.

By the way ,As far as UNIX is concerned, perl has beaten C... WOW unbelievable.

Regards,
Senthil Kumar .A
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)