buzz44 Posted June 3, 2005 Posted June 3, 2005 (edited) Just created this all in one go and surprised it worked first time lol. Format's can be HH:MM:SS or MM:SS or both or both different . Small example below. Its a bit long (not wide), in the way that theres lots of If...EndIf Statments.Edit: Also added _TimeSub(). To work correctly $iVal1 must be the larger time otherwise you will get negative integers.Edit 2: Theres a bug /w _TimeSub(), I'll fix it in the morning lol.Edit 3: My mistake, no bug.Edit 4: Formats supported...HH:MM:SS to H:M:SMM:SS to M:SBasically You can represent 4:05 as 4:5 or 3:09:01 as 3:9:1, but remember 5 = 5 not 50.expandcollapse popup$test1 = "1:45:59" $test2 = "59:50" MsgBox(0,"Time", "$test1 = " & $test1 & @CRLF & "$test2 = " & $test2) MsgBox(0,"Result Add", _TimeAdd($test1,$test2)) MsgBox(0,"Result Sub", _TimeSub($test1,$test2)) Func _TimeAdd($iVal1,$iVal2) Local $iVal1, $iVal2, $aVal1, $aVal2 Local $iHourA, $iMinA, $iSecA Local $iHourB, $iMinB, $iSecB Local $iHourC, $iMinC, $iSecC $aVal1 = StringSplit($iVal1,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf $aVal2 = StringSplit($iVal2,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf If ($aVal1[0] > 4) Or ($aVal2[0] > 4) Then SetError(1) ; Time in incorrect format Return "" ElseIf $aVal1[0] = 2 Then $iHourA = 0 $iMinA = $aVal1[1] $iSecA = $aVal1[2] ElseIf $aVal1[0] = 3 Then $iHourA = $aVal1[1] $iMinA = $aVal1[2] $iSecA = $aVal1[3] EndIf If $aVal2[0] = 2 Then $iHourB = 0 $iMinB = $aVal2[1] $iSecB = $aVal2[2] ElseIf $aVal2[0] = 3 Then $iHourB = $aVal2[1] $iMinB = $aVal2[2] $iSecB = $aVal2[3] EndIf If ($iSecA + $iSecB) < 60 Then $iSecC = ($iSecA + $iSecB) ElseIf ($iSecA + $iSecB) > 59 Then $iSecC = ($iSecA + $iSecB) - 60 $iMinA = $iMinA + 1 EndIf If ($iMinA + $iMinB) < 60 Then $iMinC = ($iMinA + $iMinB) ElseIf ($iMinA + $iMinB) > 59 Then $iMinC = ($iMinA + $iMinB) - 60 $iHourA = $iHourA + 1 EndIf $iHourC = $iHourA + $iHourB If StringLen($iSecC) = 1 Then $iSecC = "0" & $iSecC If StringLen($iMinC) = 1 Then $iMinC = "0" & $iMinC If $iHourC = 0 Then Return ($iMinC & ":" & $iSecC) ElseIf $iHourC <> 0 Then Return ($iHourC & ":" & $iMinC & ":" & $iSecC) EndIf EndFunc Func _TimeSub($iVal1,$iVal2) Local $iVal1, $iVal2, $aVal1, $aVal2 Local $iHourA, $iMinA, $iSecA Local $iHourB, $iMinB, $iSecB Local $iHourC, $iMinC, $iSecC $aVal1 = StringSplit($iVal1,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf $aVal2 = StringSplit($iVal2,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf If ($aVal1[0] > 4) Or ($aVal2[0] > 4) Then SetError(1) ; Time in incorrect format Return "" ElseIf $aVal1[0] = 2 Then $iHourA = 0 $iMinA = $aVal1[1] $iSecA = $aVal1[2] ElseIf $aVal1[0] = 3 Then $iHourA = $aVal1[1] $iMinA = $aVal1[2] $iSecA = $aVal1[3] EndIf If $aVal2[0] = 2 Then $iHourB = 0 $iMinB = $aVal2[1] $iSecB = $aVal2[2] ElseIf $aVal2[0] = 3 Then $iHourB = $aVal2[1] $iMinB = $aVal2[2] $iSecB = $aVal2[3] EndIf If ($iSecA - $iSecB) > 0 Then $iSecC = ($iSecA - $iSecB) ElseIf ($iSecA - $iSecB) = 0 Then $iSecC = "00" ElseIf ($iSecA - $iSecB) < 1 Then $iSecC = 60 + ($iSecA - $iSecB) $iMinA = $iMinA - 1 EndIf If ($iMinA - $iMinB) > 0 Then $iMinC = ($iMinA - $iMinB) ElseIf ($iMinA - $iMinB) = 0 Then $iMinC = "00" ElseIf ($iMinA - $iMinB) < 1 Then $iMinC = 60 + ($iMinA - $iMinB) $iHourA = $iHourA - 1 EndIf $iHourC = $iHourA - $iHourB If StringLen($iSecC) = 1 Then $iSecC = "0" & $iSecC If StringLen($iMinC) = 1 Then $iMinC = "0" & $iMinC If $iHourC = 0 Then Return ($iMinC & ":" & $iSecC) ElseIf $iHourC <> 0 Then Return ($iHourC & ":" & $iMinC & ":" & $iSecC) EndIf EndFunc Edited December 29, 2011 by buzz44 qq
FuryCell Posted June 4, 2005 Posted June 4, 2005 Just created this all in one go and surprised it worked first time lol. Format's can be HH:MM:SS or MM:SS or both or both different . Small example below. Its a bit long (not wide), in the way that theres lots of If...EndIf Statments.Edit: Also added _TimeSub(). To work correctly $iVal1 must be the larger time otherwise you will get negative integers.Edit 2: Theres a bug /w _TimeSub(), I'll fix it in the morning lol.Edit 3: My mistake, no bug.CODE$test1 = "1:45:59"$test2 = "59:50"MsgBox(0,"Time", "$test1 = " & $test1 & @CRLF & "$test2 = " & $test2)MsgBox(0,"Result Add", _TimeAdd($test1,$test2))MsgBox(0,"Result Sub", _TimeSub($test1,$test2))Func _TimeAdd($iVal1,$iVal2) Local $iVal1, $iVal2, $aVal1, $aVal2 Local $iHourA, $iMinA, $iSecA Local $iHourB, $iMinB, $iSecB Local $iHourC, $iMinC, $iSecC $aVal1 = StringSplit($iVal1,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf $aVal2 = StringSplit($iVal2,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf If ($aVal1[0] > 4) Or ($aVal2[0] > 4) Then SetError(1) ; Time in incorrect format Return "" ElseIf $aVal1[0] = 2 Then $iHourA = 0 $iMinA = $aVal1[1] $iSecA = $aVal1[2] ElseIf $aVal1[0] = 3 Then $iHourA = $aVal1[1] $iMinA = $aVal1[2] $iSecA = $aVal1[3] EndIf If $aVal2[0] = 2 Then $iHourB = 0 $iMinB = $aVal2[1] $iSecB = $aVal2[2] ElseIf $aVal2[0] = 3 Then $iHourB = $aVal2[1] $iMinB = $aVal2[2] $iSecB = $aVal2[3] EndIf If ($iSecA + $iSecB) < 60 Then $iSecC = ($iSecA + $iSecB) ElseIf ($iSecA + $iSecB) > 59 Then $iSecC = ($iSecA + $iSecB) - 60 $iMinA = $iMinA + 1 EndIf If ($iMinA + $iMinB) < 60 Then $iMinC = ($iMinA + $iMinB) ElseIf ($iMinA + $iMinB) > 59 Then $iMinC = ($iMinA + $iMinB) - 60 $iHourA = $iHourA + 1 EndIf $iHourC = $iHourA + $iHourB If StringLen($iSecC) = 1 Then $iSecC = "0" & $iSecC If StringLen($iMinC) = 1 Then $iMinC = "0" & $iMinC If $iHourC = 0 Then Return ($iMinC & ":" & $iSecC) ElseIf $iHourC <> 0 Then Return ($iHourC & ":" & $iMinC & ":" & $iSecC) EndIfEndFuncFunc _TimeSub($iVal1,$iVal2) Local $iVal1, $iVal2, $aVal1, $aVal2 Local $iHourA, $iMinA, $iSecA Local $iHourB, $iMinB, $iSecB Local $iHourC, $iMinC, $iSecC $aVal1 = StringSplit($iVal1,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf $aVal2 = StringSplit($iVal2,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf If ($aVal1[0] > 4) Or ($aVal2[0] > 4) Then SetError(1) ; Time in incorrect format Return "" ElseIf $aVal1[0] = 2 Then $iHourA = 0 $iMinA = $aVal1[1] $iSecA = $aVal1[2] ElseIf $aVal1[0] = 3 Then $iHourA = $aVal1[1] $iMinA = $aVal1[2] $iSecA = $aVal1[3] EndIf If $aVal2[0] = 2 Then $iHourB = 0 $iMinB = $aVal2[1] $iSecB = $aVal2[2] ElseIf $aVal2[0] = 3 Then $iHourB = $aVal2[1] $iMinB = $aVal2[2] $iSecB = $aVal2[3] EndIf If ($iSecA - $iSecB) > 0 Then $iSecC = ($iSecA - $iSecB) ElseIf ($iSecA - $iSecB) = 0 Then $iSecC = "00" ElseIf ($iSecA - $iSecB) < 1 Then $iSecC = 60 + ($iSecA - $iSecB) $iMinA = $iMinA - 1 EndIf If ($iMinA - $iMinB) > 0 Then $iMinC = ($iMinA - $iMinB) ElseIf ($iMinA - $iMinB) = 0 Then $iMinC = "00" ElseIf ($iMinA - $iMinB) < 1 Then $iMinC = 60 + ($iMinA - $iMinB) $iHourA = $iHourA - 1 EndIf $iHourC = $iHourA - $iHourB If StringLen($iSecC) = 1 Then $iSecC = "0" & $iSecC If StringLen($iMinC) = 1 Then $iMinC = "0" & $iMinC If $iHourC = 0 Then Return ($iMinC & ":" & $iSecC) ElseIf $iHourC <> 0 Then Return ($iHourC & ":" & $iMinC & ":" & $iSecC) EndIfEndFunc<{POST_SNAPBACK}>Nice function. Tried it out and it works great! HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
buzz44 Posted June 4, 2005 Author Posted June 4, 2005 Thanks! Glad it's helping someone else besides me . qq
buzz44 Posted June 4, 2005 Author Posted June 4, 2005 (edited) Added new optional parametre ($iZero). If you wish it to return in H:MM:SS set it to 1, if you want it to return H:M:S levae it or set it to 0. The Hour will never have a zero in front of it because traditionally it doesn't but if you want me to add it just say. The above parametre will only take effect on numbers that are less then 10. Eg. - 5:05 Return 5:5 is $iZero set to 0 Return 05:05 is $iZero set to 1 expandcollapse popup$test1 = "1:45:59" $test2 = "40:50" MsgBox(0,"Time", "$test1 = " & $test1 & @CRLF & "$test2 = " & $test2) MsgBox(0,"Result Add", _TimeAdd($test1,$test2)) MsgBox(0,"Result Sub", _TimeSub($test1,$test2)) MsgBox(0,"Result Add Zero", _TimeAdd($test1,$test2, 1)) MsgBox(0,"Result Sub Zero", _TimeSub($test1,$test2, 1)) Func _TimeAdd($iVal1, $iVal2, $iZero = 0) Local $iVal1, $iVal2, $aVal1, $aVal2 Local $iHourA, $iMinA, $iSecA Local $iHourB, $iMinB, $iSecB Local $iHourC, $iMinC, $iSecC $aVal1 = StringSplit($iVal1,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf $aVal2 = StringSplit($iVal2,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf If ($aVal1[0] > 4) Or ($aVal2[0] > 4) Then SetError(1) ; Time in incorrect format Return "" ElseIf $aVal1[0] = 2 Then $iHourA = 0 $iMinA = $aVal1[1] $iSecA = $aVal1[2] ElseIf $aVal1[0] = 3 Then $iHourA = $aVal1[1] $iMinA = $aVal1[2] $iSecA = $aVal1[3] EndIf If $aVal2[0] = 2 Then $iHourB = 0 $iMinB = $aVal2[1] $iSecB = $aVal2[2] ElseIf $aVal2[0] = 3 Then $iHourB = $aVal2[1] $iMinB = $aVal2[2] $iSecB = $aVal2[3] EndIf If ($iSecA + $iSecB) < 60 Then $iSecC = ($iSecA + $iSecB) ElseIf ($iSecA + $iSecB) > 59 Then $iSecC = ($iSecA + $iSecB) - 60 $iMinA = $iMinA + 1 EndIf If ($iMinA + $iMinB) < 60 Then $iMinC = ($iMinA + $iMinB) ElseIf ($iMinA + $iMinB) > 59 Then $iMinC = ($iMinA + $iMinB) - 60 $iHourA = $iHourA + 1 EndIf $iHourC = $iHourA + $iHourB If StringLen($iSecC) = 1 And $iZero = 1 Then $iSecC = "0" & $iSecC If StringLen($iMinC) = 1 And $iZero = 1 Then $iMinC = "0" & $iMinC If $iHourC = 0 Then Return ($iMinC & ":" & $iSecC) ElseIf $iHourC <> 0 Then Return ($iHourC & ":" & $iMinC & ":" & $iSecC) EndIf EndFunc Func _TimeSub($iVal1, $iVal2, $iZero = 0) Local $iVal1, $iVal2, $aVal1, $aVal2 Local $iHourA, $iMinA, $iSecA Local $iHourB, $iMinB, $iSecB Local $iHourC, $iMinC, $iSecC $aVal1 = StringSplit($iVal1,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf $aVal2 = StringSplit($iVal2,":") If @error Then SetError(1) ; Time in incorrect format Return 0 EndIf If ($aVal1[0] > 4) Or ($aVal2[0] > 4) Then SetError(1) ; Time in incorrect format Return "" ElseIf $aVal1[0] = 2 Then $iHourA = 0 $iMinA = $aVal1[1] $iSecA = $aVal1[2] ElseIf $aVal1[0] = 3 Then $iHourA = $aVal1[1] $iMinA = $aVal1[2] $iSecA = $aVal1[3] EndIf If $aVal2[0] = 2 Then $iHourB = 0 $iMinB = $aVal2[1] $iSecB = $aVal2[2] ElseIf $aVal2[0] = 3 Then $iHourB = $aVal2[1] $iMinB = $aVal2[2] $iSecB = $aVal2[3] EndIf If ($iSecA - $iSecB) > 0 Then $iSecC = ($iSecA - $iSecB) ElseIf ($iSecA - $iSecB) = 0 Then $iSecC = "00" ElseIf ($iSecA - $iSecB) < 1 Then $iSecC = 60 + ($iSecA - $iSecB) $iMinA = $iMinA - 1 EndIf If ($iMinA - $iMinB) > 0 Then $iMinC = ($iMinA - $iMinB) ElseIf ($iMinA - $iMinB) = 0 Then $iMinC = "00" ElseIf ($iMinA - $iMinB) < 1 Then $iMinC = 60 + ($iMinA - $iMinB) $iHourA = $iHourA - 1 EndIf $iHourC = $iHourA - $iHourB If StringLen($iSecC) = 1 And $iZero = 1 Then $iSecC = "0" & $iSecC If StringLen($iMinC) = 1 And $iZero = 1 Then $iMinC = "0" & $iMinC If $iHourC = 0 Then Return ($iMinC & ":" & $iSecC) ElseIf $iHourC <> 0 Then Return ($iHourC & ":" & $iMinC & ":" & $iSecC) EndIf EndFunc Edited December 29, 2011 by buzz44 qq
Insolence Posted June 4, 2005 Posted June 4, 2005 I suggest allowing the user to specify which way they want it, like I did with this little sucker: http://www.autoitscript.com/forum/index.ph...topic=10648&hl= "I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
buzz44 Posted June 4, 2005 Author Posted June 4, 2005 (edited) What do you mean? The parametre is optional look at the example. 1 = with zero, 0 without Zero.Edit: Or do you mean have a seperate parametre for Hour/Min/Sec. Edited December 29, 2011 by buzz44 qq
Insolence Posted June 4, 2005 Posted June 4, 2005 Like so: expandcollapse popup; The last parameter uses RegEx to replace '%d' with the days, %h with the hours, and so on MsgBox("","",TimeToStr ( 4320000, "The time from seconds is: %d day(s) %h:%m:%s" )) Func TimeToStr( $iAmount, $sFormat) Local $Days = 0, $Hours = 0, $Minutes = 0, $Seconds = 0, $sReturn ; Parsing out days If StringRegExp ( $sFormat, "%d" ) Then If $iAmount >= 86400 Then $Days = Int ( $iAmount/86400 ) $iAmount = $iAmount - $Days * 86400 EndIf EndIf ; Parsing out hours If StringRegExp ( $sFormat, "%h" ) Then If $iAmount >= 3600 Then $Hours = Int ( $iAmount/3600 ) $iAmount = $iAmount - $Hours * 3600 EndIf If $Hours < 10 Then $Hours = "0" & $Hours EndIf ; Parsing out minutes If StringRegExp ( $sFormat, "%m" ) Then If $iAmount >= 60 Then $Minutes = Int ( $iAmount/60 ) $iAmount = $iAmount - $Minutes * 60 EndIf If $Minutes < 10 Then $Minutes = "0" & $Minutes EndIf ; Parsing out seconds If StringRegExp ( $sFormat, "%s" ) Then ; For the sake of beauty I alias $Seconds to the remaining seconds $Seconds = Int ( $iAmount ) EndIf If $Seconds < 10 Then $Seconds = "0" & $Seconds ; Multi-level string replace, because I'm lazy return StringRegExpReplace( StringRegExpReplace( StringRegExpReplace( StringRegExpReplace( $sFormat, "%s", $Seconds ), "%m", $Minutes ), "%h", $Hours ), "%d", $Days ) EndFunc "I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now