Sign in to follow this  
Followers 0
LxP

SetLocalTime()

2 posts in this topic

SetLocalTime

Sets the system time using the current time zone information.

SetLocalTime($Hour, $Min[, $Sec[, $Day[, $Month[, $Year]]]])

Requirements: AutoIt v3.1.1.82+

Return value: True for success or False for failure.

@Error codes:

  • 0: No error.
  • 1: Parameter/s invalid or out of range.
  • 2: A DLLCall() failed.
Remarks

If the optional date parameters are omitted then the current local date is used. If $Sec is omitted then the seconds are reset to 0.

Func SetLocalTime($Hour, $Min, $Sec = 0, $Day = 0, $Month = 0, $Year = 0)

    Local $Error = 0
    Local $Return = False

; Check parameters
    If $Hour < 0 Or $Hour > 23 Then $Error = 1
    If $Min < 0 Or $Min > 59 Then $Error = 1
    If $Sec < 0 Or $Sec > 59 Then $Error = 1
    If @NumParams > 3 And ($Day < 1 Or $Day > 31) Then $Error = 1
    If @NumParams > 4 And ($Month < 1 Or $Month > 12) Then $Error = 1
    If @NumParams > 5 And ($Year < 1601 Or $Year > 30827) Then $Error = 1

    If Not $Error Then
    ; Create the necessary structure
        Local $Struct = DLLStructCreate('Short;Short;Short;Short;Short;Short;Short;Short')
        Local $StructPtr = DLLStructGetPtr($Struct)
    ; Get the current date if it is not being fully set
        If @NumParams < 6 Then
            DLLCall('Kernel32', 'None', 'GetLocalTime', 'Ptr', $StructPtr)
            If @Error Then $Error = 2
        EndIf
        If Not $Error Then
        ; Write the data to the structure
            DLLStructSetData($Struct, 5, $Hour)
            DLLStructSetData($Struct, 6, $Min)
            DLLStructSetData($Struct, 7, $Sec)
            DLLStructSetData($Struct, 8, 0)
            If @NumParams > 3 Then DLLStructSetData($Struct, 4, $Day)
            If @NumParams > 4 Then DLLStructSetData($Struct, 2, $Month)
            If @NumParams > 5 Then DLLStructSetData($Struct, 1, $Year)
        ; SetLocalTime must be called twice for DST purposes
            DLLCall('Kernel32', 'None', 'SetLocalTime', 'Ptr', $StructPtr)
            Local $Result = DLLCall('Kernel32', 'Int', 'SetLocalTime', 'Ptr', $StructPtr)
            If @Error Then
                $Error = 2
            Else
                $Return = ($Result[0] <> 0)
            EndIf
        EndIf
        DLLStructDelete($Struct)    
    EndIf

    SetError($Error)
    Return $Return

EndFunc

Share this post


Link to post
Share on other sites



@LxP, nice function - like the coding!

Cheers.. B)

Share this post


Link to post
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
Sign in to follow this  
Followers 0