Jump to content
Sign in to follow this  
Kovacic

Simple timer logic issue

Recommended Posts

Kovacic

Hi all! I am perplexed by something that should be simple. I am trying to make a small timer which will become part of a larger script, but I'm stuck on comparing the 2 times..

When you run the script, you hit OK on a message box, that's when the current system time and day of the year is saved to a variable.. when you hit OK again, the timer is stopped and the date and time are compared. I'm not using Timerinit() because I want the exact system time the timer started, and the exact system time the timer stopped. Then the time is compared and you get the difference.

This is easy as long as I am subtracting 10 seconds from 15 seconds, but what if the start timeer got second: 45 and the top timer got second: 12?

I'm sure this is cakewalk for anyone that got higher than a C in math, but I didnt. 

Can anyone shed some light on this for a simpleton like myself?

Msgbox(0,"START","Click to start timer")
$a = timestamp()


Msgbox(0,"START","Click to stop timer")
$b = timestamp()


compare($a,$b)


Func timestamp()
    Local $stSystemTime = DllStructCreate('ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort')
    DllCall('kernel32.dll', 'none', 'GetSystemTime', 'ptr', DllStructGetPtr($stSystemTime))
    $sMilliSeconds = StringFormat('%03d', DllStructGetData($stSystemTime, 8))
    $stSystemTime = 0
   return @YDay & ':' & @Hour & ':' & @Min & ':' & @Sec & ':' & $sMilliSeconds
EndFunc


Func compare($start,$stop)
$Comp_a = StringSplit($start,":",2) 
$Comp_b = StringSplit($stop,":",2)
$DayDif  = $comp_b[0] - $comp_a[0]
$HourDif = $comp_b[1] - $comp_a[1]
$MinDif  = $comp_b[2] - $comp_a[2]
$SecDif  = $comp_b[3] - $comp_a[3]
If $comp_b[4] > $comp_a[4] then
$MSecDif = $comp_b[4] - $comp_a[4]
else
$MSecDif = $comp_a[4] - $comp_b[4]
endif

Msgbox(0,"hey you, yeah you!", "Elapse Time:" & @CRLF & "Days: " & $DayDif & @CRLF & "Hours: " & $HourDif & @CRLF & "Minutes: " & $MinDif & @CRLF & "Seconds: " & $SecDif & @CRLF & "Miliseconds: " & $MSecDif  )


endfunc

Thanks in advance!!

 

EDIT:

Would this logic work?

;example

$start = 45
$stop = 12
 
$val1 = $Start - 60 ; val1 becomes 15
$Mydiff =  $stop + $val1
Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites
Melba23

Kovacic,

Why not let AutoIt do all the work for you? ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>

$hGUI = GUICreate("Test", 500, 500)

$cStart = GUICtrlCreateButton("Start", 10, 10, 80, 30)
$cStop = GUICtrlCreateButton("Stop", 10, 50, 80, 30)
GUICtrlSetState($cStop, $GUI_DISABLE)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cStart
            $sStart = _NowCalc()
            GUICtrlSetState($cStart, $GUI_DISABLE)
            GUICtrlSetState($cStop, $GUI_ENABLE)
        Case $cStop
            $sStop = _NowCalc()
            GUICtrlSetState($cStop, $GUI_DISABLE)
            GUICtrlSetState($cStart, $GUI_ENABLE)

            $iDays = _DateDiff("D", $sStart, $sStop)
            $sStart = _DateAdd("D", $iDays, $sStart)
            $iHours = _DateDiff("h", $sStart, $sStop)
            $sStart = _DateAdd("h", $iHours, $sStart)
            $iMins = _DateDiff("n", $sStart, $sStop)
            ConsoleWrite($sStart & @CRLF)
            $sStart = _DateAdd("n", $iMins, $sStart)
            ConsoleWrite($sStart & @CRLF)
            $iSecs = _DateDiff("s", $sStart, $sStop)

            MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs)

    EndSwitch

WEnd
Much easier! :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Kovacic

Kovacic,

Why not let AutoIt do all the work for you? ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>

$hGUI = GUICreate("Test", 500, 500)

