- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Need help ASAP
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 04:11 AM
тАО03-09-2006 04:11 AM
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
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 04:18 AM
тАО03-09-2006 04:18 AM
Re: Need help ASAP
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Tags:
- caljd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 04:28 AM
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 04:45 AM
тАО03-09-2006 04:45 AM
Re: Need help ASAP
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 04:49 AM
тАО03-09-2006 04:49 AM
Re: Need help ASAP
There were some odd characters which found their way into my script.
THANK YOU
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 04:51 AM
тАО03-09-2006 04:51 AM
Re: Need help ASAP
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 05:01 AM
тАО03-09-2006 05:01 AM
Re: Need help ASAP
#!/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}"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 05:05 AM
тАО03-09-2006 05:05 AM
Re: Need help ASAP
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 05:08 AM
тАО03-09-2006 05:08 AM
Re: Need help ASAP
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2006 05:17 AM
тАО03-09-2006 05:17 AM
Re: Need help ASAP
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