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.

Compiler/SIMPLELINK-CC3220-SDK: Problems with timezones / setting time

Part Number: SIMPLELINK-CC3220-SDK


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?)

  • I am not familiar with the function SNTP_getTime, and how it should interact with the compiler RTS time functions.  I suspect the article Time and clock RTS Functions will be useful.  The part on the epoch, and how you change it, seems useful.  I suspect changing to the newer 1970 epoch will simplify things.

    Thanks and regards,

    -George

  • SNTP_getTime() just retrieves a 64 bit timestamp (seconds + fractional secs) from 1.1.1970 UTC. The epoch change with the macro did something, however now I do get the timestamp even before retrieving it from the network processor, so I assume it's being read from the host instead (running this under CCS currently.) Now I am not certain I am running the correct code (this will need to wait.)

    Thanks for the link, I think I'm going to use the 64-bit functions in any case!

    (for the TLS part: AFAIK it only uses the time to set a random cookie at the start, this is not part of the protocol as such. Currently have the issue bypassed by adding 21600 magic offset 6 h to the seconds, which is somewhat ugly and will probably bite me later.)
  • You probably should provide a custom implementation of the function time.  Model it on the last example in the article Customizing the clock and time Functions.

    Thanks and regards,

    -George