Jump to content

String Add Time / Subtract Time UDF(s)


SmOke_N
 Share

Recommended Posts

  • Moderators

The _AddTime() was a request for help in the Support Forum... And the _SubtractTime() was because I knew someone would ask for it at some point.... I'm not especially proud of the Sub time, I feel like it's over complicated for some reason, but I'm entirely to tired to care.

** Note, time must be a string with 2 ':' in it (a section for hour:min:sec) and requires beta.

Local $Time1 = '00:42:56', $Time2 = '02:17:05'

MsgBox(0, 'Subtract / Add', 'Subtracted: ' & _SubTractTime($Time1, $Time2) & @CR & 'Added: ' & _AddTime($Time1, $Time2))


Func _SubTractTime($sTime1, $sTime2)
    Local $aSplit1 = StringSplit($sTime1, ':')
    Local $aSplit2 = StringSplit($sTime2, ':')
    If $aSplit1[0] <> 3 Or $aSplit2[0] <> 3 Then Return SetError(0, 0, 0)
    Local $iTotal1 = ((Int($aSplit1[1]) * 3600) + (Int($aSplit1[2]) * 60) + (Int($aSplit1[3])))
    Local $iTotal2 = ((Int($aSplit2[1]) * 3600) + (Int($aSplit2[2]) * 60) + (Int($aSplit2[3])))
    If $iTotal1 >= $iTotal2 Then
        $iSubTotal = $iTotal1 - $iTotal2
    Else
        $iSubTotal = $iTotal2 - $iTotal1
    EndIf
    Local $iHour = Int(($iSubTotal / 3600))
    $iSubTotal = $iSubTotal - (($iHour * 3600))
    Local $iMin = Int($iSubTotal / 60)
    $iSubTotal = $iSubTotal - ($iMin * 60)
    Local $iSec = Int($iSubTotal)
    Return StringFormat('%02d', $iHour) & ':' & StringFormat('%02d', $iMin) & ':' & StringFormat('%02d', $iSec)
EndFunc

Func _AddTime($sTime1, $sTime2)
    Local $aSplit1 = StringSplit($sTime1, ':')
    Local $aSplit2 = StringSplit($sTime2, ':')
    If $aSplit1[0] <> 3 Or $aSplit2[0] <> 3 Then Return SetError(0, 0, 0)
    Local $aAddTime[4] = ['', 0, 0, 0]
    For $iCount = UBound($aSplit1) - 1 To 2 Step - 1
        $aAddTime[$iCount] += Int($aSplit1[$iCount]) + Int($aSplit2[$iCount])
        Do
            If $aAddTime[$iCount] > 59 Then
                $aAddTime[$iCount - 1] += 1
                $aAddTime[$iCount] = StringFormat('%02d', String($aAddTime[$iCount] - 60))
            EndIf
        Until Int($aAddTime[$iCount]) < 60
    Next
    $aAddTime[1] += Int($aSplit1[1]) + Int($aSplit2[1]);Weird couldn't use StringFormat correctly here
    Return StringFormat('%02d', $aAddTime[1]) & ':' & $aAddTime[2] & ':' & $aAddTime[3]
EndFunc
If someone feels like making a UDF that does both, then feel free, or if there is one that already does this, sorry... I didn't look too hard to find one.

Edit:

This one's 5 x's as fast then the above _AddTime()

Func _AddTime($sTime1, $sTime2)
    Local $aSplit1 = StringSplit($sTime1, ':')
    Local $aSplit2 = StringSplit($sTime2, ':')
    If $aSplit1[0] <> 3 Or $aSplit2[0] <> 3 Then Return SetError(0, 0, 0)
    Local $iTotal1 = ((Int($aSplit1[1]) * 3600) + (Int($aSplit1[2]) * 60) + (Int($aSplit1[3])))
    Local $iTotal2 = ((Int($aSplit2[1]) * 3600) + (Int($aSplit2[2]) * 60) + (Int($aSplit2[3])))
    Local $iSubTotal = $iTotal1 + $iTotal2
    Local $iHour = Int(($iSubTotal / 3600))
    $iSubTotal = $iSubTotal - (($iHour * 3600))
    Local $iMin = Int($iSubTotal / 60)
    $iSubTotal = $iSubTotal - ($iMin * 60)
    Local $iSec = Int($iSubTotal)
    Return StringFormat('%02d', $iHour) & ':' & StringFormat('%02d', $iMin) & ':' & StringFormat('%02d', $iSec)
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Good work. :D

I don't do much with time, but when I do I'll use em. :wacko:

Thanks... I decided to go ahead and just make it 1 function... I have no idea what to call it, so for now it's just _StrAddSub().

Local $Time1 = '00:622:09', $Time2 = '02:1788:05'

MsgBox(0, 'Subtract / Add', 'Subtracted: ' & _StrAddSub($Time1, $Time2, 0) & @CR & _
     'Added: ' & _StrAddSub($Time1, $Time2))

Func _StrAddSub($sTime1, $sTime2, $iAdd = 1, $sDelim = ':'); $iAdd = 1 Add, $iAdd = 0 Subtract
    Local $aSplit1 = StringSplit($sTime1, $sDelim), $iSubTotal = 0
    Local $aSplit2 = StringSplit($sTime2, $sDelim)
    If $aSplit1[0] <> 3 Or $aSplit2[0] <> 3 Then Return SetError(0, 0, 0)
    Local $iTotal1 = ((Int($aSplit1[1]) * 3600) + (Int($aSplit1[2]) * 60) + (Int($aSplit1[3])))
    Local $iTotal2 = ((Int($aSplit2[1]) * 3600) + (Int($aSplit2[2]) * 60) + (Int($aSplit2[3])))
    If $iAdd Then
        $iSubTotal = $iTotal1 + $iTotal2
    ElseIf $iTotal1 >= $iTotal2 Then
        $iSubTotal = $iTotal1 - $iTotal2
    Else
        $iSubTotal = $iTotal2 - $iTotal1
    EndIf
    Local $iHour = Int(($iSubTotal / 3600))
    Local $iMin = Int(($iSubTotal - ($iHour * 3600)) / 60)
    Local $iSec = Int(($iSubTotal - $iHour * 3600) - ($iMin * 60))
    Return StringFormat('%02d', $iHour) & $sDelim & StringFormat('%02d', $iMin) & $sDelim & StringFormat('%02d', $iSec)
EndFunc
There's a crazy number for minutes there to for mr evil guy :D

Edit:

Added $sDelim with a default of ':' per suggestion.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

HI,

seems to work. There is already a calcDate func, so why not rename it to calcTime(). I think, that is what the funcs does. :D

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Moderators

Not familiar with that calcDate() ... I have _NowCalcDate() maybe that's what you mean.

It was suggested maybe _TimeAddDiff() or _TimeCalc() maybe even... anyway, I'm not bothered by it if it said _a() as long as the function works :D.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Works well, maybe you should add $sDelim as a paramater which has the default value of ":".

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

  • Moderators

Works well, maybe you should add $sDelim as a paramater which has the default value of ":".

I thought of that... meant to put it in earlier as well...

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...