Setting 'struct tm' tm_zone="GMT" doesn't work, takes default system timezone

Dear Dev-Meisters,


I'm trying to find out why setting tm_zone="GMT" in a "struct tm" is not working on Mac OS X 10.6.x. and mktime()....

However with gmtime() it works but apparently it's not recommended for Mac OS X...


I need to read in a data file with large datasets which contains date / time along with data.

Every date/time (can be about > 500000 lines) is converted to a time_t data structure to find out if there a gaps between dates.


However setting the structure "struct tm" with tm_zone="GMT" doesn't set it to GMT.

It always takes Mac OS X default time zone defined in the Sys Preferences.


I made succesful tests with "NSDate timeIntervalSince1970" (when setting the NSTimezone to GMT): this gives the correct GMT time...

Since > 500000 lines is a little bit overkill performance wise to always create an object I would prefer to use UNIX time stamp...


/* Test for 24 Feb. 2005 at 17h20:10 / Should return: 1109265610 as UTC time */

time_t currentTime;

struct tm myTm;


myTm.tm_year = 2005-1900; // need to subtract -1900

myTm.tm_mon = 2-1; // need to subtract -1 (since month: 0-11)

myTm.tm_mday = 24;

myTm.tm_hour = 17;

myTm.tm_min = 20;

myTm.tm_sec = 10;

myTm.tm_zone = "GMT";

myTm.tm_isdst = 0; // DayLight Saving Time flag = 0 = Off

myTm.tm_gmtoff = 0;


currentTime = mktime(&myTm);


Result on Mac OS X: 1109262010 !! Wrong result: -3600 seconds, should be 1109265610


However using:

currentTime = gmtime(&myTm);


gives the right result: 1109265610


Any suggestions is greatly appreciated

Posted on Apr 18, 2011 3:04 AM

Reply
2 replies

Apr 18, 2011 7:31 AM in response to gilcelli

You can't specify the timezone that way. That is a read-only value. To quote the man page:

"The mktime() function interprets the input structure according to the current timezone setting (see tzset(3)). The timegm() function interprets the input structure as representing Universal Coordinated Time (UTC)."

Why can't you use gmtime(), gmtime_r(), or timegm()?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Setting 'struct tm' tm_zone="GMT" doesn't work, takes default system timezone

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.