Jump to content

Subtracting time


Recommended Posts

I've got a text file that's got timestamps in it. I'm having a problem writing a small script to subtract the difference. Here's a snipet of the text file:

[Fri Feb 03 20:16:20] BecDeCorbin (QC7MWWWY) Joined as Player 1

[Fri Feb 03 21:05:58] Iolana (QCRDPJ7X) Joined as Player 2

[Fri Feb 03 21:14:40] Bert_Mechman (QC7Y9NJR) Joined as Player 3

[Fri Feb 03 21:25:39] BecDeCorbin Left as a Player (2 players left)

I can write a script to extract just the time, 20:16 from the text file, but I want to subtract 20:16 from 21:25 to see the difference. Any thoughts?
Link to comment
Share on other sites

Try again. :whistle:

Using this script from the help file (modified slighty to test):

#include <Date.au3>
$start = "20:16"
$end = "21:25"

; Calculated the number of seconds since EPOCH (1970/01/01 00:00:00) 
$iDateCalc = _DateDiff( 'n',$start,$end)
MsgBox( 4096, "", "Number of minutes: " & $iDateCalc )

The result is "0"

Link to comment
Share on other sites

Ok, no worries. I finally got a script to work:

$start = "20:35"
$end = "21:45"

If StringTrimRight($end, 3) - StringTrimRight($start, 3) > 1 Then
; ^^ If the hour difference is 1 or less continue
    If StringTrimLeft($end, 3) - StringTrimLeft($start, 3) < 0 Then
; ^^ If the min difference is negative
        $Min = (StringTrimLeft($end, 3) - StringTrimLeft($start, 3))+((StringTrimRight($end, 3) - StringTrimRight($start, 3))*60)
        msgbox(0,"test", "Minutes = "&$Min)
    ElseIf StringTrimLeft($end, 3) - StringTrimLeft($start, 3) >= 0 Then
; ^^ If the min difference is not negative then
        $Min = ((StringTrimLeft($end, 3) - StringTrimLeft($start, 3)) + ((StringTrimRight($end, 3) - StringTrimRight($start, 3))*60))
        msgbox(0,"test", "Minutes = "&$Min)
    EndIf
ElseIf StringTrimRight($end, 3) - StringTrimRight($start, 3) = 1 Then
; ^^ If the hour difference is 1 continue
    If StringTrimLeft($end, 3) - StringTrimLeft($start, 3) < 0 Then
; ^^ If the min difference is negative
        $Min = (StringTrimLeft($end, 3) - StringTrimLeft($start, 3) + 60)
        msgbox(0,"test", "Minutes = "&$Min)
    ElseIf StringTrimLeft($end, 3) - StringTrimLeft($start, 3) >= 0 Then
; ^^ If the min difference is not negative then
        $Min = ((StringTrimLeft($end, 3) - StringTrimLeft($start, 3)) + ((StringTrimRight($end, 3) - StringTrimRight($start, 3))*60))
        msgbox(0,"test", "Minutes = "&$Min)
    EndIf
ElseIf StringTrimRight($end, 3) - StringTrimRight($start, 3) < 1 Then
    If StringTrimLeft($end, 3) - StringTrimLeft($start, 3) < 0 Then
; ^^ If the min difference is negative
        $Min = (StringTrimLeft($end, 3) - StringTrimLeft($start, 3) + 60)
        msgbox(0,"test", "Minutes = "&$Min)
    ElseIf StringTrimLeft($end, 3) - StringTrimLeft($start, 3) >= 0 Then
; ^^ If the min difference is not negative then 
        $Min = StringTrimLeft($end, 3) - StringTrimLeft($start, 3)
        msgbox(0,"test", "Minutes = "&$Min)
    EndIf
EndIf
Link to comment
Share on other sites

If you can already get the times out, then just do a little math.

$start = _ToSeconds('20:16')
$end = _ToSeconds('21:25')

MsgBox(0,'',$end - $start)

Func _ToSeconds($time)
  $split = StringSplit($time, ':')
  $seconds = $split[1] * 60 + $split[2]
  Return $seconds
EndFunc

Fairly simple, at least for the usage of this script snipped. Were you perhaps looking to do something like this on a grander scale?

Link to comment
Share on other sites

Try again. :whistle:

Using this script from the help file (modified slighty to test):

#include <Date.au3>
$start = "20:16"
$end = "21:25"

; Calculated the number of seconds since EPOCH (1970/01/01 00:00:00) 
$iDateCalc = _DateDiff( 'n',$start,$end)
MsgBox( 4096, "", "Number of minutes: " & $iDateCalc )

The result is "0"

Try what again? It isn't my fault you didn't know how to use it properly.
Link to comment
Share on other sites

Try what again? It isn't my fault you didn't know how to use it properly.

Wanna tell me what you'd do different with that script then? What did I do wrong with the script I laid out then?

Edited by ttleser
Link to comment
Share on other sites

@googemyster

1. Your script uses current time and date, I need it from a file that has no date.

2. Adding the seconds back to the numbers still produces a output of zero.

#include <Date.au3>
$start = "20:16:20"
$end = "21:25:39"

; Calculated the number of seconds since EPOCH (1970/01/01 00:00:00) 
$iDateCalc = _DateDiff( 'n',$start,$end)
MsgBox( 4096, "", "Number of minutes: " & $iDateCalc )
Link to comment
Share on other sites

#include <date.au3>

