From: voegelas-hi6Y0CQ0nG0@public.gmane.org (Andreas Vögele)
Cc: bug-guile-mXXj517/zsQ@public.gmane.org
Subject: off-by-one error in SRFI 19's date-week-number procedure?
Date: Sun, 25 Apr 2004 19:24:46 +0200 [thread overview]
Message-ID: <m2k704gech.fsf@ID-28718.user.uni-berlin.de> (raw)
While browsing Guile's regression tests I came across the following
test which checks the procedure date-week-number:
(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)))))
I executed the second date-week-number expression with Guile, Scsh,
and MzScheme. All three Scheme implementations return 1 instead of 0:
$ guile --version
Guile 1.6.4
$ guile
guile> (use-modules (srfi srfi-19))
guile> (date-week-number (make-date 0 0 0 0 7 1 1984 0) 0)
1
$ scsh -o srfi-19
Welcome to scsh 0.6.5 (0.6.6)
Type ,? for help.
> (date-week-number (make-date 0 0 0 0 7 1 1984 0) 0)
1
$ mzscheme
Welcome to MzScheme version 206p1, Copyright (c) 2004 PLT Scheme, Inc.
> (require (lib "19.ss" "srfi"))
> (tm:date-week-number (tm:make-date 0 0 0 0 7 1 1984 0) 0)
1
Guile, Scsh and MzScheme use the same implementation of
date-week-number:
(define (date-week-number date day-of-week-starting-week)
(quotient (- (date-year-day date)
(tm:days-before-first-week date day-of-week-starting-week))
7))
The description of date-week-number in SRFI 19 says:
date-week-number date day-of-week-starting-week -> integer
The ordinal week of the year which holds this date, ignoring a first
partial week. 'Day-of-week-starting-week' is the integer
corresponding to the day of the week which is to be considered the
first day of the week (Sunday=0, Monday=1, etc.)."
1984 starts with a Sunday:
January 1984
S M Tu W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
If the ordinal week is zero based and Sunday is the first day of the
week, I'd expect January, 1st 1984 to be in week 0 and January, 8th to
be in week 1:
(date-week-number (make-date 0 0 0 0 1 1 1984 0) 0) => 0
(date-week-number (make-date 0 0 0 0 8 1 1984 0) 0) => 1
But why is the January, 6th (a Friday) in week 0 and January, 7th in
week 1?
(date-week-number (make-date 0 0 0 0 6 1 1984 0) 0) => 0
(date-week-number (make-date 0 0 0 0 7 1 1984 0) 0) => 1
It seems that there is an off-by-one error. Here's a modified version
of date-week-number:
(define (date-week-number date day-of-week-starting-week)
(quotient (- (date-year-day date)
(tm:days-before-first-week date day-of-week-starting-week)
1)
7))
I've checked it briefly and it seems to return the right values.
next reply other threads:[~2004-04-25 17:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-25 17:24 Andreas Vögele [this message]
[not found] ` <m2k704gech.fsf-Nuue7Tx7Zq4TOjq7KYOY4QnKXTlcm+PAG9Ur7JDdleE@public.gmane.org>
2004-04-25 17:39 ` off-by-one error in SRFI 19's date-week-number procedure? Andreas Vögele
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=m2k704gech.fsf@ID-28718.user.uni-berlin.de \
--to=voegelas-hi6y0cq0ng0@public.gmane.org \
--cc=bug-guile-mXXj517/zsQ@public.gmane.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).