$cStart = GUICtrlCreateButton("Start", 10, 10, 80, 30)
$cStop = GUICtrlCreateButton("Stop", 10, 50, 80, 30)
GUICtrlSetState($cStop, $GUI_DISABLE)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cStart
            $sStart = _NowCalc()
            GUICtrlSetState($cStart, $GUI_DISABLE)
            GUICtrlSetState($cStop, $GUI_ENABLE)
        Case $cStop
            $sStop = _NowCalc()
            GUICtrlSetState($cStop, $GUI_DISABLE)
            GUICtrlSetState($cStart, $GUI_ENABLE)

            $iDays = _DateDiff("D", $sStart, $sStop)
            $sStart = _DateAdd("D", $iDays, $sStart)
            $iHours = _DateDiff("h", $sStart, $sStop)
            $sStart = _DateAdd("h", $iHours, $sStart)
            $iMins = _DateDiff("n", $sStart, $sStop)
            ConsoleWrite($sStart & @CRLF)
            $sStart = _DateAdd("n", $iMins, $sStart)
            ConsoleWrite($sStart & @CRLF)
            $iSecs = _DateDiff("s", $sStart, $sStop)

            MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs)

    EndSwitch

WEnd
Much easier! :)

M23

 

 

That was what I planned originally but I need to use milliseconds, but it looks like _DateDiff only handles as small as seconds.. Good call though

Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites
Melba23

Kovacic,

Then we have to be creative: :)

Case $cStart
    $sStart = _NowCalc()
    $iStartMS = @MSEC
    ConsoleWrite($sStart & "." & $iStartMS & @CRLF)
    
    GUICtrlSetState($cStart, $GUI_DISABLE)
    GUICtrlSetState($cStop, $GUI_ENABLE)
Case $cStop
    $sStop = _NowCalc()
    $iStopMS = @MSEC
    ConsoleWrite($sStop & "." & $iStopMS & @CRLF)
    
    GUICtrlSetState($cStop, $GUI_DISABLE)
    GUICtrlSetState($cStart, $GUI_ENABLE)
    
    $iDays = _DateDiff("D", $sStart, $sStop)
    $sStart = _DateAdd("D", $iDays, $sStart)
    $iHours = _DateDiff("h", $sStart, $sStop)
    $sStart = _DateAdd("h", $iHours, $sStart)
    $iMins = _DateDiff("n", $sStart, $sStop)
    $sStart = _DateAdd("n", $iMins, $sStart)
    $iSecs = _DateDiff("s", $sStart, $sStop)

    ; See if Secs values are the same
    $iMSecs = $iStopMs - $iStartMS
    If StringRight($sStart, 2) <> StringRight($sStop, 2) Then
        If $iStartMS > $iStopMs Then
            $iMSecs = 1000 - $iStartMS + $iStopMS
            $iSecs -= 1
        EndIf
    EndIf

    MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs & "." & $iMSecs)
Checking the calculated time against the values written in the console it seems to be correct - over to you for more testing. :)

M23

Edit: Not quite - still working on it. :(

2nd Edit: Try now. :)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Kovacic

Kovacic,

Then we have to be creative: :)

Case $cStart
    $sStart = _NowCalc()
    $iStartMS = @MSEC
    ConsoleWrite($sStart & "." & $iStartMS & @CRLF)
    
    GUICtrlSetState($cStart, $GUI_DISABLE)
    GUICtrlSetState($cStop, $GUI_ENABLE)
Case $cStop
    $sStop = _NowCalc()
    $iStopMS = @MSEC
    ConsoleWrite($sStop & "." & $iStopMS & @CRLF)
    
    GUICtrlSetState($cStop, $GUI_DISABLE)
    GUICtrlSetState($cStart, $GUI_ENABLE)
    
    $iDays = _DateDiff("D", $sStart, $sStop)
    $sStart = _DateAdd("D", $iDays, $sStart)
    $iHours = _DateDiff("h", $sStart, $sStop)
    $sStart = _DateAdd("h", $iHours, $sStart)
    $iMins = _DateDiff("n", $sStart, $sStop)
    $sStart = _DateAdd("n", $iMins, $sStart)
    $iSecs = _DateDiff("s", $sStart, $sStop)

    ; See if Secs values are the same
    $iMSecs = $iStopMs - $iStartMS
    If StringRight($sStart, 2) <> StringRight($sStop, 2) Then
        If $iStartMS > $iStopMs Then
            $iMSecs = 1000 - $iStartMS + $iStopMS
            $iSecs -= 1
        EndIf
    EndIf

    MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs & "." & $iMSecs)
