From 0bb2076fa4475443ee93d4f538ce4f70c67ca932 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johan=20Sundstr=C3=B6m?= <oyasumi@gmail.com>
Date: Tue, 11 Feb 2003 16:59:58 +0100
Subject: [PATCH] Somewhat kludgy fix to make the Calendar format functions
 report the correct start date for certain boundary case weeks.

Rev: lib/modules/Calendar.pmod/Gregorian.pmod:1.7
Rev: lib/modules/Calendar.pmod/YMD.pmod:1.49
Rev: lib/modules/Calendar.pmod/testsuite.in:1.6
---
 lib/modules/Calendar.pmod/Gregorian.pmod |  1 +
 lib/modules/Calendar.pmod/YMD.pmod       | 10 +++++-----
 lib/modules/Calendar.pmod/testsuite.in   |  8 +++++++-
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/lib/modules/Calendar.pmod/Gregorian.pmod b/lib/modules/Calendar.pmod/Gregorian.pmod
index ad064933b8..8603a9c7c0 100644
--- a/lib/modules/Calendar.pmod/Gregorian.pmod
+++ b/lib/modules/Calendar.pmod/Gregorian.pmod
@@ -94,6 +94,7 @@ static array(int) year_month_from_month(int y,int m)
 static array(int) month_from_yday(int y,int yd)
 {
 // [month,day-of-month,ndays,month-year-day]
+   if (yd<1) return ({12,31+yd,32,335+year_leap_year(y-1)});
    int l=year_leap_year(y);
    if (yd<32) return ({1,yd,31,1});
    yd-=l;
diff --git a/lib/modules/Calendar.pmod/YMD.pmod b/lib/modules/Calendar.pmod/YMD.pmod
index bf2f320714..853d78a85b 100644
--- a/lib/modules/Calendar.pmod/YMD.pmod
+++ b/lib/modules/Calendar.pmod/YMD.pmod
@@ -399,7 +399,7 @@ class YMD
       if (m==CALUNKNOWN) make_month();
       if (w==CALUNKNOWN) make_week();
       return sprintf("%04d-%02d-%02d (%s) -W%02d-%d (%s)",
-		     y,m,md,
+		     ((yd < 1)?y-1:y),m,md,
 		     month_shortname(),
 		     w,wd, // fixme - what weekday?
 		     week_day_shortname());
@@ -432,25 +432,25 @@ class YMD
 		 ("SunMonTueWedThuFriSat"/3)[compat_week_day(wd)],
 		 md,
 		 ("zzzJanFebMarAprMayJunJulAugSepOctNovDec"/3)[m],
-		 y);
+		 ((yd < 1)?y-1:y));
    }
 
    string format_ymd()
    {
       if (m==CALUNKNOWN) make_month();
-      return sprintf("%04d-%02d-%02d",y,m,md);
+      return sprintf("%04d-%02d-%02d",((yd < 1)?y-1:y),m,md);
    }
 
    string format_ymd_short()
    {
       if (m==CALUNKNOWN) make_month();
-      return sprintf("%04d%02d%02d",y,m,md);
+      return sprintf("%04d%02d%02d",((yd < 1)?y-1:y),m,md);
    }
 
    string format_ymd_xshort()
    {
       if (m==CALUNKNOWN) make_month();
-      return sprintf("%02d%02d%02d",y%100,m,md);
+      return sprintf("%02d%02d%02d",((yd < 1)?y-1:y)%100,m,md);
    }
 
    string format_iso_week()
diff --git a/lib/modules/Calendar.pmod/testsuite.in b/lib/modules/Calendar.pmod/testsuite.in
index ceaa67d078..8c7136e8c4 100644
--- a/lib/modules/Calendar.pmod/testsuite.in
+++ b/lib/modules/Calendar.pmod/testsuite.in
@@ -1,4 +1,4 @@
-dnl $Id: testsuite.in,v 1.5 2002/12/11 22:57:24 nilsson Exp $
+dnl $Id: testsuite.in,v 1.6 2003/02/11 15:59:58 jhs Exp $
 
 dnl NOTE:
 dnl   *every* time the Calendar tests have failed, it's not the 
@@ -102,3 +102,9 @@ test_day("2001-01-01 Mon 2001w01")
 
 test_day("2003-12-31 Wed 2004w01")
 test_day("2004-01-01 Thu 2004w01")
+
+test_eq([[ Calendar.ISO.Week(2001, 1)->format_ymd() ]], "2001-01-01")
+test_eq([[ Calendar.ISO.Week(2002, 1)->format_ymd() ]], "2001-12-31")
+test_eq([[ Calendar.ISO.Week(2003, 1)->format_ymd() ]], "2002-12-30")
+test_eq([[ Calendar.ISO.Week(2004, 1)->format_ymd() ]], "2003-12-29")
+test_eq([[ Calendar.ISO.Week(2005, 1)->format_ymd() ]], "2005-01-03")
-- 
GitLab