From: Tomas Volf <~@wolfsden.cz>
To: 74841@debbugs.gnu.org
Cc: Tomas Volf <~@wolfsden.cz>
Subject: bug#74841: [PATCH] srfi-19: Fix ~V converter in date->string.
Date: Thu, 12 Dec 2024 22:03:21 +0100 [thread overview]
Message-ID: <20241212210322.32016-2-~@wolfsden.cz> (raw)
The ~V is supposed to print ISO week number, not a week number. This
commit fixes that.
* module/srfi/srfi-19.scm (date-week-number-iso): New procedure taken
from the reference implementation.
(directives)<#\V>: Use it.
* test-suite/tests/srfi-19.test ("date->string ~V"): Add tests taken
from the reference test suite.
---
module/srfi/srfi-19.scm | 21 ++++++++++++-
test-suite/tests/srfi-19.test | 57 ++++++++++++++++++++++++++++++++++-
2 files changed, 76 insertions(+), 2 deletions(-)
diff --git a/module/srfi/srfi-19.scm b/module/srfi/srfi-19.scm
index d809ac1ec..77be57a0e 100644
--- a/module/srfi/srfi-19.scm
+++ b/module/srfi/srfi-19.scm
@@ -753,6 +753,25 @@
(days-before-first-week date day-of-week-starting-week))
7))
+(define (date-week-number-iso date)
+ ;; The week with the year's first Thursday is week 01.
+ (let* ((first-day-of-the-week (week-day 1 1 (date-year date)))
+ (offset (if (> first-day-of-the-week 4) 0 1))
+ ;; -2: decrement one day to compensate 1-origin of date-year-day,
+ ;; and decrement one more day for Sunday belongs to the previous week.
+ (w (+ (floor-quotient (+ (date-year-day date) first-day-of-the-week -2)
+ 7)
+ offset)))
+ (cond ((zero? w)
+ ;; date belongs to the last week of the previous year
+ (date-week-number-iso (make-date 0 0 0 0 31 12
+ (- (date-year date) 1) 0)))
+ ((and (= w 53)
+ (<= (week-day 1 1 (+ (date-year date) 1)) 4))
+ ;; date belongs to the first week of the next year
+ 1)
+ (else w))))
+
(define (current-date . tz-offset)
(let ((time (current-time time-utc)))
(time-utc->date
@@ -1043,7 +1062,7 @@
(display (padding (date-week-number date 0)
#\0 2) port))))
(cons #\V (lambda (date pad-with port)
- (display (padding (date-week-number date 1)
+ (display (padding (date-week-number-iso date)
#\0 2) port)))
(cons #\w (lambda (date pad-with port)
(display (date-week-day date) port)))
diff --git a/test-suite/tests/srfi-19.test b/test-suite/tests/srfi-19.test
index 55eb82320..5e3e1f445 100644
--- a/test-suite/tests/srfi-19.test
+++ b/test-suite/tests/srfi-19.test
@@ -412,7 +412,62 @@ incomplete numerical tower implementation.)"
(with-test-prefix "date-week-number"
(pass-if (= 0 (date-week-number (make-date 0 0 0 0 1 1 1984 0) 0)))
(pass-if (= 0 (date-week-number (make-date 0 0 0 0 7 1 1984 0) 0)))
- (pass-if (= 1 (date-week-number (make-date 0 0 0 0 8 1 1984 0) 0)))))
+ (pass-if (= 1 (date-week-number (make-date 0 0 0 0 8 1 1984 0) 0))))
+
+ (let ((convert (λ (lst)
+ (date->string
+ (make-date 0 0 0 0
+ (caddr lst) (cadr lst) (car lst)
+ 0)
+ "~V"))))
+ (with-test-prefix "date->string ~V"
+ (pass-if-equal "Thursday, week 53" "53"
+ (convert '(2020 12 31)))
+ (pass-if-equal "Friday, week 53 (previous year)" "53"
+ (convert '(2021 1 1)))
+ (pass-if-equal "Sunday, week 53 (previous year)" "53"
+ (convert '(2021 1 3)))
+ (pass-if-equal "Monday, week 1" "01"
+ (convert '(2021 1 4)))
+
+ (pass-if-equal "Sunday, week 52" "52"
+ (convert '(2019 12 29)))
+ (pass-if-equal "Monday, week 1 (next year)" "01"
+ (convert '(2019 12 30)))
+ (pass-if-equal "Tuesday, week 1 (next year)" "01"
+ (convert '(2019 12 31)))
+ (pass-if-equal "Wednesday, week 1" "01"
+ (convert '(2020 1 1)))
+
+ (pass-if-equal "Saturday, week 52" "52"
+ (convert '(2016 12 31)))
+ (pass-if-equal "Sunday, week 52 (previous year)" "52"
+ (convert '(2017 1 1)))
+ (pass-if-equal "Monday, week 1" "01"
+ (convert '(2017 1 2)))
+ (pass-if-equal "Sunday, week 1" "01"
+ (convert '(2017 1 8)))
+ (pass-if-equal "Monday, week 2" "02"
+ (convert '(2017 1 9)))
+
+ (pass-if-equal "Sunday, week 52" "52"
+ (convert '(2014 12 28)))
+ (pass-if-equal "Monday, week 1 (next year)" "01"
+ (convert '(2014 12 29)))
+ (pass-if-equal "Tuesday, week 1 (next year)" "01"
+ (convert '(2014 12 30)))
+ (pass-if-equal "Wednesday, week 1 (next year)" "01"
+ (convert '(2014 12 31)))
+ (pass-if-equal "Thursday, week 1" "01"
+ (convert '(2015 1 1)))
+ (pass-if-equal "Friday, week 1" "01"
+ (convert '(2015 1 2)))
+ (pass-if-equal "Saturday, week 1" "01"
+ (convert '(2015 1 3)))
+ (pass-if-equal "Sunday, week 1" "01"
+ (convert '(2015 1 4)))
+ (pass-if-equal "Monday, week 2" "02"
+ (convert '(2015 1 5))))))
;; Local Variables:
--
2.46.0
reply other threads:[~2024-12-12 21:03 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='20241212210322.32016-2-~@wolfsden.cz' \
--to=~@wolfsden.cz \
--cc=74841@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).