CE-Programming/toolchain

localtime reports the wrong day on leap years

TheMiningTeamYT opened this issue · 5 comments

On leap years, if you use localtime, tm_mday from the returned struct will be 1 too high until March 1st.
Here's my code:
time.tar.gz

Can you please be more descriptive?

Somehow I expected that when I submitted the issue.
If you call localtime (from time.h/ctime), it returns a pointer to a tm struct
That struct has a property (tm_mday) that is supposed to reflect the current day of the month.
However, if it is before March 1st on a leap year, the value it returns is 1 higher than it should be.
(For instance, on January 1st, 2024 it returns 2 [as in January 2nd, 2024] instead of 1)
(It works fine if the date is March 1st or after or if the year is not a leap year)
You can see this with the code I attached. (If you press '4' to open the clock, it shows the current reported day of the month, which is taken directly from the struct pointer returned by localtime)

This is now fixed in the latest nightly! Thanks for the report

Also @TheMiningTeamYT have you used the asctime or strftime C functions before? I imagine it would greatly clean up your code.

As a matter of fact, no! I'll try it sometime.