Warning: Many of these scripts require additional files which will be included in the download.
Executing these scripts without those files could result in anything from minor irritations to code failure.
Be sure to download the zip file if you plan on using the code.

   Time.au3

Last modified:   Wednesday, September 30, 2020

;*******************************************************************************
;
;   Function List
;         _Time()
;         _Time_12Hour()
;         _Time_CurrentDisp()
;         _Time_DST_Bias()
;         _Time_FormatLocal()
;         _Time_InRange()
;         _Time_UTC_Offset()
;
;*******************************************************************************

#include-once

;===============================================================================
; Function Name:     _Time()
; Description:   Return the system time formatted according to your regional settings
; Syntax:   _Time([include seconds[, Time format (12|24 hr)]])
; Parameter(s):   $iSec - whether to include the seconds in the output display
;                         0 - (default) don't include, anything except 0 - include
;                 $tFormat - type of return (12 or 24 hour) default = 12
;
; Requirement(s):   
; Return Value(s):   Formatted system time string (localized)
; Author(s):     George (GEOSoft) Gedye with thanks to Valuater
; Modification(s):   
; Note(s):    This is just a variation of _Time_12Hour() that allows you to return
;             a localized time in either 12 or 24 hour format.
;             To convert a time other than system time, see 
; Example(s):
#cs
   MsgBox(0, "TEST", _Time(1, 24)) ;; Include the seconds in 24 Hr. format
   MsgBox(0, "TEST", _Time(1) ;; Include the seconds in 12 Hr. format with AM\PM added
   MsgBox(0, "TEST", _Time() ;; 12 Hr. format without the seconds with AM\PM added (default)
#ce
;===============================================================================

Func _Time($iSec = 0, $tFormat = 12)
  Local $fKey = "HKCU\Control Panel\International", $ap = RegRead($fKey, "s1159")
  Local $pStr = RegRead($fKey, "s2359"), $tSep = RegRead($fKey, "sTime"), $sStr = ""
  Local $hour = @Hour
  If $tFormat = 12 Then
    If $hour = 0 Then $hour = 12
    If @Hour >= 12 Then
      $hour = @Hour
      If $hour > 12 Then $hour -= 12
      $ap = $pStr
    EndIf
  Else
    $ap = ""
    $hour = StringFormat("%02d",$hour)
  EndIf
  If $iSec <> 0 Then $sStr = $tSep & @Sec
  If $ap <> "" Then $ap = Chr(32) & $ap
  Return $hour & $tSep & @Min & $sStr & $ap
EndFunc ;<==> _Time()

;===============================================================================
; Function Name:     _Time_12Hour()
; Description:   Return the time formatted according to your regional settings
; Syntax:   _Time_12Hour([include seconds])
; Parameter(s):   $iSec - whether to include the seconds in the output display
;                         0 - (default) don't include, anything except 0 - include
; Requirement(s):   
; Return Value(s):   Formatted time string
; Author(s):     GEOSoft with thanks to Valuater for helping shorten the code
;                                    as well as good advice. That's one more I owe him.
; Modification(s):   
; Note(s):    
; Example(s):   MsgBox(0, "TEST", _Time_12Hour(1))
;===============================================================================

Func _Time_12Hour($iSec = 0)
  Local $fKey = "HKCU\Control Panel\International", $ap = RegRead($fKey, "s1159")
  Local $pStr = RegRead($fKey, "s2359"), $tSep = RegRead($fKey, "sTime"), $sStr = ""
  Local $hour = @Hour
  If $hour = 0 Then $hour = 12
  If @Hour >= 12 Then
    $hour = @Hour
    If $hour > 12 Then $hour -= 12
    $ap = $pStr
  EndIf
  If $iSec <> 0 Then $sStr = $tSep & @Sec
  Return $hour & $tSep & @Min & $sStr & Chr(32) & $ap
EndFunc ;<==> _Time_12Hour()

;===============================================================================
; Function Name:     _Time_CurrentDisp()
; Description:   Returns the current setting (Daylight or Standard)
; Syntax:         
; Parameter(s):   
; Requirement(s):   
; Return Value(s):   
; Author(s):   George (GEOSoft) Gedye
; Modification(s):   
; Note(s):    
; Example(s):   
;===============================================================================

Func _Time_CurrentDisp()
  Local $dMth, $sMth, $dDay, $sDay
  $objT_Zone = ObjGet("winmgmts:\\localhost\root\CIMV2")
  If IsObj ($objT_Zone) Then
    $tzItems = $objT_Zone.ExecQuery("SELECT * FROM Win32_TimeZone", "WQL",0x10 + 0x20)
    If IsObj($tzItems) Then
      For $objItem In $tzItems
        With $objItem
          $dMth = .DaylightMonth 
          $sMth = .StandardMonth
          $dDay = .DaylightDay
          $sDay = .StandardDay +1
        EndWith
        If (@Mon >= $dMth And @Mday >= $dDay) AND (@Mon <= $sMth And @MDay <= $sDay) Then
          Return $objItem.StandardName
        Else
          Return $objItem.DaylightName
        EndIf
      Next
    Else
      Return SetError(1)
    EndIf
  Else
    Return SetError(1)
  EndIf
EndFunc   ;<==> _Time_CurrentDisp()

;===============================================================================
; Function Name:     _Time_DST_Bias()
; Description:   Determine the Daylight Savings Time offset
; Syntax:         _Time_DST_Bias([return in hours])
; Parameter(s):   $hr - If 0 (default) then returns in minutes else returns in hours
; Requirement(s):   
; Return Value(s):   Success - Daylight savings time offset (usually 1 hour)
;                    Failure - Sets @Error to 1 if an object could not be connected
; Author(s):   George (GEOSoft) Gedye
; Modification(s):   
; Note(s):    
; Example(s):   
;===============================================================================

Func _Time_DST_Bias($hr = 0)
  $objT_Zone = ObjGet("winmgmts:\\localhost\root\CIMV2")
  If IsObj ($objT_Zone) Then
    $tzItems = $objT_Zone.ExecQuery("SELECT * FROM Win32_TimeZone", "WQL", 0x10 + 0x20)
    If IsObj($tzItems) Then
      For $objItem In $tzItems
        $dsBias = $objItem.DaylightBias
        If $hr <> 0 Then $dsBias /= 60
        Return $dsBias
      Next
    Else
      Return SetError(1)
    EndIf
  Else
    Return SetError(1)
  EndIf
EndFunc   ;<==> _Time_DST_Bias()

;===============================================================================
; Function Name:   _Time_FormatLocal()
; Description:     
; Syntax:          
; Parameter(s):    
; Requirement(s):  
; Return Value(s): - Success 
;                  - Failure 
; Author(s):       George (GEOSoft) Gedye
; Modification(s): 
; Note(s):    
; Example(s):   
#cs
   MsgBox(262208, "TIME", _Time_FormatLocal(@Hour & @Min & @Sec))
   MsgBox(262208, "TIME", _Time_FormatLocal(@Hour & "h" & @Min & ":" & @Sec))
#ce
;===============================================================================

Func _Time_FormatLocal()
   Local $sFormat = RegRead("HKCU\Control Panel\International", "sTimeFormat")
   If @Error Then $sFormat = "h:mm:ss tt"
   Local $aFormat = StringRegExp($sFormat,"\w*(.)\w*(.).*", 3)
   Return StringRegExpReplace($sTime, "(\d{2}).?(\d{2}).?(\d{2})", "\1" & $aFormat[0] & "\2" & $aFormat[1] & "\3")
EndFunc   ;<==> _Time_FormatLocal()

;===============================================================================
; Function Name:   _Time_InRange()
; Description:     Determine if the current time is within a given Range
; Syntax:          _Time_InRange("Low time", "High time")
; Parameter(s):    $sTime - Low end of the range (Start time)
;                  $eTime - High end of the range (End time)
; Requirement(s):   
; Return Value(s): 1 if current time is within the range otherwise 0
; Author(s):   George (GEOSoft) Gedye
; Modification(s): Replaced _NowTime(4) With Formatted @Hour & @Min
; Note(s):    This only uses Hours and Minutes
; Example(s):   MsgBox(0, "Time is good", _Time_InRange("08:00", "23:59"))
;===============================================================================

Func _Time_InRange($sTime, $eTime)
  Local $vRange = 0, $cTime
  $cTime = Number(@Hour & @Min)
  If $cTime >= Number(StringFormat("%04d",StringRegExpReplace($sTime, "\D", ""))) AND _
      $cTime <= Number(StringFormat("%04d",StringRegExpReplace($eTime, "\D", ""))) Then $vRange = 1
  Return $vRange
EndFunc   ;<==> _Time_InRange()

;===============================================================================
; Function Name:     _Time_UTC_Offset()
; Description:   Retrieve the offset from GMT
; Syntax:   _Time_UTC_Offset([return in hours])
; Parameter(s):   $hr - If 0 (default) then returns in minutes else returns in hours
; Requirement(s):   
; Return Value(s):   Success - Time zone offset
;                    Failure - Sets @Error to 1 if an object could not be connected
; Author(s):   George (GEOSoft) Gedye
; Modification(s):   
; Note(s):    
; Example(s):   
;===============================================================================

Func _Time_UTC_Offset($hr = 0)
  $objT_Zone = ObjGet("winmgmts:\\localhost\root\CIMV2")
  If IsObj ($objT_Zone) Then
    $tzItems = $objT_Zone.ExecQuery("SELECT * FROM Win32_TimeZone", "WQL", 0x10 + 0x20)
    If IsObj($tzItems) Then
      For $objItem In $tzItems
        $utcOffset = $objItem.Bias
        If $hr <> 0 Then $utcOffset /= 60
        Return $utcOffset
      Next
    Else
      Return SetError(1)
    EndIf
  Else
    Return SetError(1)
  EndIf
EndFunc ;<==> _Time_UTC_Offset()