Jump to content

_TimeToGmt


theguy0000
 Share

Recommended Posts

Quick func to convert a time into Greenwich Mean Time.

CODE
Func _TimeToGmt ($hour, $min)
    $zone = RegRead ("HKLM\SYSTEM\ControlSet001\Control\TimeZoneInformation", "StandardName")
    $disp = RegRead ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\"&$zone, "Display")
    $aOffset = _StringBetween ($disp, "(", ")")
    $offset = $aOffset[0]
    $offset = StringReplace ($offset, "GMT", "")
    If $offset = "" Then $offset = "+00:00"
    If StringLeft ($offset, 1) = "-" Then
        $addSubtract = "+"
    ElseIf StringLeft ($offset, 1) = "+" Then
        $addSubtract = "-"
    EndIf
    $offset = StringTrimLeft ($offset, 1)
    $offsetSplit = StringSplit ($offset, ":")
    $offset_hour = $offsetSplit[1]
    $offset_mins = $offsetSplit[2]
    $newHour = Execute (String($hour)&$addSubtract&$offset_hour)
    If $newHour < 0 Then
        $newHour = 24 - Abs($newHour)
    ElseIf $newHour > 24 Then
        $newHour = $newHour - 24
    EndIf
    $newMin = Execute (String($min)&$addSubtract&$offset_mins)
    If $newMin < 0 Then
        $newMin = 60 - Abs($newMin)
        $newHour -= 1
    ElseIf $newHour > 59 Then
        $newMin = $newMin - 60
        $newHour += 1
    EndIf
    Local $ret[2]
    $ret[0] = $newHour
    $ret[1] = $newMin
    Return $ret
EndFunc

Returns a one-dimensional array with two elements:

$ret[0] is the new hour in GMT

$ret[1] is the new minute in GMT. This usually doesn't change from the original minute, but does in some time zones...

Oh, and it has only been tested on XP SP2, It probably will not work on other versions of Windows.

Edited by theguy0000

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

#include <String.au3>
$a = _TimeToGmt(@HOUR, @MIN)
If IsArray($a) Then MsgBox(0, "GMT", $a[0] & ":" & $a[1])

Func _TimeToGmt ($hour, $min)
    $zone = RegRead ("HKLM\SYSTEM\ControlSet001\Control\TimeZoneInformation", "StandardName")
    $disp = RegRead ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\"&$zone, "Display")
    $aOffset = _StringBetween ($disp, "(", ")")
    $offset = $aOffset[0]
    $offset = StringReplace ($offset, "GMT", "")
    If $offset = "" Then $offset = "+00:00"
    If StringLeft ($offset, 1) = "-" Then
        $addSubtract = "+"
    ElseIf StringLeft ($offset, 1) = "+" Then
        $addSubtract = "-"
    EndIf
    $offset = StringTrimLeft ($offset, 1)
    $offsetSplit = StringSplit ($offset, ":")
    $offset_hour = $offsetSplit[1]
    $offset_mins = $offsetSplit[2]
    $newHour = Execute (String($hour)&$addSubtract&$offset_hour)
    If $newHour < 0 Then
        $newHour = 24 - Abs($newHour)
    ElseIf $newHour > 24 Then
        $newHour = $newHour - 24
    EndIf
    $newMin = Execute (String($min)&$addSubtract&$offset_mins)
    If $newMin < 0 Then
        $newMin = 60 - Abs($newMin)
        $newHour -= 1
    ElseIf $newHour > 59 Then
        $newMin = $newMin - 60
        $newHour += 1
    EndIf
    Local $ret[2]
    $ret[0] = $newHour
    $ret[1] = $newMin
    Return $ret
EndFunc

Doesnt work.

# MY LOVE FOR YOU... IS LIKE A TRUCK- #
Link to comment
Share on other sites

I think A working one I prefer byref parameters is better than the array... Just a taste feeling

$hour=@HOUR
$min=@MIN
_TimeToGMT($hour,$min)
MsgBox(0,"GMT time", $hour & ":" & $min)

Func _TimeToGmt (ByRef $hour, ByRef $min, $daylight =-1)
    $zone = RegRead ("HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation", "StandardName")
    $disp = RegRead ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\"&$zone, "Display")
    
    ; daylight saving information
    If $daylight = -1 Then
    ; need the corresponding date to be taken in account
        $TZI_struct = DllStructCreate("dword;char[64];int;int;int;int;int;int;int;int;dword;char[64];int;int;int;int;int;int;int;int;dword")
        $daylight = DllCall("kernel32.dll","int","GetTimeZoneInformation","ptr",DllStructGetPtr($TZI_struct))
        $TZI_struct = 0

        $daylight -= 1 ; daylight saving time
    EndIf
    
    $hour += $daylight
    
    $offset=StringRegExp($disp,"\(GMT(.*)\)",3)
    If @error = 0 Then
        $offset=$offset[0]
    Else
        $offset = "+00:00"
    EndIf
    
    If StringLeft ($offset, 1) = "-" Then
        $addSubtract = 1
    ElseIf StringLeft ($offset, 1) = "+" Then
        $addSubtract = 0
    EndIf
    $offset = StringTrimLeft ($offset, 1)
    $offsetSplit = StringSplit ($offset, ":")
    $offset_hour = int($offsetSplit[1])
    $offset_mins = int($offsetSplit[2])
    
    If $addSubtract Then
        $min = $min + $offset_mins
        If $min > 59 Then
            $min = $min - 60
            $hour += 1
        EndIf
        $hour = $hour + $offset_hour
        If $hour > 24 Then
            $hour = $hour - 24
        EndIf
    Else
        $min = $min - $offset_mins
        If $min < 0 Then
            $min = 60 - Abs($min)
            $hour -= 1
        EndIf
        $hour = $hour - $offset_hour
        If $hour < 0 Then
            $hour = 24 - Abs($hour)
        EndIf
    EndIf
    