Checking the calculated time against the values written in the console it seems to be correct - over to you for more testing. :)

M23

Edit: Not quite - still working on it. :(

2nd Edit: Try now. :)

 

 

Looking much better, im looking at the Ms now, heres what I got while testing:

Start: 2014/07/03 12:21:54.200
Stop: 2014/07/03 12:21:55.879
 
 
 
Diff reported by script:  Seconds: 1   Msec: 1679
 
 
Actual diff: Seconds: 1 Msec: 679
 
I think my brain hurts lol
 
EDIT:
 
one of our calculations is close... im about to build a counting GUI to test the theory:
 
If $stop[1] >= $Start[1] then
$HourDif = $stop[1] - $Start[1]
else
$HourDif =  $Start[1] + ($stop[1] - 60)
endif
Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites
BrewManNH

I find that when comparing times it's much easier to convert both times to msec's subtract one from the other, and then turn that result into hh:mm:ss:ms rather than trying to do the math on the raw data.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Kovacic

I find that when comparing times it's much easier to convert both times to msec's subtract one from the other, and then turn that result into hh:mm:ss:ms rather than trying to do the math on the raw data.

 

that was one of my earlier thoughts, however I will need to grab the current time and day, then a later time and day, and compare the two. The only way I can break that down to Msec is if I grab the current MS since the beginning of the year and calculate since then.


C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites
Melba23

Kovacic,

A couple of glasses of good red wine later - this might be the solution (with a big shout to BrewManNH for his suggestion above): ;)

Case $cStop
    $sStop = _NowCalc()
    $iStopMS = @MSEC
    ConsoleWrite($sStop & "." & $iStopMS & @CRLF)

    GUICtrlSetState($cStop, $GUI_DISABLE)
    GUICtrlSetState($cStart, $GUI_ENABLE)

    $iStartMs =  (Number(StringRight($sStart, 2)) * 1000) + $iStartMS
    $iStopMs = (Number(StringRight($sStop, 2)) * 1000) + $iStopMS
    $iDiffMs = $iStopMs - $iStartMS

    $iDays = _DateDiff("D", $sStart, $sStop)
    $sStart = _DateAdd("D", $iDays, $sStart)
    $iHours = _DateDiff("h", $sStart, $sStop)
    $sStart = _DateAdd("h", $iHours, $sStart)
    $iMins = _DateDiff("n", $sStart, $sStop)
    $sStart = _DateAdd("n", $iMins, $sStart)
    $iSecs = Int($iDiffMs / 1000)
    $iMSecs = $iDiffMs - ($iSecs * 1000)

    ConsoleWrite($iSecs & "." & $iMSecs & @CRLF)
I cannot break it after a good few tests - your turn. :D

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Chimp

also this could do

MsgBox(0, "START", "Click to start timer")
$a = timestamp()

MsgBox(0, "STOP", "Click to stop timer")
$b = timestamp()

compare($a, $b)

Func timestamp()
    Return @YDAY & ':' & @HOUR & ':' & @MIN & ':' & @SEC & ':' & @MSEC
EndFunc   ;==>timestamp


