ref: 8598e0bb82f2722b96e7afa37922ba5107062692
parent: 48514a5765fbec60ae8f776d7bff7fad66cfd578
author: Naveen Narayanan <zerous@simple-cc.org>
date: Thu Aug 27 11:54:08 EDT 2020
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.
--- a/src/libc/time/strftime.c
+++ b/src/libc/time/strftime.c
@@ -17,6 +17,33 @@
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 @@
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;