Sign in to follow this  
Followers 0
golfjrr

Simple Elapsed Time Calculator

5 posts in this topic

Input two times, It gives you the elapsed time in format HH:MI:SS

Note: it only works in a 24 hours period, but will work over midnight (i.e. 23:22:11 to 01:33:22)

Probably easier ways to do it, but I'm an autoit newb and this works...

#comments-start
 Elapsedtime.ai3
 By: snipeye@golfc.com
 Autoit 3 Function to compute elapsed time
 Will only work within a 24 hour period but will cross midnight
 Parameters $Oldtime = earlier time, $NewTime = later time
 If NewTime < OldTime it assumes you are crossing days 
 Format for both inputs is "HH:MI:SS" or "HHMISS"
 11/05/2004 Written
#comments-end

;below for testing
;
;$Oldtime = "23:01:10"
;$NewTime = "23:02:18"
;or you can omit the colons (i.e. "230110")
;$junk = ElapsedTime ($Oldtime, $NewTime)
;splashtexton ("Elapsed",$junk, 150,20)
;sleep (5000)
;splashoff ()
;exit

Func ElapsedTime ( $OldTime, $NewTime )
Local $OldTime,$NewTime,$DiffTime, $Ohour,$Ominute,$Osecond,$Nhour,$Nminute,$Nsecond
Local $Dhour, $Dminute, $Dsecond, $Otot, $Ntot, $Diffwork, $Diffmin, $Diffsec, $Diffhour
;
;Determine how to break up time based on colon or not
;
If StringInStr ($Oldtime, ":") = 0 then
    $Ohour =   number ( stringmid ($OldTime,1,2) )
    $Ominute = number ( stringmid ($OldTime,3,2) )
    $Osecond = number ( stringmid ($OldTime,5,2) )
else
    $Ohour =   number ( stringmid ($OldTime,1,2) )
    $Ominute = number ( stringmid ($OldTime,4,2) )
    $Osecond = number ( stringmid ($OldTime,7,2) )
endif
If StringInStr ($NewTime, ":") = 0 then
    $Nhour =   number ( stringmid ($NewTime,1,2) )
    $Nminute = number ( stringmid ($NewTime,3,2) )
    $Nsecond = number ( stringmid ($NewTime,5,2) )
else
    $Nhour =   number ( stringmid ($NewTime,1,2) )
    $Nminute = number ( stringmid ($NewTime,4,2) )
    $Nsecond = number ( stringmid ($NewTime,7,2) )
endif

If $Ohour > $Nhour then $Nhour = $Nhour + 24     ;Rolled over a day so add 24 hours

;
;Now do the math
;
$Otot = ($Ohour * 60 * 60) + ($Ominute * 60 ) + $Osecond ;convert to seconds
$Ntot = ($Nhour * 60 * 60) + ($Nminute * 60 ) + $Nsecond ;convert to seconds
$Diffwork = $Ntot - $Otot         ;This is the difference in seconds

select 
    case $Diffwork < 60     ;No hours or minutes, only seconds
         $Diffhour = "00"
         $Diffmin = "00"
         $Diffsec = $Diffwork   ;seconds
        
    case $Diffwork < 3600   ;No hours, just minutes and seconds
         $DiffHour = "00"
         $Diffmin = Int($Diffwork/60)  ;minutes
         $Diffsec = mod($Diffwork,60)  ;seconds
     
    case else              ;Hours, minutes, and seconds included
         $DiffHour  = Int($Diffwork/3600)  ;Hours
         $Diffwork = $Diffwork-3600    ;Subtract out hours
         $Diffmin = Int($Diffwork/60)  ;minutes
         $Diffsec = mod($Diffwork,60)  ;seconds
    
endselect

$DiffTime = StringFormat("%02i:%02i:%02i", $DiffHour, $DiffMin, $DiffSec)

return $DiffTime  ;Difference in time in hh:mi:ss to be formatted as such

endfunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Many ways to do this. Here is a different approach.

