Tool/software: TI C/C++ Compiler
Hello!
I am having a hard time trying to set the timezone and/or time from SNTP to get the readings in UTC (and stored as such.) I think gmtime() and localtime() return wrong readings. I do use SNTP_getTime(), set the system time with clock_settime(CLOCK_REALTIME), and then check gmtime() and localtime(), where if not touching anything I get values when printed with strftime(gmtime(time(NULL))) and strftime(localtime(time(NULL))) are;
- showing UTC in localtime
- showing value off by 6 hours into future in gmtime()
Writing _tz (is this the correct way to set the timezone?) seems to affect localtime() (as expected) and the printed time zone (which is "CST" otherwise.) Setting _tz.timezone to 0 will make locatime() and gmtime() match, but the result is off (6 h into future.) I use the following to set retrieve the time and print it:
uint64_t ntpts; int rval = SNTP_getTime(NULL, 0, NULL, &ntpts); if (rval == 0) { // seconds offset from 1900 to 1970 is 2208988800
// floor(1e9 / 2**32) = 15258 to scale unsigned fraction = ((ntpts & ((1ul << 32) - 1)) * 15258ull) >> 16; struct timespec ts = { .tv_sec = (ntpts >> 32) - 2208988800ul, .tv_nsec = fraction }; clock_settime(CLOCK_REALTIME, &ts); } char buf[64]; time_t t = time(NULL); /* can be -1, should check */ // there's nothing split second, and the zone cannot be printed? strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S %Z", gmtime(&t)); puts(buf); strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S %Z", localtime(&t)); puts(buf);
Is there a way to get the time zones correct (I am fine with having in everything in UTC!) BTW, do I need to set the time to the network processor to (sl_DeviceSet) for TLS sockets, and what time should be used there (gmtime/localtime / some specific timezone?)