commit 7fff983b4e096b79c6923852afed8f01ba6b7e3d
parent c715d4030343260c37b064df864a91ec1064a13b
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Sun, 19 Apr 2026 21:03:35 +0200
libc/time: Use correct year counting down
In mktime() we begin the loop in EPOCH, but when we are
before EPOCH then we want to move the time to the beginning
of the year before EPOCH, and for that reason we have to
decrement the year number before calling _daysyear()
otherwise we are going to substract the time of EPOCH instead
othe time of EPOCH-1.
Diffstat:
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/libc/time/mktime.c b/src/libc/time/mktime.c
@@ -111,12 +111,13 @@ mktime(struct tm *tm)
t = 0;
year = tm->tm_year + BASEYEAR;
+ i = EPOCH;
if (year >= EPOCH) {
- for (i = EPOCH; i < year; ++i)
- t += _daysyear(i) * SECDAY;
+ while (i < year)
+ t += _daysyear(i++) * SECDAY;
} else {
- for (i = EPOCH; i > year; --i)
- t -= _daysyear(i) * SECDAY;
+ while (i > year)
+ t -= _daysyear(--i) * SECDAY;
}
for (i = 0; i < tm->tm_mon; ++i)