[Zope-dev] PythonScript for iso_week
Steve Alexander
steve@cat-box.net
Wed, 14 Mar 2001 12:32:49 +0000
Hi folks,
At various times in the past, people on this list have asked about
getting week numbers from DateTime instances.
I recently had this requirement, so I wrote a PythonScript to produce an
iso_week tuple (year, week_number, dow_monday_is_one__sunday_is_seven)
from a DateTime instance.
I've submitted the same to the collector as a feature-enhancement patch
to DateTime.py.
I've tested this by empirically comparing it with the output of
mxDateTime's iso_week attribute for all days from 1920-01-01 to some
date in the twenty-third century.
Note also that this iso_week method does not create additional DateTime
instances.
See also http://www.cl.cam.ac.uk/~mgk25/iso-time.html
--------
PythonScript iso_week
Parameters: 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
http://www.cat-box.net