Add option to Teensy Loader to set UTC time and not local time

shawn

Well-known member
In short, I very strongly think that the RTC should contain the seconds in UTC and not local time. Could there at least be a Teensy Loader option that sets UTC time and not local time?

When the Teensy Loader program sets the RTC on the Teensy, it uses local time, but I believe it should use UTC time. Functions such as std::chrono::system_clock::now().time_since_epoch().count() and gettimeofday() expect the time to be in UTC. All local time zone adjustments should be done by the program and not the RTC.

The example program shows that the time will be off by an amount equal to the time zone offset in which the board was programmed. Compare with the value at https://www.unixtimestamp.com/. For example, in Portland, the current time zone offset is 7 hours. The values shown by the program will be 7*60*60=25200 too small.

Example code:
C++:
#include <sys/time.h>

#include <chrono>
#include <cinttypes>
#include <cstdint>

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 4000) {
    // Wait for Serial
  }

  const auto now = std::chrono::system_clock::now();
  printf("chrono: %" PRId32 "\r\n",
         static_cast<int32_t>(now.time_since_epoch().count()/1'000'000'000));

  struct timeval t;
  gettimeofday(&t, nullptr);
  printf("gettimeofday: %" PRId32 "\r\n",
         static_cast<int32_t>(t.tv_sec));
}

void loop() {
}

Refs:
* https://www.unixtimestamp.com/ <-- This shows the current time in seconds since the epoch
* https://pubs.opengroup.org/onlinepubs/009695399/functions/gettimeofday.html
* https://en.cppreference.com/w/cpp/chrono/system_clock.html
 
Back
Top