Jump to content

reading time stamps


 Share

Recommended Posts

Right now i am using StringRegExp to read the hour time and second, i was wondering what is the best way to get the time difference of them, is there a function to do this or do i have to use good ol math to determine the difference?

[2:58:24 PM]
[3:05:21 PM]
Edited by d0n
Link to comment
Share on other sites

I don't think it would be hard to do with a little math, but if you are a bit lazy (who ain't? >_< ) then there is _DateDiff()

(can be found in helpfile under "User Defined Functions>Date Management")

Link to comment
Share on other sites

just another quick question regarding time functions, is there a quicker way to obtain the format needed for _DateDiff() than using StringRegExp and doing lots of IF statements for the months and the AM PM

[3:03:24 PM Jul 29 2009]
Link to comment
Share on other sites

just another quick question regarding time functions, is there a quicker way to obtain the format needed for _DateDiff() than using StringRegExp and doing lots of IF statements for the months and the AM PM

[3:03:24 PM Jul 29 2009]

Try this.

;
#Include <Date.au3>

Local $sDateTim = "[3:03:24 PM Jul 29 2009]"

Local $sRetVar = StringRegExpReplace($sDateTim, "\[(\d{1,2}):(\d{2}):(\d{2}) (\w{2}) (\w+) (\d{1,2}) (\d{4})\]", _
        '_CallFunction("\1","\2","\3","\4","\5","\6","\7") & ')

$sRetVar = Execute(StringTrimRight($sRetVar, 3))

MsgBox(64, "Results", $sRetVar & @CRLF & _DateDiff("h", $sRetVar, @YEAR & "/" & @MON & "/" &  @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC) & " hrs difference")


Func _CallFunction($h, $min, $s, $APm, $mth, $d, $y)
    ConsoleWrite($h & $min & $s & $APm & $mth & $d & $y & @CRLF)
    Local $aMths[13] = [12, "January", "February", "March", "April", "May",  "June", "July", "August", "September", "October", "November", "December"]
    If $APm = "PM" Then
        $h += 12
    EndIf
    For $x = 1 To 12
        If StringInStr($aMths[$x], $mth) <> 0 Then $mth = $x
    Next
    Return StringFormat("%4d/%02d/%02d %02d:%02d:%02d", $y, $mth, $d, $h, $min, $s)
EndFunc   ;==>_CallFunction
;

Edit: Thinking about d0n's post #10, I made the input time/date a lot more flexible in this added script.

;
#include <Date.au3>

Local $sDateTim = "The time/date is 12:3:24 7/29 09."
;Local $sDateTim = "[12:03:24 PM Jul 29 09]"

Local $sRetVar = StringRegExpReplace($sDateTim, ".*?(\d{1,2}):(\d{1,2}):(\d{1,2})\s+(\w{0,2})\s*(\w+|\d{1,2})(\s+|/)(\d{1,2})(\s+|/)(\d{1,4}).*$", _
        '_CallFunction("\1","\2","\3","\4","\5","\7","\9") & ')

$sRetVar = Execute(StringTrimRight($sRetVar, 3))

MsgBox(64, "Results", $sRetVar & @CRLF & _DateDiff("h", $sRetVar, @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC) & " hrs difference")


Func _CallFunction($h, $min, $s, $APm, $mth, $d, $y)
    ConsoleWrite($h & $min & $s & $APm & $mth & $d & $y & @CRLF)
    Local $aMths[13] = [12, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    If StringUpper($APm) = "PM" And $h <> 12 Then
        $h += 12
    EndIf
    If $mth = 0 Then ; if a string then $mth = numeric 0, therefore a month number not used.
        For $x = 1 To 12
            If StringInStr($aMths[$x], $mth) <> 0 Then $mth = $x
        Next
    EndIf
    If StringLen($y) <= 2 Then ; if year only 1 or 2 digits long used then add for correct century.
        If Number($y) > StringRight(@YEAR, 2) Then
            $y = 1900 + Number($y)
        Else
            $y = 2000 + Number($y)
        EndIf
    EndIf
    Return StringFormat("%4d/%02d/%02d %02d:%02d:%02d", $y, $mth, $d, $h, $min, $s)
EndFunc   ;==>_CallFunction
;
Edited by Malkey
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...