Func compare($start, $stop)
    $Comp_a = StringSplit($start, ":", 2)
    $Comp_b = StringSplit($stop, ":", 2)
    Local $comp_c[5]

    ; start
    $Comp_a[0] = $Comp_a[0] * 86400000 ; Day
    $Comp_a[1] = $Comp_a[1] * 3600000 ; Hour
    $Comp_a[2] = $Comp_a[2] * 60000 ; Mins
    $Comp_a[3] = $Comp_a[3] * 1000 ; Sec

    ; stop
    $Comp_b[0] = $Comp_b[0] * 86400000 ; Day
    $Comp_b[1] = $Comp_b[1] * 3600000 ; Hour
    $Comp_b[2] = $Comp_b[2] * 60000 ; Mins
    $Comp_b[3] = $Comp_b[3] * 1000 ; Sec

    Local $started = $Comp_a[0] + $Comp_a[1] + $Comp_a[2] + $Comp_a[3] + $Comp_a[4]
    Local $stopped = $Comp_b[0] + $Comp_b[1] + $Comp_b[2] + $Comp_b[3] + $Comp_b[4]

    Local $elapsed = $stopped - $started ; elapsed time in milliseconds

    ; rebuild "days/time"
    $comp_c[0] = Int($elapsed / 86400000) ; days
    $elapsed -= $comp_c[0] * 86400000
    $comp_c[1] = Int($elapsed / 3600000) ; hours
    $elapsed -= $comp_c[1] * 3600000
    $comp_c[2] = Int($elapsed / 60000) ; mins
    $elapsed -= $comp_c[2] * 60000
    $comp_c[3] = Int($elapsed / 1000) ; secs
    $elapsed -= $comp_c[3] * 1000
    $comp_c[4] = $elapsed ; milliseconds

    Local $duration = $comp_c[0] & ":" & StringFormat("%02s", $comp_c[1]) & ":" & StringFormat("%02s", $comp_c[2]) & ":" & StringFormat("%02s", $comp_c[3]) & ":" & StringFormat("%03s", $comp_c[4])

    MsgBox(0, $duration, "Started at " & $start & @CRLF & "Stopped at " & $stop & @CRLF & "Elapse Time : " & @CRLF & " Days : " & $comp_c[0] & @CRLF & " Hours : " & $comp_c[1] & @CRLF & " Minutes : " & $comp_c[2] & @CRLF & " Seconds : " & $comp_c[3] & @CRLF & " Miliseconds : " & $comp_c[4])
EndFunc   ;==>compare

p.s.

is there a glass of wine for me too? :)


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Kovacic

Between those two answers, I think that solved it!! Thanks!!!!!!

WINE (or beer) FOR ALL!

Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites
Chimp

