Search code examples
cmacoscocoatimezonenstimezone

equivalent to msdn TIME_ZONE_INFORMATION struct in OSX


I'm working on OS X client that suppose to send messages to remote server along with data about client current timezone. However, the server is programmed to handle only windows timezone format according to the following structure that defined here here

typedef struct _TIME_ZONE_INFORMATION {
    LONG       Bias;
    WCHAR      StandardName[32];
    SYSTEMTIME StandardDate;
    LONG       StandardBias;
    WCHAR      DaylightName[32];
    SYSTEMTIME DaylightDate;
    LONG       DaylightBias;
 } TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION;

I've managed to find some of the needed information in OS X framework such as NSTimeZone, but contains only partial list.

perhaps there's a similar structure in OS X that can be generated by some Cocoa module ?

NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
string(localTimeZone.name.UTF8String);
localTimeZone.daylightSavingTimeOffset;

Solution

  • You have all of that information in the NStimeZone class. First thing you'll need to figure out is wether or not that timezone currently observes daylight saving time, which you can do via the isDaylightSavingTime property. Then based on that information you'll have to figure out a date where it observes DST and where it doesn't, the nextDaylightSavingTimeTransition property will help you there (plus the information of wether it currently is daylight saving time). With that out of the way, you can get the names of the timezone using the abbreviationForDate: method, once for DST and once for without. Similarly, you can figure out the bias via secondsFromGMTForDate: for both dates and a bit of math. Finally, you'll have to use NSCalendar to get a NSDateComponents object for you two dates with which you can fill the SYSTEMTIME struct members. Of course, there are time zones which don't observe DST and you'll have to account for these cases.