Flaw in Teensy Loader time upload; off by time zone offset

shawn

Well-known member
When Teensy Loader uploads some firmware, it also sets the Teensy's clock. The value it's sending is incorrect. It looks like it gets the current system time from the computer and then adds the time zone offset. For example, it's about Wed Feb 4 9:40AM GMT-08:00 now, which corresponds to 1770226800 seconds. However, the Teensy prints 1770198000 seconds. This is exactly 8 hours too short.

In my strong view, the Teensy clock should be set to GMT time, because I'd expect _gettimeofday() to return time in UTC (note: almost the same as GMT) (so sayeth https://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html), given that the time zone parameter is obsolete. In other words, don't add the computer's local time zone offset when sending the time from Teensy Loader.

However, if it is really desired that _gettimeofday() returns the local time, with the time zone included (and I disagree with this), then so be it, but I'd like to point out another potential flaw. Say a program has proper time zone knowledge. It then needs to subtract the time zone offset from the system clock, but if this is ever fixed, that code would be wrong.

In short, I strongly believe _gettimeofday() should return the time in UTC and not in local time.

You can play with these numbers at https://epochconverter.com. A screenshot of these values is attached, as well as some source code that prints the current time in seconds since the Epoch.

C++:
#include <chrono>
#include <cinttypes>

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

  const auto now = std::chrono::system_clock::now().time_since_epoch();
  const auto now_s = std::chrono::duration_cast<std::chrono::seconds>(now);
  printf("now = %" PRId64 " s\r\n", now_s.count());

  printf("Copy this into https://epochconverter.com and observe"
         " the incorrect time zone.\r\n");
  printf("Copy into the field and press [Timestamp to Human date].\r\n");
}

void loop() {
  // put your main code here, to run repeatedly:

}
 

Attachments

  • Screenshot 2026-02-04 at 9.41.42 AM.png
    Screenshot 2026-02-04 at 9.41.42 AM.png
    196.3 KB · Views: 8
Last edited:
Apparently, I posted about this before: https://forum.pjrc.com/index.php?th...der-to-set-utc-time-and-not-local-time.77042/

The salient link: https://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html
It specifies that gettimeofday() should return the time in seconds since the Epoch.

Quote:
The gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since the Epoch

Could there at least be an option that can be provided to Teensy Loader (eg. in the menus or startup script or whatever) that causes it to send the time in UTC instead of local time?
 
Back
Top