Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minute differences.

Status
Not open for further replies.

btinder

Programmer
Mar 12, 2004
27
US
Hi all,

I have a process that wakes up, works for a little bit, then goes back to sleep for 5 minutes, wakes up, etc...

It outputs in a log file when its sleeping and when its working.

I'd like to have a script that will figure out the difference in minutes between when it went to sleep and the current time.

The problem I have is when its 12:00 and if it went to sleep at 11:57.

Are there commands and/or functions to handle differences in time? Thanks for any help...
 
convert your hh:mm into minutes and do your math in minutes

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I did that, but I had issues when the current time is 00 and the previous time was 57. I was wondering if there's a better way to handle that situation.
 
> 12:00 and if it went to sleep at 11:57.
t1 = 12*60+0 = 720
t2 = 11*60+57 = 717
Therefore, difference in seconds is 720-717

--
 
Convert times to minutes; subtract start time from end time; if negative then started before mignight so add 24 hours (1440 mins); print result....
Code:
#!usr/bin/ksh

start="23:45"
end="00:25"

((start_mins = $(expr substr "$start" 1 2)*60 + $(expr substr "$start" 4 2)))
((end_mins = $(expr substr "$end" 1 2)*60 + $(expr substr "$end" 4 2)))
((elapsed_mins = end_mins - start_mins))

if [[ "$elapsed_mins" -lt 0 ]]
then
   ((elapsed_mins += 1440))
fi

if [[ "$elapsed_mins" -gt 59 ]]
then
   print $((elapsed_mins / 60)) hours and $((elapsed_mins % 60)) minutes
else
   print $elapsed_mins minutes
fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top