[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

possible bug in srfi-19 implementation (fix included)

From: joost
Subject: possible bug in srfi-19 implementation (fix included)
Date: Fri, 27 Sep 2002 14:03:19 +0200


I'm using guile-1.6.0 and srfi-19 for date stuff.
It seems that the week-numbers are not calculated correctly, as it
returns week 0 for all dates between 2002-1-1 and 2002-1-12. All other
weeknumbers after the 12th are 2 off.

This is the stuff to reproduce it:

guile> (date-week-number (string->date "2002-1-23" "~Y-~m-~d") 1)
guile> (date-week-number (string->date "2002-1-16" "~Y-~m-~d") 1)
guile> (date-week-number (string->date "2002-1-9" "~Y-~m-~d") 1) 
guile> (date-week-number (string->date "2002-1-3" "~Y-~m-~d") 1)

The problem occurs because of:
guile> (priv:days-before-first-week (string->date "2002-1-1" "~Y-~m-~d")  1)

In the epxression: 
(define (date-week-number date day-of-week-starting-week)
  (quotient (- (date-year-day date)
               (priv:days-before-first-week  date day-of-week-starting-week))

http://www.cl.cam.ac.uk/~mgk25/iso-time.html (ISO 8601) says:
 In commercial and industrial applications (delivery times, production
 plans, etc.), especially in Europe, it is often required to refer to a
 week of a year. Week 01 of a year is per definition the first week
 that has the Thursday in this year, which is equivalent to the week
 that contains the fourth day of January. In other words, the first
 week of a new year is the week that has the majority of its days in
 the new year. Week 01 might also contain days from the previous year
 and the week before week 01 of a year is the last week (52 or 53) of
 the previous year even if it contains days from the new year. A week
 starts with Monday (day 1) and ends with Sunday (day 7). For example,
 the first week of the year 1997 lasts from 1996-12-30 to 1997-01-05
 and can be written in standard notation as................

I've seen this solution:
  If you know the date, how do you calculate the corresponding week
  number (as defined in ISO-8601)?

  1) Using the formulas in section 2.15.1, calculate the Julian Day
     Number, J.

  2) Perform the following calculations (in which the divisions are
     integer divisions in which the remainder is discarded):

     d4 = (J+31741 - (J mod 7)) mod 146097 mod 36524 mod 1461
     L  = d4/1460
     d1 = ((d4-L) mod 365) + L
     WeekNumber = d1/7+1 
in http://www.pauahtun.org/CalendarFAQ/cal/calendar24.txt.
This leads to the implementation:

(define (mydatetoweeknumber dt)
  (let* ((J (inexact->exact (date->julian-day dt)))
         (d4 (modulo
              (modulo (modulo
                    (- (+ J 31741) (modulo J 7)) 146097) 36524) 1461))
         (L (inexact->exact (/ d4 1460)))
         (d1 (+ (modulo (- d4 L) 365) L)))
         (+ (inexact->exact (/ d1 7)) 1)  

Is my solution acceptable? If not, is there anyone who can implement a
better solution?

Joost Helberg
Joost Helberg
Technisch Directeur Snow BV http://snow.nl Tel 0418-653333 Fax 0418-653666
Voorzitter VOSN         http://www.vosn.nl Tel 0418-653336 Fax 0418-653666
GPG PblKey fprnt= 738C 20AC A545 02AE 6F5D  5A9F D724 EB4B 2B10 150B

reply via email to

[Prev in Thread] Current Thread [Next in Thread]