Iterating Windows Mobile Timezones in C

I’m trying to debug an error in some managed code related to timezones when a P/Invoke is throwing a native access violation exception.  To make sure it’s not something with the platform itself I decided to do the same in native code first to ensure that it’s not some platform limitation or bug.  Surprisingly I didn’t readily find any sample code for doing it, so here’s my contribution to the community at large.  It uses dynamic loading (and it doesn’t make sure that the function loads succeed, so you might want to add error checking if you actually intend to use this):

#include <windows.h>

typedef void    (*INITCITYDB)(void);
typedef void    (*UNINITCITYDB)(void);
typedef void    (*LOADTZDATA)(void);
typedef void    (*FREETZDATA)(void);
typedef int        (*GETNUMZONES)(void);
typedef void *    (*GETTZDATABYOFFSET)(int, int*);
typedef void *    (*GETTZDATA)(int);

struct TZData
    TCHAR *Name;
    TCHAR *ShortName;
    TCHAR *DSTName;
    int GMTOffset;
    int DSTOffset;

int _tmain(int argc, _TCHAR* argv[])
    TZData *pTZ = NULL;
    int index;

    // load the library
    HINSTANCE hLib = LoadLibrary(_T(“CityDB.dll”));

    // load the CityDB functions
    INITCITYDB InitCityDB = (INITCITYDB)GetProcAddress(
            hLib, _T(“InitCityDb”));
            hLib, _T(“UninitCityDb”));
    LOADTZDATA ClockLoadAllTimeZoneData = (LOADTZDATA)GetProcAddress(
            hLib, _T(“ClockLoadAllTimeZoneData”));
    FREETZDATA ClockFreeAllTimeZoneData = (FREETZDATA)GetProcAddress(
            hLib, _T(“ClockFreeAllTimeZoneData”));
    GETNUMZONES ClockGetNumTimezones = (GETNUMZONES)GetProcAddress(
            hLib, _T(“ClockGetNumTimezones”));
    GETTZDATABYOFFSET ClockGetTimeZoneDataByOffset = 
(GETTZDATABYOFFSET)GetProcAddress(hLib, _T(“ClockGetTimeZoneDataByOffset”));
    GETTZDATA ClockGetTimeZoneData = (GETTZDATA)GetProcAddress(
            hLib, _T(“ClockGetTimeZoneData”));

    // Init the library

    // load the TZ data

    // find out how many zones are defined
    int zoneCount = ClockGetNumTimezones();

    // interate through them all
    for(int zone = 0 ; zone < zoneCount ; zone++)
        // these are pointers to a timezone data struct
        pTZ = (TZData*)ClockGetTimeZoneDataByOffset(zone, &index);

    // unload the TZ data

    // uninit the library

    return 0;

4 thoughts on “Iterating Windows Mobile Timezones in C”

  1. Too funny, I was trying to do something like this in managed code not even 2 days ago. Were you able to find something in managed code or did you have to rely on this to get the listing for you?



  2. The managed code version is already in the SDF ( It does have a bug though, which is what I was trying to debug here. The bug did not affect pre-WM 5.0 devices, but threw an invalid access exception in WM 5.0. You’ll need to change the P/Invoke definition for ClockGetTimeZoneDataByOffset to take a second parameter that is a ref int (the original only has one parameter, which is incorrect).


    1. This is the only place where I can add my notes. Please, note, that struct TZData can have more members. If you want to loop thought the list of zones and use one of them via method SetTimeZoneInformation() you MUST know also dates of time-changes. Not only those time-shifts. Otherwise windows-ui will not correctly indicate selected timezone. I haven’t discovered all possible struct-members and their names so I am only pasting what I have now:

      (Names correspond to struct TIME_ZONE_INFORMATION and child structs. See SetTimeZoneInformation() at MSDN)

      Can someone add more knowledge?

      struct TZData
      TCHAR *Name;
      TCHAR *ShortName;
      TCHAR *DSTName;
      int GMTOffset;
      int DSTOffset;

      // Following names are guessed:
      // For example this timezone has probably different meaning:

      int DaylightBias;
      WORD b;
      WORD StandardDateMonth;
      WORD StandardDateDay;
      WORD StandardDateWDay;
      WORD StandardDateHour;
      WORD StandardDateMinute;
      WORD StandardDateSecond;
      WORD i;
      WORD j;
      WORD DaylightDateMonth;
      WORD DaylightDateDay;
      WORD DaylightDateWDay;
      WORD DaylightDateHour;
      WORD DaylightDateMinute;
      WORD DaylightDateSecond;
      WORD q;
      WORD r;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s