DateTime and timezone.

G

Guest

I have some dates stored in a database. They are based on a fixed time zone (not GMT don't ask why). I want to know the offset between the local time and the time stored in the database. Is there an easy way to figure out the time span difference between the local time and an arbitrary timezone (not GMT)? Say the database stores the times in MT(mountain time) and the local time is in CT(central time) I want some way to figure out the time difference so I can compare the local time with what is stored in the database

Thank you for your suggestions

Kevin
 
J

Jerry Pisk

Not easily and not in .Net. You have to p/invoke and go from your time zone
to UTC and then to the target zone. And it will only work on WinXP and
Win2k3 Server and newer. Take a look at TzSpecificLocalTimeToSystemTime and
SystemTimeToTzSpecificLocalTime Win32 API functions.

Jerry

Kevin Burton said:
I have some dates stored in a database. They are based on a fixed time
zone (not GMT don't ask why). I want to know the offset between the local
time and the time stored in the database. Is there an easy way to figure out
the time span difference between the local time and an arbitrary timezone
(not GMT)? Say the database stores the times in MT(mountain time) and the
local time is in CT(central time) I want some way to figure out the time
difference so I can compare the local time with what is stored in the
database.
 
J

Jerry Pisk

Windows (NT line) has this in the registry, look under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.
The TZI binary value is the TIME_ZONE_INFORMATION structure needed to do
those time translations.

Jerry

Kevin Burton said:
Thank you that will give me a start. Looking at these API's it looks like
you have to pass in a TIME_ZONE_INFORMATION structure that contains the
offset from GMT (UTC). That is what I want the system to give me. I don't
want to have to hard code the fact that eastern time is an hour ahead of
central time and mountain time is a hour behind central time. This seems
error prone and as you stated "not easy". Is there some function that will
return the timezone information based on a standard description such as "ET"
for eastern time zone or "CT" for central timezone?
Thanks again.

----- Jerry Pisk wrote: -----

Not easily and not in .Net. You have to p/invoke and go from your time zone
to UTC and then to the target zone. And it will only work on WinXP and
Win2k3 Server and newer. Take a look at
TzSpecificLocalTimeToSystemTime and
 
G

Guest

You have been very helpful. I am well on my way to a good support class. I have chosen to use managed C++ rather than P/Invoke. It is just easier for me.

One more question. The registry has entries for the standard time zones and there is a TIME_ZONE_INFORMATION struction associated with each of the standard time zones. However, if I want to get the local time in the Mountain Standard Time zone (I am in Central Time Zone) I do a GetSystemTime() then SystemTimeToTzSpecificLocalTime using the key value from the "Mountain Standard Time" for the TIME_ZONE_INFORMATION. If I calculate the difference between the current time, in CT, and MT it comes out to be 2 hours instead of 1 hour like I would expect. I am guessing that since I passed "Mountain Standard Time" the TIME_ZONE_INFORMATION structure that is returned is for "Mountain Standard Time" and not "Mountain Daylight Savings Time". I see that in the structure that there are bias' for daylight savings time and standard time. How do I detect that daylight savings time is in effect?

Thanks again.

Kevin
 
J

Jerry Pisk

You should get the daylight savings time if it's in effect. I don't know why
you're getting time that's an hour off. If the original system time correct
in UTC?

Btw there is a class to do this using P/Invoke, it's on gotdotnet I think, I
guess I should've said that earlier...

Jerry

Kevin Burton said:
You have been very helpful. I am well on my way to a good support class. I
have chosen to use managed C++ rather than P/Invoke. It is just easier for
me.
One more question. The registry has entries for the standard time zones
and there is a TIME_ZONE_INFORMATION struction associated with each of the
standard time zones. However, if I want to get the local time in the
Mountain Standard Time zone (I am in Central Time Zone) I do a
GetSystemTime() then SystemTimeToTzSpecificLocalTime using the key value
from the "Mountain Standard Time" for the TIME_ZONE_INFORMATION. If I
calculate the difference between the current time, in CT, and MT it comes
out to be 2 hours instead of 1 hour like I would expect. I am guessing that
since I passed "Mountain Standard Time" the TIME_ZONE_INFORMATION structure
that is returned is for "Mountain Standard Time" and not "Mountain Daylight
Savings Time". I see that in the structure that there are bias' for daylight
savings time and standard time. How do I detect that daylight savings time
is in effect?
 
K

Kevin Burton

The sequence that I am using:

GetTimeZoneInformation - This returns the TIME_ZONE_INFORMATION for the
current compter. I am in the central time zone so I see a bias of 360
minutes (6 hours) with a daylight savings time bias of -60 (-1 hour).
GetSystemTime - This returns the UTC current time it show a time that is 5
hours ahead of the current time.
Read TIME_ZONE_INFORMATION from registry - For my test I am using "Mountain
Standard Time" which shows a bias of 420 minutes and 0 daylight savings
bias.
SystemTimeToTzSpecificLocalTime - I pass the system time that I received
from GetSystemTime and the TIME_ZONE_INFORMATION that I read from the
registry. With no errors this functions tells me that it is 2 hours earlier
in the mountain time zone as opposed to the 1 hour that I was suspecting. I
must be calling these wrong and/or there is something with daylight savings
time that I am not taking into account.

Thank you for your help. I will look for the P/Invoke sample.

Kevin
 
G

Guest

Do you have the name of the class? I was unable to find it on GotDotNet

Thanks again

Kevin
 
J

Jerry Pisk

Are you sure you're using "Mountain Standard Time" and not "US Mountain
Standard Time"? The difference between those two is that "US Mountain
Standard Time" is used in Arizona (except Navajo Nation) and doesn't observe
daylight saving time. That would explain the difference you're seeing (and
show that your code works fine :)

Jerry
 

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