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.

RM46L852: how to convert the Unix time to UTC time fetched from SNTP server

Part Number: RM46L852

Hello,

I am using the Lwip Stack in my software  and integrated SNTP.c and .h file in my project.

I have debugged the software and found that whenever control hit the break point at below line

static void
sntp_process(u32_t *receive_timestamp)
{
/* convert SNTP time (1900-based) to unix GMT time (1970-based)
* @todo: if MSB is 1, SNTP time is 2036-based!
*/
time_t t = (ntohl(receive_timestamp[0]) - DIFF_SEC_1900_1970);

}

We get seconds value in 't' instance. for example t = 1518585734

my question is, how should i convert the unix time to UTC time (YY:MM:DATE:HR:MIN:SEC). is there any ready made function available?

Shall i use following code?

static void
sntp_process(u32_t *receive_timestamp)
{


/* convert SNTP time (1900-based) to unix GMT time (1970-based)
* @todo: if MSB is 1, SNTP time is 2036-based!
*/
time_t timenow = (ntohl(receive_timestamp[0]) - DIFF_SEC_1900_1970);

// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"

struct tm ts;

char buf[80];

ts = *localtime(&timenow);

strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);

}

please advice.