[Zope-dev] iso_week for Zope's DateTime

Steve Alexander steve@cat-box.net
Sun, 18 Mar 2001 18:24:11 +0000


The Collector doesn't seem to be working at the moment, so I'm posting 
this here.

I've written a small test for my iso_week function.

You can download it, plus some test data, plus the script to create the 
test data here:

   http://www.cat-box.net/steve/zope_iso_week.tar
   (700k)

You'll need to install mxDateTime if you want to make your own test 
data. You'll need bzip2 if you want to use the supplied test data.

The function passes the test for all dates from 1600-01-01 to 2585-08-23.

I've submitted it (with the date argument renamed self) to the Collector 
as a feature patch to DateTime.py. if you want to keep the standard 
DateTime.py, you can use it as PythonScript in Zope easily enough.

----
def iso_week(date):
   doy=date.dayOfYear()
   dow=(date.dow()-1)%7
   y=date.year()
   thurs_this_week=doy+(3-dow)
   is_leap=y%4==0 and (y%100!=0 or y%400==0)

   if thurs_this_week>(is_leap and 366 or 365):
     week=1
     if dow<8:
       y=y+1
   else:
     mon_this_week=doy-dow
     day_year_begins=(dow-doy+1)%7
     day_of_4_jan=(day_year_begins+3)%7
     monday_of_first_week=4-day_of_4_jan
     week=(mon_this_week-monday_of_first_week)/7+1
     if week==0:
       y=y-1
       is_last_leap=y%4==0 and (y%100!=0 or y%400==0)
       if day_year_begins==4 or (day_year_begins==5 and is_last_leap):
         week=53
       else:
         week=52

   return y,week,dow+1
----

--
Steve Alexander
Software Engineer
Cat-Box limited