a cold beer for me, thanks :P


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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  

  • Similar Content

    • Jemboy
      By Jemboy
      Hi,
      Ones(some times twice) a month I get an e-mail with zip file, which has price updates from a supplier.
      I have to upload the file to an FTP to get it processed.
      When uploading the file, it will get "timestamped" with the time and date at which time the file was uploaded.
      Normally this is fine, because I mostly upload the file the same day.
      Sometimes it may take 1 or 2 days before I can upload the file.
      For historical purpose, I would like to have the file timestamped with the original date.
      I have tried using:  _FTP_Command ( $hFTPSession, "MFCT YYYYMMDDHHMMSS path") 
      however this command does not work or change the timestamp as I expected.
      Does anyone now a way how I can change the timestamp of a FTP-file?
    • Rskm
      By Rskm
      Hi, for a script of mine, I wish to keep a time frame on till how the program will work.. basically, I want the program to read the date and if the read date < 'a particular date', the program continues, else it exit. I don't want to read the time from the computer as the program can be fooled by resetting the pc time. is there a way to read time from internet - so that the user cannot fool the program by date reset? thanks
    • Shedunn
      By Shedunn
      I've been looking through some different functions(not sure if that's the right word) to get the Date and time and I can't find one that doesn't include "/ " or ":" in what is returned.

      I'm trying to create a file name with this format "QuickConfig_MMDDYYYY_HHMM"  (doesn't matter if 24 or 12hr format)
      #include <Date.au3> Func _SaveConfig MouseClick('primary',155, 46, 1, 0) ;clicks save as WinWait('Save bluePRINT Configuration As...','', 4) MouseClick('primary',632, 47, 1, 0) ;clicks the file path bar $Documents = @MyDocumentsDir $FilePath = $Documents & '\BP3 Configs' Send($FilePath) Send('{ENTER}') MouseClick('primary',166, 580, 1, 0) ;clicks file name bar $FileName = 'QuickConfig_' & _NowDate & '_' & _NowTime Send($FileName) Send('{ENTER}') EndFunc  
       
      I'm wondering if there is a function somewhere to do this?
    • nacerbaaziz
      By nacerbaaziz
      hello
      please i need to link a progress bar with a time can you help me?
      e.g
      i want to set a progress bar for 10 sec
      am waiting for your answers
      thank you.
    • imitto
      By imitto
      Hello all!
      I use Autoit for a while, already made some automation for a TV station's master control room with it. I made a UDF to easily work with PAL timecode and time with milliseconds, convert, add or subtract them. Feel free to use it if you want something like this
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_Description=PAL Timecode Calculator UDF #AutoIt3Wrapper_Res_LegalCopyright=horvath.imre@gmail.com #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; ; #FUNCTION# ; Name...........: _tcAdd ; Description....: Returns addition of two timecodes ; Syntax.........: _tcAdd($fTc1, fTc2 [, $fFormat = "P"]) ; ; Parameters.....: $fTc1 - First timecode in hh:mm:ss.ff format ; $fTc2 - Second timecode in hh:mm:ss.ff format ; $fFormat - Time base - "P" (default): PAL (25 fps) ; "M" : millisecond ; ; Return value...: Sum of the two timecode in the selected format Func _tcAdd($fTc1, $fTc2, $fFormat = "P", $fHourFormat = 1) Local $fMs1 = _tcToMs($fTc1) Local $fMs2 = _tcToMs($fTc2) Local $fSumMs = $fMs1 + $fMs2 Return _msToTc($fSumMs, $fFormat, $fHourFormat) EndFunc ; #FUNCTION# ; Name...........: _tcsSub ; Description....: Returns addition of two timecodes ; Syntax.........: _tcSub($fTc1, fTc2 [, $fFormat = "P"]) ; ; Parameters.....: $fTc1 - First timecode in hh:mm:ss.ff format ; $fTc2 - Second timecode in hh:mm:ss.ff format ; $fFormat - Time base - "P" (default): PAL (25 fps) ; "M" : millisecond ; ; Return value...: Subtract $fTc2 from $fTc1 in the source format Func _tcSub($fTc1, $fTc2, $fFormat = "P") Local $fMs1 = _tcToMs($fTc1) Local $fMs2 = _tcToMs($fTc2) Local $fSumMs = $fMs1 - $fMs2 If $fSumMs < 0 Then $fSumMs = _tcToMs("24:00:00.00") - ($fSumMs * -1) EndIf Return _msToTc($fSumMs, $fFormat) EndFunc ; #FUNCTION# ; Name...........: _tcToMs ; Description....: Returns timecode converted to total milliseconds ; Syntax.........: _tcToMs($fTc) ; ; Parameters.....: $fTc - Timecode in hh:mm:ss.ff or hh:mm:ss:xxx format, where xxx are milliseconds ; ; Return value...: Milliseconds as an integer value Func _tcToMs($fTc) Local $fTemp = StringSplit($fTc, ":.") Local $fChr = StringLen($fTemp[4]) Switch $fChr Case 2 Return ($fTemp[4] * 40) + ($fTemp[3] * 1000) + ($fTemp[2] * 60000) + ($fTemp[1] * 3600000) Case 3 Return ($fTemp[4]) + ($fTemp[3] * 1000) + ($fTemp[2] * 60000) + ($fTemp[1] * 3600000) EndSwitch EndFunc ; #FUNCTION# ; Name...........: _msToTc ; Description....: Converts total milliseconds to timecode ; Syntax.........: _msToTc($fIn, $fFormat = "P", $fHourFormat = 1) ; ; Parameters.....: $fIn - Time in milliseconds ; $fFormat - Output format "P": PAL TC (default) ; "M": hh:mm:ss.xxx where xxx are milliseconds ; $fHourFormat - Hour format "1": max. value is 23, then starts from 0 (default) ; "0": hours can be more then 23 ; ; Return value...: Timecode as string in the selected format Func _msToTc($fIn, $fFormat = "P", $fHourFormat = 1) Switch $fFormat Case "P" Local $fFr = StringFormat("%02i", (StringRight($fIn, 3) - Mod(StringRight($fIn, 3), 40)) / 40) Case "M" Local $fFr = StringFormat("%03i", StringRight($fIn, 3)) EndSwitch $fIn = StringTrimRight($fIn, 3) Local $fSec = StringFormat("%02i", Mod($fIn, 60)) $fIn -= $fSec Local $fMinTot = $fIn / 60 Local $fMin = StringFormat("%02i", Mod($fMinTot, 60)) $fIn -= $fMin*60 Local $fHourTot = $fIn / 60 / 60 Switch $fHourFormat Case 1 $fHour = StringFormat("%02i", Mod($fHourTot, 24)) Case 0 $fHour = StringFormat("%02i", $fHourTot) EndSwitch Return($fHour & ":" & $fMin & ":" & $fSec & "." & $fFr) EndFunc ; #FUNCTION# ; Name...........: _tcFormatChange ; Description....: Toggle TC format ; Syntax.........: _tcFormatChange($fTc) ; ; Parameters.....: $fTc - Timecode in hh:mm:ss.ff or hh:mm:ss:xxx format, where xxx are milliseconds ; ; Return value...: PAL timecode or time with milliseconds as string, depends on input Func _tcFormatChange($fTc) Local $fTemp = StringSplit($fTc, ":.") Local $fChr = StringLen($fTemp[4]) Switch $fChr Case 2 Return $fTemp[1]&":"&$fTemp[2]&":"&$fTemp[3]&"."&StringFormat("%03i", $fTemp[4]*40) Case 3 Return $fTemp[1]&":"&$fTemp[2]&":"&$fTemp[3]&"."&StringFormat("%02i", ($fTemp[4]-Mod($fTemp[4], 40))/40) EndSwitch EndFunc And the example script:
      #include<_PAL_TC_Calc.au3> $palTC1 = "00:01:12.20" $palTC2 = "23:59:50.02" $msTC1 = "00:01:12.800" $msTC2 = "23:59:50.120" MsgBox(0, "1", _tcAdd($palTC1, $palTC2)); Adds $palTC1 to $palTC2, turns hour back to 0 after 23, returns PAL TC format MsgBox(0, "2", _tcAdd($palTC1, $palTC2, "M")); Adds $palTC1 to $palTC2, turns hour back to 0 after 23, returns time with milliseconds format MsgBox(0, "3", _tcAdd($palTC1, $palTC2, "M", 0)); Adds $palTC1 to $palTC2, hours can be infinite, returns time with milliseconds format MsgBox(0, "4", _tcAdd($msTC1, $msTC2)); Adds $palTC1 to $palTC2, turns hour back to 0 after 23, returns PAL TC format MsgBox(0, "5", _tcAdd($msTC1, $msTC2, "M")); Adds $palTC1 to $palTC2, turns hour back to 0 after 23, returns time with milliseconds format MsgBox(0, "6", _tcAdd($msTC1, $msTC2, "M", 0)); Adds $palTC1 to $palTC2, hours can be infinite, returns time with milliseconds format MsgBox(0, "7", _tcSub($palTC2, $palTC1)); Subtract $palTC1 from $palTC2, returns PAL TC format MsgBox(0, "8", _tcSub($palTC2, $palTC1, "M")); Subtract $palTC1 from $palTC2, time with milliseconds format MsgBox(0, "9", _tcSub($msTC1, $msTC2)); Subtract $palTC1 from $palTC2, returns PAL TC format - when hits zero, counts back from 24:00:00.00 MsgBox(0, "10", _tcSub($msTC1, $msTC2, "M")); Subtract $palTC1 from $palTC2, time with milliseconds format - when hits zero, counts back from 24:00:00.000 MsgBox(0, "11", _tcFormatChange($palTC2)); Convert PAL TC to time with milliseconds and back MsgBox(0, "12", _tcFormatChange($msTC2)); Convert PAL TC to time with milliseconds and back  
      TC_CALC_example.au3
      _PAL_TC_Calc.au3
×