Getting current timezone on windows

[quote=82863:@Michel Bujardet]Yes. I mentioned it above. You found the problem. Congratulations.

I have since incorporated in the project Dave Sysemore method of detecting the Windows version :

[code]Function GetVersion() As string
#If TargetWin32

dim OS as string  = "Windows"
dim OS_CODE as integer

//try to be more specific of windows version
Soft Declare Sub GetVersionExA Lib "Kernel32" ( info As Ptr )
Soft Declare Sub GetVersionExW Lib "Kernel32" ( info As Ptr )

Dim info As MemoryBlock

If System.IsFunctionAvailable( "GetVersionExW", "Kernel32" ) Then
  info =  New MemoryBlock( 20 + (2 * 128) )
  info.Long( 0 ) = info.Size
  GetVersionExW( info )
Else
  info =  New MemoryBlock( 148 )
  info.Long( 0 ) = info.Size
  GetVersionExA( info )
End If

Dim Str As String
OS_CODE=info.Long(4)*100+info.long(8)
Select Case OS_CODE
Case 400
  os = "Windows 95/NT 4.0"
Case 410
  os = "Windows 98"
Case 490
  os = "Windows Me"
Case 300 To 399
  os = "Windows NT 3.51"
  OS_CODE=30
Case 500
  os = "Windows 2000"
Case 501
  os = "Windows XP"
Case 502
  os = "Windows Server 2003"
Case 600
  os = "Windows Vista"
Case 601
  os = "Windows 7"
Case 602
  os = "Windows 8"
End Select
Str = " Build " + Str( info.Long( 12 ) )

If System.IsFunctionAvailable( "GetVersionExW", "Kernel32" ) Then
  Str = Str + " " + Trim( info.WString( 20 ) )
Else
  Str = Str + " " + Trim( info.CString( 20 ) )
End If

os = os + Str
Return os

#EndIf
End Function
[/code]

Now the method works on both XP and earlier, and reads as :

[code] Sub Action()
if not TargetWin32 then return
dim tzname as string
dim os as string = GetVersion
if instr(os, “Windows XP”)>0 then
tzname = “StandardName”
elseif instr(os, “Windows 95/NT 4.0”)>0 or instr(os, “Windows 98”)>0 or instr(os, “Windows Me”)>0 then
return
else
tzname = “TimeZoneKeyName”
end if

// get a registry key
Dim reg As New RegistryItem(“HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\TimeZoneInformation”)
Dim TimeZ As String
// now we look on all values on this key
For i As Integer = 0 To reg.KeyCount- 1
Dim name As String = reg.Name(i)
Dim value As Variant = reg.Value(i)
if name = tzname then
TimeZ = reg.Value(i)
exit
end if
Next

reg = New RegistryItem(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones”+TimeZ)
Dim TimeCities as String
// now we look on all values on this key
For i As Integer = 0 To reg.KeyCount- 1
Dim name As String = reg.Name(i)
Dim value As Variant = reg.Value(i)
if name = “Display” then
TimeCities = reg.Value(i)
exit
end if
Next

// and display the time zone as cities
MsgBox TimeZ+EndOfLine+TimeCities

End Sub
[/code]

Thanks to the OP for an interesting challenge…[/quote]

Second thought. To get Windows versions, would it not be enough to get the KEY Value from windows registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName ?

[quote=82777:@Lee Page]How odd Microsoft says so?

http://technet.microsoft.com/en-us/library/bb491007.aspx[/quote]

Command: systeminfo works on mine Windows XP SP3. But I try to avoid using Shell where possible

OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 3 Build 2600

Probably yes. I just used Dave’s method because it was available, but no doubt the registry contains the information.

I will look into it :slight_smile:

[quote=82923:@Richard Duke]OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 3 Build 2600[/quote]

Mine is the home edition. That maybe the problem.

This is the final version of timezone for Windows. As suggested by John Hansen, I use the
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName to find the Windows version.

I also have placed the key value reading in a method, so the code is more compact.

[code]Sub Action()
if not TargetWin32 then return
dim tzname as string

dim os as string = GetzKey(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“ProductName”)
if instr(os, “Windows XP”)>0 then
tzname = “StandardName”
elseif instr(os, “Windows 95/NT 4.0”)>0 or instr(os, “Windows 98”)>0 or instr(os, “Windows Me”)>0 then
return
else
tzname = “TimeZoneKeyName”
end if

dim TimeZ as string = GetzKey(“HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\TimeZoneInformation”,tzname)

dim TimeCities as string
TimeCities = GetzKey(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones”+TimeZ,“Display”)

// and display the time zone and cities
MsgBox TimeZ+EndOfLine+TimeCities

End Sub
[/code]

[code]Function GetzKey(path as string, key as string) As string
// get a registry key
Dim reg As New RegistryItem(path)
Dim TimeZ As String
// now we look on all values on this key
dim name as string
For i As Integer = 0 To reg.KeyCount- 1
name = reg.Name(i)
if name = key then
TimeZ = reg.Value(i)
exit
end if
Next
return TimeZ

End Function
[/code]

Here is the source code

[quote=82945:@Michel Bujardet]Richard Duke OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 3 Build 2600
Mine is the home edition. That maybe the problem.[/quote]

i think u r right… the link say system info exist in Windows XP Pro

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/systeminfo.mspx?mfr=true

Apparently XP Home did not have systeminfo, according to the results of a googling with “systeminfo xp home edition”. I found several pages advising use of utilities that do the same.

Well. XP is now officially retired, and I will not loose sleep over that :wink:

I had problems in getting TimeZone like “+01:00” on Mac, Windows and Linux…
I found a solution see:
Forum link 18813-getting-current-timezone
Someone could test if it works in other Counties? Thank you
Rob

Or just use xojo.core.timezone.current

[quote=82688:@John Hansen]Okay. This is the closest I can get in windows. Now it will show the countries as displayed in timezone settings for windows. I have just added two properties to be displayed in the code.

Will display: b Amsterdam, Berlin, Bern, Rome, Stockholm, vienna[/b] for my timezone, same as windows

[code]
// This example will get the computer system running Windows timezone
//http://library.wmifun.net/cimv2/win32_timezone.html

//http://msdn.microsoft.com/en-us/library/aa394498%28v=vs.85%29.aspx

Dim locator, objWMIService, objs, objProperty As OLEOBJECT
Dim nobjs as Integer

// Connect to WMI
locator = new oleObject(“WbemScripting.SWbemlocator”, true)

Dim wmiServiceParams(2) as variant
wmiServiceParams(1) = “.”
wmiServiceParams(2) = “root\cimv2”

objWMIService= locator.invoke(“ConnectServer”, wmiServiceParams)

// Run the WMI query
objs = objWMIService.ExecQuery (“SELECT * FROM Win32_TimeZone”)

nobjs = objs.count - 1

For i as integer = 0 to nobjs
Dim stringData As String

objProperty = objs.ItemIndex(i)
// ItemIndex() is not supported in Windows XP only from Windows Vista and upwards

stringData = "DaylightName: "  + objProperty.Value("DaylightName") + EndOfLine
stringData = stringData + "TimeZone: "  + objProperty.Value("StandardName") + EndOfLine
stringData = stringData + "Description: "  + objProperty.Value("Description") + EndOfLine
stringData = stringData + "Caption: "  + objProperty.Value("Caption") + EndOfLine
msgbox stringData

Next

locator = Nil

exception err as oleexception
msgbox err.message[/code][/quote]

This worked for me once I changed the output string as I just needed the current time zone and not the offset.

stringData = mid(objProperty.Value("Description"),5,6)