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.

MSP430F6720: Where can I find the source code for localtime() and gmtime() presumably in time.c?

Part Number: MSP430F6720

Hi,

I am trying to convert Unix epoch time to the one used by MSP430 in CCS V10/TI v20.x.y compiler. Presumably TI compiler uses epoch time starting from 1900-01-01 00:00:00, at least the following code indicate so.

#include "time.h"

// Unix epoch time entered via UART console
uint32_t unixEpochTime = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
unixEpochTime += (uint32_t)2208988800; /*CCS use time since 1900-1-1 00:00:00*/

struct tm* calendarTime = gmtime(&unixEpochTime);

uint8_t debugInfo[48];
sprintf(debugInfo, "%04d-%02d-%02d %02d:%02d:%02d, ", calendarTime->tm_year + 1900, calendarTime->tm_mon + 1, calendarTime->tm_mday, calendarTime->tm_hour, calendarTime->tm_min, calendarTime->tm_sec);
sendCmdArray((uint8_t *)debugInfo, 21); // Output via UART console

If I enter 0x00000001 for 1 second since epoch or 00000E10 for 1 hour since epoch, I will get following calendar time as expected:

1970-01-01 00:00:01 // 1 second since epoch, as expected
1970-01-01 01:00:00 // 1 hour since epoch, as expected

However, if I enter 0x00015180 for 1 day since epoch, I get an output different from what I expected:

1970-01-01 05:47:44   // It should be 1970-01-02 00:00:00

What am I missing here?

Thanks.

  • However, if I enter 0x00015180 for 1 day since epoch, I get an output different from what I expected:

    The value 0x00005180 represents 1970-01-01 05:47:44, which is the output you got.

    On a MSP430 an int is 16-bits.

    In the following code I think the index into bytes[] will be promoted to unsigned ints, which are 16 bits, and thus the "(bytes[0] << 24)" and "(bytes[1] << 16)" will end up as zeros in the following:

    uint32_t unixEpochTime = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];

    Try changing the code to:

    uint32_t unixEpochTime = ((uint32_t) bytes[0] << 24) | ((uint32_t) bytes[1] << 16) | ((uint32_t) bytes[2] << 8) | (uint32_t) bytes[3];

  • Hi Mr. Gillon,

    Thanks for taking the time and identifying the error I made in input. I should have noticed that part myself.

    The type cast in the following line fixed my actual problem. I thought it was something in the implementation of localtime/gmtime.

    uint32_t unixEpochTime = ((uint32_t) bytes[0] << 24) | ((uint32_t) bytes[1] << 16) | ((uint32_t) bytes[2] << 8) | (uint32_t) bytes[3];

    I guess a temporal variable with default type int which is 16bit is created for both bytes[0] << 24 and bytes[1] << 16. I didn't think about this much, but apparently this caused several hours of agony.