views:

65

answers:

1

Customers from around the world can send certain 'requests' to my server application. All these customers are located in many different time zones.

For every request, I need to map the request to an internal C++ class instance. Every class instance has some information about its 'location', which is also indicated by a time zone.

Every customer can send requests relating to instances belonging to different time zones. To prevent my customers from converting everything themselves to the time zone of the 'target' instance, I have to convert everything myself from one time zone to another. However, I only find in C++ (unmanaged, native) functions to convert times between local time and GTM, but not from/to a time zone that is not your current time zone.

I could ask my customers to send every date time in UTC or GTM, but that does not solve my problem as I still have to convert this to the time zone of the 'instance', which can be any time zone in the world.

I also don't seem to find a Windows function that does this. What I do find is a managed .Net class that does this, but I want to keep my application strictly unmanaged.

Are there any Windows (XP, Vista, 7, 2003, 2008) functions that I can use (and which I overlooked in the documentation), or are there any other free algorithms that can convert between one time zone and the other?

Notice that it is not the GMT-difference that is posing the problem, but the actual DST-transition moment that seems to depend on the time zone. E.g:

  • Western Europe goes from non-DST to DST the last Sunday before April 1st.
  • USA goes from non-DST to DST the 2nd Sunday after March 1st.
  • China has no DST.
  • Australia goes from non-DST to DST the 1st Sunday after October 1st.

All this DST-transition information is available somewhere in the Windows registry. Problem is: which Windows function can I use to exploit this information.

+2  A: 

I don't know of a way to extract information about other TimeZones via the API: I've seen it done by querying the registry though (we do this in a WindowsCE-based product).

The TimeZones are defined as registry keys under

HKLM\Software\Microsoft\Windows NT\Current Version\Time Zones

Each key contains several values, and the one which tells you about offsets & Daylight Savings is the TZI key. This is a binary blob, and it represents this structure:

typedef struct
{
    LONG m_nBias;
    LONG m_nStandardBias;
    LONG m_nDaylightBias;
    SYSTEMTIME m_stcStandardDate;
    SYSTEMTIME m_stcDaylightDate;
} TZI;

Look up MSDN's TIME_ZONE_INFORMATION page (http://msdn.microsoft.com/en-us/library/ms725481(v=VS.85).aspx) for how to interpret the Bias fields, and especially the StandardDate and DaylightDate fields -- they are gently abused to support constructs like "the last Saturday in April".

HTH

AAT
Thanks, via your link I found the Windows function GetTimeZoneInformationForYear (>= Vista SP1) which returns this information for a specific time zone. Problem is: I don't find sample code for this function, and the function documentation does not make it clear which input data should be exactly given to this function. Do you have an example?
Patrick
@Patrick - I don't have an example I'm afraid: since we're working on WinCE we don't have GetTimeZoneInformationForYear() as an option. But I am not sure if it helps you much: you want to get the Time Zone for a remote customer, if I understand right, not the time zone for your computer in an arbitrary year.
AAT