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.

Convert time_t to tm struct

Hi,

I have project, where I have to update the RTC (it's the hibernate module in calendar mode), but when I get the seconds via a time_t member, and convert into a tm structure, that I can use to adjust the Hibernate module via the driver library, the TM structure seems to be incorrect:

        tm_sec    int    43    0x20028A2C
        tm_min    int    4    0x20028A30    
        tm_hour    int    14    0x20028A34    
        tm_mday    int    19    0x20028A38    
        tm_mon    int    11    0x20028A3C    
        tm_year    int    44    0x20028A40    
        tm_wday    int    2    0x20028A44    
        tm_yday    int    353    0x20028A48    
        tm_isdst    int    -1    0x20028A4C    

As you can see, eg.: the year is correct (1970 + 44 = 2014), the day also correct, today is 19th, but the month is incorrect (it tells us, that there is November instead of December). This is the code fraction, that is responsible the conversion:

        u32 time;
        xQueueReceive(queueHandle, &time, 0);
        std::tm* t = std::localtime(&time);

Can anybody tell me, what did I wrong? Or what can I additionally to do for the correct behavioral?

Regards,

Norbert

  • Hi,

    If you use CCS, please take into account TI uses time epoch as 1900, not 1970.

    There are various disscutions on this subject on CCS forum...

    Petrei

  • Hi Petrei,

    thank you for your reply. I adjusted the the base date, and the result seems to be better:
    tm_sec int 59 0x20028A2C
    tm_min int 37 0x20028A30
    tm_hour int 8 0x20028A34
    tm_mday int 20 0x20028A38
    tm_mon int 11 0x20028A3C
    tm_year int 114 0x20028A40
    tm_wday int 6 0x20028A44
    tm_yday int 353 0x20028A48
    tm_isdst int -1 0x20028A4C

    but there is a problem yet: the mount has been slipped 1 month backward. It says, it's November yet. As I see, every other fields are appropriate.

    Regards,
    Norbert
  • Hi,

    One mention: in Tiva/utils/ustdlib.c there are a couple of functions: ulocaltime() and umktime() based on traditional Unix epoch, 1970 - the source code is available. 

    I apologize - I forgot about these… try them (I did not observed problems with ulocaltime...).

    Petrei

  • I haven't used the Tiva and I am not familiar with the time implementation. Most implementations have tm_mon as 0 to 11. So 11 would be December.

    EDIT: Missed one character and the meaning came out all wrong. Sorry.

  • Hello Norman,

    Norman Wong said:
    Never use the Tiva

    I beg to differ on your point. Even though TM4C129 has the month as 1 to 12 unlike most implementations of tm_mon from 0-11, it would correct the same via the SW driverlib (please see comment in the file hibernate.c). So a clear read of the data sheet and it's driverlib would allow the user to still get the correct calendar format.

       //

       // Populate the date and time fields in the psTime structure.

       // We must subtract 1 from the month, since the time structure lists

       // the month from 0 to 11 and the HIB lists it from 1 to 12.

       //

    Regards

    Amit

  • Oops. Sorry Amit. Typed too rapidly and pressed Reply without properly proof-reading. I mean "(I) never use(d) the Tiva (myself)". I've edited my reply to avoid an confusion.