This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem with gmtime

Other Parts Discussed in Thread: MSP430F5418

Hi,

I'm writing code for an MSP430F5418 using CCE 4.2.1.00004.

I'm trying to use gmtime in time.h to convert the unix time coming out of my RTC to the struct tm format, the results coming out of gmtime seem to have an offset of 1 day and 6 hours. For example if I feed in the value 0 to gmtime i get

*(brokenTime) = {...}
 tm_sec = 0
 tm_min = 0
 tm_hour = 6
 tm_mday = 1
 tm_mon = 0
 tm_year = 0
 tm_wday = 1
 tm_yday = 0
 tm_isdst = -1

Where of course it should be all 0's, this offset seems to apply to any value if I feed in 10 I get,

*(brokenTime) = {...}
 tm_sec = 10
 tm_min = 0
 tm_hour = 6
 tm_mday = 1
 tm_mon = 0
 tm_year = 0
 tm_wday = 1
 tm_yday = 0
 tm_isdst = -1

and so on...

I'm guessing this is something I'm doing wrong, but I would be grateful for any help?

Regards,

Ben

  • For starters, tm_mday is 1-based, not 0-based, and tm_wday represents the day of the week (1=Monday).  I'm a little shaky on tm_hour, I'll check the standard.

  • You are using gmtime, which automatically adds the difference between your time zone and GMT, which is 6 hours.  Try localtime if this is undesirable.

  • Archaeologist said:

    You are using gmtime, which automatically adds the difference between your time zone and GMT, which is 6 hours.  Try localtime if this is undesirable.

    The difference between my time zone and GMT is not 6 hours (at the moment it's actually 0) so do I need to set something up so the compiler knows this?

    Also what your desctibing goes agasint the C standard where gmtime is supposed to return the time in GMT and localtime is supposed to take into account variations in time zone, otherwise the naming of the functions wouldn't make much sense.

  • Ben Walpole said:

    You are using gmtime, which automatically adds the difference between your time zone and GMT, which is 6 hours.  Try localtime if this is undesirable.

    The difference between my time zone and GMT is not 6 hours (at the moment it's actually 0) so do I need to set something up so the compiler knows this?

    Also what your desctibing goes agasint the C standard where gmtime is supposed to return the time in GMT and localtime is supposed to take into account variations in time zone, otherwise the naming of the functions wouldn't make much sense.

    [/quote]

    Ok, I've switched to using localtime and this is giving me the correct answers apart from the tm_wday field. As I understood it this should be a value between 0 and 6 giving how many days have elapsed since Sunday. When I call locatime with the value 12345678 which equates to Sat, 23 May 1970 21:21:18 the tm_wday field is 3 when surely it should be 6?

  • First of all, I'm sorry that in my haste to provide an answer, I didn't provide a complete answer.

    When I said "your time zone", I was mistaken.  The time zone is actually hard-coded in the RTS, so the 6 hours is the difference between the hard-coded time zone (US Central  Standard Time).

    The TI RTS does not use the POSIX epoch for the basis of time_t, it uses UTC-6 January 1, 1900.  See http://processors.wiki.ti.com/index.php/Time_and_clock_RTS_Functions and SDSCM00037169

  • Ok thanks, I can take that into account in my code.

    I know there isn't really a standard for implementing the epoch etc in C, but your implementation of time.h does seem to fly in the face of convention some what. I still think you have the functionality of gmtime and localtime the wrong way round.