aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2013-06-28 12:03:58 -0400
committerRich Felker <dalias@aerifal.cx>2013-06-28 12:03:58 -0400
commitc5faf1bf09c01641e74844c4bfb0f129bc3974a1 (patch)
treee65eb79340fe4aeb1e55fee65a04fbcb99de00ca /src
parent1e2281b8356d1935a129ddb199d71677f19a1619 (diff)
downloadmusl-c5faf1bf09c01641e74844c4bfb0f129bc3974a1.zip
musl-c5faf1bf09c01641e74844c4bfb0f129bc3974a1.tar.gz
musl-c5faf1bf09c01641e74844c4bfb0f129bc3974a1.tar.bz2
implement week numbers and half of the week-based-year logic for strftime
output for plain week numbers (%U and %W) has been sanity-checked, and output for the week-based-year week numbers (%V) has been checked extensively against known-good data for the full non-negative range of 32-bit time_t. year numbers for week-based years (%g and %G) are not yet implemented.
Diffstat (limited to 'src')
-rw-r--r--src/time/strftime.c41
1 files changed, 38 insertions, 3 deletions
diff --git a/src/time/strftime.c b/src/time/strftime.c
index b69a83a..ab1c6dc 100644
--- a/src/time/strftime.c
+++ b/src/time/strftime.c
@@ -8,6 +8,14 @@
const char *__langinfo(nl_item);
+static int is_leap(int y)
+{
+ /* Avoid overflow */
+ if (y>INT_MAX-1900) y -= 2000;
+ y += 1900;
+ return !(y%4) && ((y%100) || !(y%400));
+}
+
size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
{
nl_item item;
@@ -116,10 +124,37 @@ do_fmt:
fmt = "%d";
goto number;
case 'U':
- case 'V':
+ val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
+ fmt = "%02d";
+ goto number;
case 'W':
- // FIXME: week number mess..
- continue;
+ val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
+ fmt = "%02d";
+ goto number;
+ case 'V':
+ val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
+ /* If 1 Jan is just 1-3 days past Monday,
+ * the previous week is also in this year. */
+ if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
+ val++;
+ if (!val) {
+ val = 52;
+ /* If 31 December of prev year a Thursday,
+ * or Friday of a leap year, then the
+ * prev year has 53 weeks. */
+ int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
+ if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
+ val++;
+ } else if (val == 53) {
+ /* If 1 January is not a Thursday, and not
+ * a Wednesday of a leap year, then this
+ * year has only 52 weeks. */
+ int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
+ if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
+ val = 1;
+ }
+ fmt = "%02d";
+ goto number;
case 'w':
val = tm->tm_wday;
fmt = "%d";