Time library returns WRONG year.

BriComp

Well-known member
Below is some simple code using the time library.
Unfortunately the breakTime function returns 54 as the year rather than 24.
Code:
void setup() {

    tmElements_t t;
    time_t ut;
    uint8_t h, m, s, d, mo, y;
    h = 12;
    m = 45;
    s = 0;
    d = 4;
    mo = 9;
    y = 2024;

    Serial.begin(9600);
    while (!Serial and millis() < 5000);

    setTime(h,m,s,d,mo,y);
    ut = now();
    Serial.println(ut);

    breakTime(ut,t);// 1725448362 + 3600, t);  // break time_t into elements

    Serial.print(t.Hour); Serial.print(":");
    Serial.print(t.Minute); Serial.print(":");
    Serial.print(t.Second); Serial.print("   ");
    Serial.print(t.Wday); Serial.print(" ");
    Serial.print(t.Day); Serial.print("/");
    Serial.print(t.Month); Serial.print("/");
    Serial.print(t.Year);
};

void loop() {

};
 
Not in my code that demonstrated the problem, sorry for NOT including complete code.
Code:
#include <TimeLib.h>

void setup() {

    tmElements_t t;
    time_t ut;
    uint8_t h, m, s, d, mo;
    int  y;

    h = 12;
    m = 45;
    s = 0;
    d = 4;
    mo = 9;
    y = 2024;

    Serial.begin(9600);
    while (!Serial and millis() < 5000);

    setTime(h,m,s,d,mo,y);
    ut = now();
    Serial.println(ut);

    breakTime(ut,t);// 1725448362 + 3600, t);  // break time_t into elements

    Serial.print(t.Hour); Serial.print(":");
    Serial.print(t.Minute); Serial.print(":");
    Serial.print(t.Second); Serial.print("   ");
    Serial.print(t.Wday); Serial.print(" ");
    Serial.print(t.Day); Serial.print("/");
    Serial.print(t.Month); Serial.print("/");
    Serial.print(t.Year);
};

void loop() {

};
 
Being that now() returns the number seconds passed since Jan 1st 1970, it's counting years since then, which is 54
But, is that the expected behavior?
Quick fix it to just add 1970 to the value of t.Year
 
EDIT:
Sorry @Paul I should have RTFM, or in this case the Instructions. It clearly says "tm.Year Year 0 to 99 (offset from 1970)".
Funny how many times you see what you want/expect to see rather than the true answer!

@Rezo Yeah a quick solution.
 
Last edited:
Thanks for the followup. I do have a couple other bug reports to investigate in that library. Will take this one off my list.
If it helps, the copy of makeTime() included in the cores library (in Time.cpp) is the one that has issues calculating leap days while the version in TimeLib works as expected.
 
Back
Top