$Oldtime = "23:03:10"
$NewTime = "23:03:09"
$out=elapsedtime($OldTime, $NewTime )
msgbox(1,"",$out)


Func ElapsedTime ( $OldTime, $NewTime )
$old=StringSplit($OldTime,":")
$new=StringSplit($NewTime,":" )
$Oseconds=$old[3]+($old[2]*60)+($old[1]*3600)
$Nseconds=$new[3]+($new[2]*60)+($new[1]*3600)
if $Oseconds>$Nseconds then $Nseconds=$Nseconds+24*3600

$outsec=$Nseconds-$Oseconds
$hour=Int($outsec/3600)
$min=Int(($outsec-($hour*3600))/60)
$sec=$outsec-($hour*3600)-($min*60)
$DiffTime = StringFormat("%02i:%02i:%02i", $hour,$min,$sec)
return $DiffTime
endfunc

oh yea, and found a slight flaw in yours while debugging my own version:

Try

$Oldtime = "23:03:10"

$NewTime = "23:03:09"

This is 24hours, 59min,and 59seconds, I doubt you wanted to return -1 sec.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I have developed a bunch of date functions back in february to be included in the standard UDF library being compiled ...

It includes a _DateDiff function:

#include <datenew.au3>
Dim $HH, $MM, $SS
$SS = _DateDiff('s', "2005/01/01 02:01:01", "2005/01/01 04:20:30")
_TicksToTime($ss * 1000, $HH, $MM, $SS)
MsgBox(0, 'test', StringFormat("%02i:%02i:%02i", $HH, $MM, $SS))

This is the list of extra function on top of the current DATE.AU3:

; Added: _DateAdd, _DateDiff, _DateToDayValue, _DayValueToDate

; _FormatDateTime, _DayOfWeek, _DateIsValid

; _SplitDateTime, _Now(), _NowTime(), _NowDate(),_DaysInMonth()

; _DaysOfWeek(), Nowcalc(), _JulianDayNo(), _JulianToDate($iJDay)

Edited by Jon

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Always nice to write your own and debug all the possible problems out :)

But Premade ones are great time savers.

My JedB lookalike version:

;===============================================================================
;
; Description:      Returns the difference in time. if $Oldtime>$Newtime it assumes a day has passed
; Parameter(s):     $Oldtime = in format of hour:min:sec "23:03:10"
;                   $NewTime = in format of hour:min:sec "01:03:09"
;
; Requirement(s):   None
; Return Value(s):  On Success - time in format of 23:03:10
; Author(s):        Duane <scriptkitty@autoitscript.com>
; Note(s):          English only
;
;===============================================================================
Func _ElapsedTime ( $OldTime, $NewTime )
$old=StringSplit($OldTime,":")
$new=StringSplit($NewTime,":" )
$Oseconds=$old[3]+($old[2]*60)+($old[1]*3600)
$Nseconds=$new[3]+($new[2]*60)+($new[1]*3600)
if $Oseconds>$Nseconds then $Nseconds=$Nseconds+24*3600

$outsec=$Nseconds-$Oseconds
$hour=Int($outsec/3600)
$min=Int(($outsec-($hour*3600))/60)
$sec=$outsec-($hour*3600)-($min*60)
$DiffTime = StringFormat("%02i:%02i:%02i", $hour,$min,$sec)
return $DiffTime
endfunc
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

ScriptKitty's version worked best for me, but I was getting weird results if the new minutes were :00. A look at the returned array showed me that my $NewTime and $OldTime were coming from the script with the date included, so the hour ($old[1]) included the date and messed up the calculations. I solved the problem by using StringRight on the $old[1].

I replaced:

$Oseconds=$old[3]+($old[2]*60)+($old[1]*3600)
$Nseconds=$new[3]+($new[2]*60)+($new[1]*3600)

with StringRight($xxx[1],2) to eliminate the hours --- now the script works everytime

$Oseconds=$old[3]+($old[2]*60)+(StringRight($old[1],2)*3600)
$Nseconds=$new[3]+($new[2]*60)+(StringRight($old[1],2)*3600)

I love AutoIt3!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0