Jump to content

FileGetTimeV2


numdig
 Share

Recommended Posts

In case, you have a LAN/VPN that spawns across two countries such as UK and France where the time is not the same, you might need something like that to get everybody synchronised:

(FileGetTime returns the local time...)

;-------------------------------------------------------------------------------
; Name .........: FileGetTimeV2
; Description ..: Similar to FileGetTime but without the 'daylight saving'
;                 nor 'bias' part. See 'FileGetTimeLocal' for the equivalent
;                 function dealing with the  local time.
; Parameters ...: $filename - Filename to check.
;                 $option [optional] - Flag to indicate which timestamp
;                                        0 = Modified (default)
;                                        1 = Created
;                                        2 = Accessed
;                 $format [optional] - To specify type of return
;                                        0 = return an array (default)
;                                        1 = return a string YYYYMMDDHHMMSS
;                                        2 = return a string YYYYMM-DD-HH-MMSS
;                                        3 = return a string YYYY/MM/DD HH:MM:SS
; Return .......: Success: Returns an array or string that contains the file
;                          time information. See Remarks.
;                 Failure: Returns empty string and sets @error to 1.
; Remarks ......: The array is a single dimension array containing six elements:
;                 $array[0] = year (four digits)
;                 $array[1] = month (range 01 - 12)
;                 $array[2] = day (range 01 - 31)
;                 $array[3] = hour (range 00 - 23)
;                 $array[4] = min (range 00 - 59)
;                 $array[5] = sec (range 00 - 59)
;                 Notice that return values are zero-padded.
; Testing ......:
;                 ConsoleWrite(FileGetTimeV2(@ScriptFullPath, 0, 0)&@CRLF)
;                 ConsoleWrite(FileGetTimeV2(@ScriptFullPath, 0, 1)&@CRLF)
;                 ConsoleWrite(FileGetTimeV2(@ScriptFullPath, 0, 2)&@CRLF)
;                 ConsoleWrite(FileGetTimeV2(@ScriptFullPath, 0, 3)&@CRLF)
;
; History ......: JUN 9, 2010 - Created for 'HostMonitor.au3'
;                 JUL 14, 2010 - Added format '2' (eg. "201007-12-15-0007")
;                 JAN 28, 2011 - Added format '3' (eg. "YYYY/MM/DD[ HH:MM:SS]")
;-------------------------------------------------------------------------------
Func FileGetTimeV2($filename, $option = 0, $format = 0)
    Local $return = ""
    Local $hFile = _WinAPI_CreateFile($filename, 2, 2, 2)
    If $hFile = $INVALID_FILEHANDLE Then
        SetError(1)
        Return $return
    Else
        Local $a_tFiletime = _Date_Time_GetFileTime($hFile)
        Local $index = 2 ; by default we read modification date
        If $option = 1 Then $index = 0 ; creation date
        If $option = 2 Then $index = 1 ; last access date
        Local $aTime = _Date_Time_FileTimeToArray($a_tFiletime[$index])
        If $format Then
            Local $pattern = "%04d%02d%02d%02d%02d%02d"
            If $format = 2 Then
                $pattern = "%04d%02d-%02d-%02d-%02d%02d"
            ElseIf $format = 3 Then
                $pattern = "%04d/%02d/%02d %02d:%02d:%02d"
            EndIf
            $return = StringFormat($pattern _
                    , $aTime[2], $aTime[0], $aTime[1], $aTime[3], $aTime[4], $aTime[5])
        Else
            Dim $return[6] = [ _
                    StringFormat("%04d", $aTime[2]) _
                    , StringFormat("%02d", $aTime[0]) _
                    , StringFormat("%02d", $aTime[1]) _
                    , StringFormat("%02d", $aTime[3]) _
                    , StringFormat("%02d", $aTime[4]) _
                    , StringFormat("%02d", $aTime[5])]
        EndIf
        _WinAPI_CloseHandle($hFile)
        Return $return
    EndIf
EndFunc   ;==>FileGetTimeV2
Link to comment
Share on other sites

In a similar way, we can replace macros such as @HOUR by something like this:

