- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: finding difference between 2 given time.
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
тАО10-30-2003 05:24 AM
тАО10-30-2003 05:24 AM
I am writing a script which genrates the start time and the end time of some transactions. I need to find the difference between them. How do I accomplish it. For example one of the start time and end time are as follows:
start time = 021037 (2nd hour, 10th min and 37th sec)
end time = 021206 (2nd hour, 12th min and 06th sec)
How do I find the difference (89 sec) in the script.
Thanks,
Andy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:32 AM
тАО10-30-2003 05:32 AM
Re: finding difference between 2 given time.
It starts at zero when the script runs. So if you have two portions of a script you want to time, you could use:
let CHECKPOINT=$SECONDS
echo "Elapsed time for fisrt operation is $CHECKPOINT seconds"
let CHECKPOINT=${SECONDS}-${CHECKPOINT}
echo "Elapsed time for second operation is $CHECKPOINT seconds"
You can also reset the SECONDS variable to zero at key points as well if you prefer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:54 AM
тАО10-30-2003 05:54 AM
Re: finding difference between 2 given time.
What about using the time command and wrapping all your transactions into a script:
time scriptname
real 0m0.25s
user 0m0.02s
sys 0m0.02s
Where scriptname contains all your transactions.
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 06:10 AM
тАО10-30-2003 06:10 AM
Re: finding difference between 2 given time.
for example:-
IVR Start Time=195415,IVR End Time=195535
is one of the output created by my script.
Now I would need to find the difference between 195535 and 195415 which is 80sec.
How can I do this in a script.
Sorry for conveying improper message in my previous mail.
Thanks,
Anand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 06:14 AM
тАО10-30-2003 06:14 AM
Solution---------------------------------
#!/usr/bin/sh
calc_seconds()
{
typeset TIME=${1}
shift
typeset -i10 OUT=$(echo "${TIME}" | awk '{ print ((substr($0,1,2) + 0) * 3600) + ((substr($0,3,2) + 0) * 60) + (substr($0,5,2) + 0) }')
typeset -i10 STAT=${?}
echo "${OUT}"
return ${STAT}
} # calc_seconds
TIME1=021037
TIME2=021206
DIFF=$(( $(calc_seconds ${TIME2}) - $(calc_seconds ${TIME1}) ))
echo "Diff = ${DIFF} seconds."
------------------------------------
Note: I am not out of my mind (more or less) with those seemingly stupid substr() + 0's. The + 0 is a standard awk idiom to force an expression into a numeric context.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 06:23 AM
тАО10-30-2003 06:23 AM
Re: finding difference between 2 given time.
In perl:
#!/bin/perl
sub seconds{
my($t) = @_;
return ((60*substr($t,0,2)+substr($t,2,2))*60+substr($t,4,2))
}
$begin = shift @ARGV;
$end = shift @ARGV;
$diff = seconds( $end ) - seconds( $begin );
$diff += seconds("240000") if ($diff < 0);
print "Diff = $diff\n";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 06:51 AM
тАО10-30-2003 06:51 AM
Re: finding difference between 2 given time.
will you ever have to deal with a start time just before midnite and an end in the next day?
If so you will need to augment Clay's solution with an "add 86400 to diff if it is negative" like my perl answer does.
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 08:22 PM
тАО10-30-2003 08:22 PM
Re: finding difference between 2 given time.
for this kind of problem usually I used two functions I've created: ResTime & GetTime
######################################
restime() {
######################################
# Resets var
SECONDS=0
}
######################################
gettime() {
######################################
# Returns the time spent (hh:mm:ss) by commands between restime and itself #
sec=$SECONDS
hh=$(expr $sec / 3600)
mm=$(expr \( $sec - 3600 \* $hh \) / 60)
ss=$(expr $sec - 3600 \* $hh - 60 \* $mm)
printf "%.2d:%.2d:%.2d\n" $hh $mm $ss
unset sec hh mm ss
}
In your script put the restime at the top of teh script and at the end you can type:
echo $(gettime) to see how much time your script used.
This are my 2 cents.
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-23-2004 12:48 PM
тАО10-23-2004 12:48 PM
Re: finding difference between 2 given time.
I used your solution for calculating the time difference between 2 times successfully in HP-UNIX but it is giving some errors while using the same solution in SUN OS. The error is :
syntax error at line 6: `(' unexpected
Can you please help. I know this forum is for HP-UX but just wanted to know if you could help me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2004 07:31 AM
тАО10-24-2004 07:31 AM
Re: finding difference between 2 given time.
1. Change the "shebang" to #!/usr/bin/ksh.
B. Change the typeset -i10 XXX= to simply typeset -i XXX=.
III. Change 'awk' to 'nawk'. Under Solaris, awk is 'oawk' (Old Awk) so that nawk must be invoked explicitly. On HP-UX, awk is 'nawk' (New Awk). By the way, new awk is at least 15 years old and oawk is considered obsolete but I guess someone forgot to tell Sun.
Okay, that's 3 suggestions but who's counting.