commit ef9e03046d35d4d0a3a73316e4675470228ebc98
parent 3ffe44ba0b84317b63bfcdbfe4bc1418f0ae59ca
Author: Naveen Narayanan <zerous@simple-cc.org>
Date: Tue, 29 Sep 2020 10:29:57 +0200
libc: Fix first() in strftime.c
We consider the days of the week to be circular in nature starting
with SUN - 0 through SAT - 6. Hence, we have to consider two use
cases:
1. When the difference between the respective day and the newyear
doesn't wrap around Eg: day = WED newyear = TUE
2. When the difference between the respective day and the newyear
wraps around Eg: day = TUE newyear = FRI
This patch fixes the algorithm by considering both usecases.
Diffstat:
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/libc/time/strftime.c b/src/libc/time/strftime.c
@@ -25,7 +25,9 @@ first(int day, int year)
ny = _newyear(year);
if (ny == day)
return 0;
- return 7 - (ny + day);
+ if (day - ny < 0)
+ return 7 - (ny - day);
+ return day - ny;
}