- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Calculate time difference in Unix shell script
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
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
тАО12-10-2002 06:08 PM
тАО12-10-2002 06:08 PM
I have a question on shell scripting. I have a scenario whereby an application has captured the start timestamp on a file in the format of
2002-11-14 09:31 when the application services started up.
I am required to produce a script to calculate the time difference between the current system date with the timestamp captured on the file I have mentioned above. The checking is scheduled to run every 4 hours.
I am wondering is there a way that I can do a conversion of the time captured on file into a value where I can do the mathematical expression such as subtraction.
My objective is to subtract the current system date with the timestamp that was captured on the file.
time_difference=curr_date - prev_timestamp
How can this be done if I were to use the shell script to calculate the time difference. Appreciate if you could give me some advice on how to get this done. Thank you.
Regards,
Melvin
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-10-2002 06:19 PM
тАО12-10-2002 06:19 PM
Re: Calculate time difference in Unix shell script
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xea948cc5e03fd6118fff0090279cd0f9,00.html
Plus have a look here for a lot of scripts:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x026250011d20d6118ff40090279cd0f9,00.html
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2002 12:13 AM
тАО12-11-2002 12:13 AM
Solutionthere is probably no easy way of doing that. I was once requested to do something similar and I ended up making the attached script. It is slow but can return the number of seconds elapsed since midnight, Jan 1 of a specified year, e.g. 1970 and a certain specified date.
Example:
# ./return_seconds.sh 1970 2002 11 14 9 31 00
# 1037266260
The attached script can - if executed without parameters - also calculate the number of seconds elapsed since midnight Jan 1 1970 and current system time, e.g.:
# ./return_seconds.sh
1039595802
Difference:
# echo 1039596799 - 1037266260 | bc
2330539
However, if you do not expect the time difference to be bigger than a few days you can speed up things by only counting back to midnight of e.g. the current year, e.g.:
# date
Ons. 11 Dec. 2002 08:45
# ./return_seconds.sh 2002 2002 Dec 11 8 45 00
29753100
./return_seconds.sh 2002 2002 11 14 9 31 00
27423060
# echo 29753100 - 27423060 | bc
2330040
As time is passing the two results are not identical...
Hope it is of some use, but if you wait long enough I am sure someone will come up with a perl one-liner!
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2002 12:28 AM
тАО12-11-2002 12:28 AM
Re: Calculate time difference in Unix shell script
the same question:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x31d5cdec06f1d61190050090279cd0f9,00.html
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2002 01:16 AM
тАО12-11-2002 01:16 AM
Re: Calculate time difference in Unix shell script
Simple C program (compile with make) then use within a shell script like :
#!/usr/bin/sh
# get date in VAR for example :
VAR=$(cat file | line | cut -c1-2)
# use my prog (each field from date is an arg)
myprog $(echo $VAR | tr ':-' ' ')
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2002 02:08 AM
тАО12-11-2002 02:08 AM
Re: Calculate time difference in Unix shell script
I am not aware of a shell command that would make this easy; but if you look at
http://docs.hp.com/cgi-bin/onlinedocs.py?mpn=B2355-90683&service=hpux&path=../B2355-90683/00/00/89&title=HP-UX%20Reference%20Volume%204%3A%20Section%203
you'll find how to use difftime() to get the time difference between two dates...
Cheers,
FiX
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2002 03:23 AM
тАО12-11-2002 03:23 AM
Re: Calculate time difference in Unix shell script
Quick bit of perl would also do it:
perl -e '{use Time::Local;($y,$mo,$d,$h,$mi)=split /:| |-/,shift;print time-timelocal(0,$mi,$h,$d,$mo-1,$y-1900),"\n";}' "2002-12-11 11:22"
Rgds, Robin.