; this would be some array created by reading from your file
Global $arrayfromsomefile[5] = [4, _
        '[Fri Feb 03 20:16:20] BecDeCorbin (QC7MWWWY) Joined as Player 1', _
        '[Fri Feb 03 21:05:58] Iolana (QCRDPJ7X) Joined as Player 2', _
        '[Fri Feb 03 21:14:40] Bert_Mechman (QC7Y9NJR) Joined as Player 3', _
        '[Fri Feb 03 21:25:39] BecDeCorbin Left as a Player (2 players left)']

MsgBox(0, '', 'The difference is ' & _nameofmydatedifffunction() & ' minutes.')

Func _nameofmydatedifffunction()
    Local $somevarname, $someothervarname
    For $i = 1 To UBound($arrayfromsomefile) - 1
        If StringInStr($arrayfromsomefile[$i], 'BecDeCorbin') And StringInStr($arrayfromsomefile[$i], 'Joined') Then $somevarname = _nameofmyfuncttoparsetheline($arrayfromsomefile[$i])
        If StringInStr($arrayfromsomefile[$i], 'BecDeCorbin') And StringInStr($arrayfromsomefile[$i], 'Left') Then $someothervarname = _nameofmyfuncttoparsetheline($arrayfromsomefile[$i])
    Next
    Local $start = @YEAR & '/' & @MON & '/' & @WDAY & ' ' & $somevarname   ; if you're using something other than the current y/m/d you can parse in your own
    Local $end = @YEAR & '/' & @MON & '/' & @WDAY & ' ' & $someothervarname
    Return _DateDiff('n', $start, $end)
EndFunc

; you may have to change this - no clue how constant your file's format is
Func _nameofmyfuncttoparsetheline($watchoutincomingvar)
    Return StringLeft(StringTrimLeft($watchoutincomingvar, 12), 8)
EndFunc

edit: formatting, couple explanations

Edited by xcal
Link to comment
Share on other sites

CODE
;===============================================================================
;
; Function Name:    _AdjustTime()
; Description:      Add to, or subtract from, a basetime
; Parameter(s):     $sTime - the basetime
;                   $sSpan - the timespan
; Requirement(s):   _SecondsToTimespan()
; Return Value(s):  On Success - returns the adjusted timestring
;                                @extended set to offset of days
;                   On Failure - returns an empty string
;                                @error set to 1
; Author(s):        D
; Note(s):          None
;
;===============================================================================
Func _AdjustTime($sTime, $sSpan)
    ; Split basetime to array while checking for parameter format errors
    Local $asTime = StringRegExp($sTime, "^([01]\d|2[0-3])(?::([0-5]\d)|)(?::([0-5]\d)|)$", 1)
    If @error Then Return SetError(1, 1, "")

    ; Split timespan to array while checking for parameter format errors
    Local $asSpan = StringRegExp($sSpan, "^([+-]?)(\d+)(?::([0-5]\d)|)(?::([0-5]\d)|)$", 1)
    If @error Then Return SetError(1, 2, "")

    ; Special case: for zero timespan return unaltered time
    If StringRegExp($sSpan, "^00(?::00|)(?::00|)$") Then Return $sTime

    ; Redimension to be sure of array sizes
    ReDim $asTime[3], $asSpan[4]

    ; Convert basetime and timespan to seconds
    Local $iTime = ($asTime[0] * 3600) + ($asTime[1] * 60) + $asTime[2]
    Local $iSpan = ($asSpan[1] * 3600) + ($asSpan[2] * 60) + $asSpan[3]

    Switch $asSpan[0]
        Case ""
            ContinueCase
        Case "+"
            Local $iMod = Mod($iTime + $iSpan, 86400), $sReturn
            $sReturn = _SecondsToTimespan($iMod - (86400 * ($iMod > 86400)))
        Case "-"
            Local $iMod = Mod($iTime - $iSpan, 86400), $sReturn
            $sReturn = _SecondsToTimespan($iMod + (86400 * ($iMod < 0)))
    EndSwitch

    ; Set @extended macro to the difference in days, and return new timestring
    Return SetExtended(Floor(($iTime + ($asSpan[0] & $iSpan)) / 86400), $sReturn)
EndFunc   ;==>_AdjustTime


;===============================================================================
;
; Function Name:    _SecondsToTimespan()
; Description:      Convert a number of seconds to a timespan
; Parameter(s):     $iSecs - the number of seconds
; Requirement(s):   None
; Return Value(s):  On Success - returns a string in the form "000:00:00"
;                   On Failure - returns an empty string
;                                @error set to 1
; Author(s):        D
; Note(s):          None
;
;===============================================================================
Func _SecondsToTimespan($iSecs)
    ; Minus seconds not allowed
    If $iSecs < 0 Then Return SetError(1, 0, "")

    ; Special case: for zero seconds return zero timespan
    If Not $iSecs Then Return "00:00:00"

    ; Seconds
    Local $iS = Mod($iSecs, 60)

    ; Minutes
    Local $iMins = Floor($iSecs / 60)
    Local $iM = Mod($iMins, 60)

    ; Hours
    Local $iHours = Floor($iMins / 60)
    Local $iH = Mod($iHours, 24)

    ; Days
    Local $iDays = Floor($iHours / 24)
    Local $iD = Mod($iDays, 365)

    ; Set @extended to offset of days and return the timespan as a string
    Return SetExtended($iD, StringFormat("%02s:%02s:%02s", $iH, $iM, $iS))
EndFunc   ;==>_SecondsToTimespan

Try these. They're a bit rough and ready but functional.

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...