Time isn't converted correct from Unix time

I'm trying to convert unix time to readable format but if i do the conversion it's off by 30 years and 1 day and I dont have a clue why (time is correct). Hope someone can help me out.

Code :

char   buf[80];
time_t rawtime = epoch; // time(epoch);
struct tm ts = *localtime(&rawtime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
Serial.println(buf);

Output :

Seconds since Jan 1 1900 = 3878967015
Unix time = 1669978215
Sun 2052-12-01 10:50:15 ?

unix time starts 1970, but some system use seconds since 2000

"In 1984, the International Astronomical Union decided that epoch 2000.0 would begin at 1200 UTC on January 1, 2000."

What Is Epoch? (techtarget.com)

@noiasca in astronomy

Thx, that was indeed the problem.Substracted the difference and now it works.

char   buf[80];
time_t rawtime = epoch-946684800; // time(epoch);
struct tm ts = *localtime(&rawtime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
Serial.println(buf);

Output :

Seconds since Jan 1 1900 = 3878974629
Unix time = 1669985829
Fri 2022-12-02 12:57:09 ?

I tried to "autodetect" the epoche and calculate a difference/offset on wokwi:

epoch_test.ino - Wokwi Arduino and ESP32 Simulator

Rather than using a magic number, you can use UNIX_OFFSET, provided by Arduino's time.h.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.