;-------------------------------------------------------------------------------
; Name .........: MyHOUR
; Description ..: @HOUR but without the 'daylight saving' info.
; Parameters ...: none
; Return .......: String. See Remarks.
; Remarks ......: hour (range 00 - 23)
;                 Notice that return values are zero-padded.
; History ......: JUN 24, 2010 - Created for 'BuildManager.au3'
;-------------------------------------------------------------------------------
Func MyHOUR()
    Local $tTime = _Date_Time_GetSystemTimeAsFileTime()
    Local $aTime = _Date_Time_FileTimeToArray($tTime)
    Return StringFormat("%02d", $aTime[3])
EndFunc   ;==>MyHOUR

Same for the other macros. Let me know if you need them:

MySEC(), MyMIN(), MyHOUR(), MyMDAY(), MyMON() and MyYEAR().

Link to comment
Share on other sites

We can continue using this trick and rewrit other UDFs:

;-------------------------------------------------------------------------------
; Name .........: _NowCalcV2
; Description ..: Similar to _NowCalc but without the 'daylight saving' info.
; Parameters ...: none
; Return .......: Formated String "YYYY/MM/DD HH:MM:SS"
; History ......: JUN 24, 2010 - Created for 'BuildManager.au3'
;-------------------------------------------------------------------------------
Func _NowCalcV2()
    Local $tTime = _Date_Time_GetSystemTimeAsFileTime()
    Local $aTime = _Date_Time_FileTimeToArray($tTime)
    Return StringFormat("%04d/%02d/%02d %02d:%02d:%02d" _
            , $aTime[2], $aTime[0], $aTime[1], $aTime[3], $aTime[4], $aTime[5])
EndFunc   ;==>_NowCalcV2

And same for _NowCalcDateV2(), etc.

Link to comment
Share on other sites

And writing an ISO version of _Now() using ISO 8601 format:

Just call

$formattedISOTime = _NowISO_Ext()

;-------------------------------------------------------------------------------
; Name .........: MyBIAS
; Description ..:
; Parameters ...:
; Return .......: String. See Remarks.
; Remarks ......:
; History ......: NOV 3, 2010 - Created for 'EasyLog.au3'
;-------------------------------------------------------------------------------
Func MyBIAS()
    Do
        Local $tSyst = _Date_Time_GetLocalTime()
        Local $tTime = _Date_Time_SystemTimeToFileTime(DllStructGetPtr($tSyst))
        Local $aTime = _Date_Time_FileTimeToArray($tTime)
    until ($aTime[6] < 950 Or $aTime[5] <> 59 Or $aTime[4] <> 59)
    return (@HOUR - MyHOUR()) * 60
EndFunc   ;==>MyBIAS

;-------------------------------------------------------------------------------
; Name .........: _NowISO_Ext
; Description ..:
; Parameters ...: $flag = 0 - local time
;                 $flag = 1 - UTC time
; Return .......: String in the ISO 8601 format.
;                 (eg. "2011-07-25 02:39:51.800+01")
; History ......: NOV 3, 2010 - Created for 'EasyLog.au3'
;-------------------------------------------------------------------------------
Func _NowISO_Ext($flag = 0, $format = "%04d-%02d-%02d %02d:%02d:%02d.%03d%s")

    ; Use UTC by default (flag <> 0)
    Local $bias = 0
    Local $tSyst = _Date_Time_GetSystemTime()
    If $flag = 0 Then
        $bias = MyBIAS()
        $tSyst = _Date_Time_GetLocalTime()
    EndIf

    Local $tTime = _Date_Time_SystemTimeToFileTime(DllStructGetPtr($tSyst))
    Local $aTime = _Date_Time_FileTimeToArray($tTime)

    $tTime = _Date_Time_GetSystemTimeAsFileTime()

    Local $sign = "+"
    Local $diff = $bias / 60
    If $diff < 0 Then
        $sign = "-"
        $diff *= -1
    EndIf

    Local $suffix = StringFormat("%s%02d" _
            , $sign _
            , $diff _
            )

    Return StringFormat($format _
            , $aTime[2] _
            , $aTime[0] _
            , $aTime[1] _
            , $aTime[3] _
            , $aTime[4] _
            , $aTime[5] _
            , $aTime[6] _
            , $suffix _
            )

EndFunc   ;==>_NowISO_Ext
Link to comment
Share on other sites

  • 1 month later...

that's just fabulous. I'm working with sqlite that accept only ISO8601 datetime. Will give a try.

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
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...