reading time zone information

L

Linda Liu [MSFT]

Hi Steve,

You can make use of the Win32 API function GetTimeZoneInformation in your
application to get the current time zone on the local machine.

The following is a sample.

Imports System.Runtime.InteropServices

Public Class Form1

<StructLayout(LayoutKind.Sequential)> _
Public Structure SYSTEMTIME
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure TIME_ZONE_INFORMATION
<MarshalAs(UnmanagedType.I4)> _
Public Bias As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
Public StandardName As String
Public StandardDate As SYSTEMTIME
<MarshalAs(UnmanagedType.I4)> _
Public StandardBias As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
Public DaylightName As String
Public DaylightDate As SYSTEMTIME
<MarshalAs(UnmanagedType.I4)> _
Public DaylightBias As Int32

End Structure

Declare Auto Function GetTimeZoneInformation Lib "kernel32.dll" (ByRef
lpTimeZoneInformation As TIME_ZONE_INFORMATION) As UInt32

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tziNative As TIME_ZONE_INFORMATION
GetTimeZoneInformation(tziNative)
End Sub
End Class


Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steve

Hi All

I have a windows application written in VB.net 2005

The users have to select a State of Australia, which I use to check they have the correct windows time zone selected in control panel

Dim myzone As TimeZone = TimeZone.CurrentTimeZone
If they change the Time zone whilst my application is running, a new call to Dim myzone As TimeZone = TimeZone.CurrentTimeZone returns the same time zone NOT the new one

They have to restart my application to get the correct time zone

It appears that the time zone is read by my application on startup and is cached rather than reading directly from Windows each time

How can I get my application to refresh its Time zone value, so the user doesn't have to restart my application, after changing the Windows time zone?

Regards

Steve
 
C

Cor Ligthert[MVP]

Steve,

As the time zone is changed in your system, then in my opinion it is normally as well the situation in your application, which thinking error is it I am making?

Cor
"Steve" <[email protected]> schreef in bericht Hi All

I have a windows application written in VB.net 2005

The users have to select a State of Australia, which I use to check they have the correct windows time zone selected in control panel

Dim myzone As TimeZone = TimeZone.CurrentTimeZone
If they change the Time zone whilst my application is running, a new call to Dim myzone As TimeZone = TimeZone.CurrentTimeZone returns the same time zone NOT the new one

They have to restart my application to get the correct time zone

It appears that the time zone is read by my application on startup and is cached rather than reading directly from Windows each time

How can I get my application to refresh its Time zone value, so the user doesn't have to restart my application, after changing the Windows time zone?

Regards

Steve
 
S

Steve

Hi Cor

Thanks for the response

When the user changes the time zone in Windows control panel with my application still running, a repeat call to
Dim myzone As TimeZone = TimeZone.CurrentTimeZone
returns the time zone BEFORE they changed it. NOT the new time zone

If they restart my application, the call to Dim myzone As TimeZone = TimeZone.CurrentTimeZone, returns the correct NEW time zone

I need to know how to make my application refresh the time zone from windows

Regards
Steve
Steve,

As the time zone is changed in your system, then in my opinion it is normally as well the situation in your application, which thinking error is it I am making?

Cor
"Steve" <[email protected]> schreef in bericht Hi All

I have a windows application written in VB.net 2005

The users have to select a State of Australia, which I use to check they have the correct windows time zone selected in control panel

Dim myzone As TimeZone = TimeZone.CurrentTimeZone
If they change the Time zone whilst my application is running, a new call to Dim myzone As TimeZone = TimeZone.CurrentTimeZone returns the same time zone NOT the new one

They have to restart my application to get the correct time zone

It appears that the time zone is read by my application on startup and is cached rather than reading directly from Windows each time

How can I get my application to refresh its Time zone value, so the user doesn't have to restart my application, after changing the Windows time zone?

Regards

Steve
 
A

Armin Zingler

As the time zone is changed in your system, then in my opinion it is
normally as well the situation in your application, which thinking
error is it I am making?

Cor, In the application, the time zone information is outdated. He changed
it in control panel, but in his code, there is still the old information.

Steve, I don't think this is changable, because, when you think about it:
When does the time zone change while the PC is running? Whenever you are
flying from one zone to the other. This is so rare that it is acceptable to
restart the application, IMO.

Armin

 
S

Steve

Hi Armin

When the users select a 'state of Australia' in my application and try to
save, I compare the State they have selected to the Time zone currently set
in Windows
If it is different, I display a message advising them of the discrepancy and
ask them to either select the correct state OR change the windows time zone
If they minimise my program to go to Control panel to change the Time Zone I
want my application to recognise this and do a new comparison of the Time
zone vs the state of Australia

My application is used Australia wide and I need to ensure each location has
the correct Time Zone set as I use date offsets depending on the location to
ensure dates at Head Office are consistent

regards
Steve
 
A

Andrew Morton

Steve said:
When the users select a 'state of Australia' in my application and
try to save, I compare the State they have selected to the Time zone
currently set in Windows
If it is different, I display a message advising them of the
discrepancy and ask them to either select the correct state OR change
the windows time zone If they minimise my program to go to Control
panel to change the Time Zone I want my application to recognise this
and do a new comparison of the Time zone vs the state of Australia

I suppose you'll have to look in the registry:
http://vbnet.mvps.org/index.html?code/locale/timezonedisplay.htm

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top