scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 880f47a7e9feb9c4ac5a5a1fa4def916dc01a2cd
parent dc1a3248ccf389c94c16abf747bcd3348991c6c8
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:
Msrc/libc/time/strftime.c | 4+++-
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; }