Why do I get wrong result when using time.h library when using AVR boards, such Nano/Uno/Pro Micro (while when using TimeLib.h).
OUTPUT (for given Epoch ):
TimeLib: 2022-04-02 16:11:20
time.h: 0152-03-01 16:11:20
CODE:
#define CASE 1
#if CASE == 1
#include <time.h>
#elif CASE == 2
#include <TimeLib.h>
#endif
time_t bootTime = 1648915880;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\nStart!");
delay(1000);
char clk2[40];
#if CASE == 1
struct tm *tm = localtime(&bootTime);
sprintf(clk2, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
#elif CASE == 2
sprintf(clk2, "%04d-%02d-%02d %02d:%02d:%02d", year(bootTime), month(bootTime), day(bootTime), hour(bootTime), minute(bootTime), second(bootTime));
#endif
Serial.println(clk2);
}
void loop()
{
// put your main code here, to run repeatedly:
}
localtime()requires a timezone to be set. No idea how you set that. Usegmtime()instead to get the UTC (GMT) time which has no timezone. – Majenko Apr 03 '22 at 11:33153while we are 2022, strikes a bit odd. Also subtracting few secondsbootTime, yielded a non rational change in result. – guyd Apr 03 '22 at 12:22