scc

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

commit 43bd1434081e8789eabfa11607528dd3ccd3f33b
parent 30bf44b5d95c8a2abc56636d986814cc365ab45e
Author: Naveen Narayanan <zerous@simple-cc.org>
Date:   Thu, 27 Aug 2020 15:54:08 +0200

libc: Fix strftime (time.h)

%U, conversion specifier, stipulates the first day of week 01 be a Sunday.
%W, conversion specifier, stipulates the first day of week 01 be a Monday.

Diffstat:
Msrc/libc/time/strftime.c | 35+++++++++++++++++++++++++++++------
1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/src/libc/time/strftime.c b/src/libc/time/strftime.c @@ -17,6 +17,33 @@ static char *months[] = { static char *am_pm[] = {"AM", "PM"}; +static int +first(int day, int year) +{ + int ny; + + ny = _newyear(year); + if (ny == day) + return 0; + return 7 - ny + day; +} + +static int +weeknum(struct tm* tm, int day) +{ + int fday, val; + + fday = first(day, tm->tm_year); + if (tm->tm_yday < fday) { + val = 0; + } else { + val = tm->tm_yday - fday; + val /= 7; + val++; + } + return val; +} + static size_t sval(char *s, size_t siz, char **strs, int abrev, int idx, int max) { @@ -210,14 +237,10 @@ strftime(char * restrict s, size_t siz, val = tm->tm_wday+1; goto number; case 'U': - val = tm->tm_yday / 7; - if (_newyear(tm->tm_year) == SAT) - val++; + val = weeknum(tm, SUN); goto number; case 'W': - val = tm->tm_yday / 7; - if (_newyear(tm->tm_year) == MON) - val++; + val = weeknum(tm, MON); goto number; case 'w': width = 1;