EndFunc

Link to comment
Share on other sites

I think A working one I prefer byref parameters is better than the array... Just a taste feeling

$hour=@HOUR
$min=@MIN
_TimeToGMT($hour,$min)
MsgBox(0,"GMT time", $hour & ":" & $min)

Func _TimeToGmt (ByRef $hour, ByRef $min, $daylight =-1)
    $zone = RegRead ("HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation", "StandardName")
    $disp = RegRead ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\"&$zone, "Display")
    
    ; daylight saving information
    If $daylight = -1 Then
    ; need the corresponding date to be taken in account
        $TZI_struct = DllStructCreate("dword;char[64];int;int;int;int;int;int;int;int;dword;char[64];int;int;int;int;int;int;int;int;dword")
        $daylight = DllCall("kernel32.dll","int","GetTimeZoneInformation","ptr",DllStructGetPtr($TZI_struct))
        $TZI_struct = 0

        $daylight -= 1 ; daylight saving time
    EndIf
    
    $hour += $daylight
    
    $offset=StringRegExp($disp,"\(GMT(.*)\)",3)
    If @error = 0 Then
        $offset=$offset[0]
    Else
        $offset = "+00:00"
    EndIf
    
    If StringLeft ($offset, 1) = "-" Then
        $addSubtract = 1
    ElseIf StringLeft ($offset, 1) = "+" Then
        $addSubtract = 0
    EndIf
    $offset = StringTrimLeft ($offset, 1)
    $offsetSplit = StringSplit ($offset, ":")
    $offset_hour = int($offsetSplit[1])
    $offset_mins = int($offsetSplit[2])
    
    If $addSubtract Then
        $min = $min + $offset_mins
        If $min > 59 Then
            $min = $min - 60
            $hour += 1
        EndIf
        $hour = $hour + $offset_hour
        If $hour > 24 Then
            $hour = $hour - 24
        EndIf
    Else
        $min = $min - $offset_mins
        If $min < 0 Then
            $min = 60 - Abs($min)
            $hour -= 1
        EndIf
        $hour = $hour - $offset_hour
        If $hour < 0 Then
            $hour = 24 - Abs($hour)
        EndIf
    EndIf
    
EndFunc
you're right, it's a lot easier that way ;)

edit: and i looked through the relevant keys, but I can't figure out how to make it tell if daylight savings is on...

Edited by theguy0000

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

  • 10 months later...

Does this function calculate the GMT time correctly while Daylight savings is in effect?

I didn't see it calling the DaylightBias or DaylightStart values....

I mean - not all areas use the same start and end points (or even the same Bias) for DST - I have to be sure.

------------------------------------

PS: Here's all the information I could find on the web regarding the values:

Bias (DWORD->Decimal == minutes offset from GMT for timezone)

StandardBias (?deviation from GMT outside of daylight savings or timezone?)

DaylightBias (DWORD->Decimal == minutes offset from GMT for daylight savings)

StandardStart (Binary Date == Date to switch to Standard Time)

DaylightStart (Binary Date == Date to switch to Daylight Time)

Binary Date Example:

00 00 0A 00 00 00 05 00 03 00 00 00 00 00 00 00

00 00 - is the Year from a 1900 time base.

0A 00 - The first byte is the Month (January is 01).

00 00 - The first byte is the DayOfWeek (Sunday=0).

05 00 - The first byte is the Week (starts at 1 and 5 means last).

03 00 - The first byte is the Hour.

00 00 - The fist byte is the Minute.

00 00 - The first byte is the Seconds.

00 00 - the first byte is the Millisecond.

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

  • 4 years later...

$hour=@HOUR + GMTdiff()
If $hour>24 Then $hour-=24
MsgBox(0,"GMT time", StringFormat("%02d:%02d:%02d", $hour, @MIN, @SEC) )

Func GMTdiff()
Local $TZI_struct = DllStructCreate("long;wchar[32];word;word;word;word;word;word;word;word;long;wchar[32];word;word;word;word;word;word;word;word;long")
Local $posun = DllCall(@SystemDir & "\kernel32.dll","int","GetTimeZoneInformation","ptr",DllStructGetPtr($TZI_struct))
Local $GMT=DllStructGetData($TZI_struct, 1)
If $posun[0]=2 Then $GMT+=DllStructGetData($TZI_struct, 21)
$TZI_struct = 0
Return Round($GMT/60)
EndFunc

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