Could not find the standard library, so I had to flip it. The main difficulty lies in finding the first day of the year ISO (which may be last year). You need to add some input checks, etc.
import datetime def isoWeekToGregorian(isoYear, isoWeek, isoDayOfWeek):
We can create a simple test:
#TEST: we know 2004 has 53 weeks... t0 = datetime.datetime(2004,12,26,0,0,0) t1 = datetime.datetime(2005,1,10,0,0,0) ndays = (t1-t0).days + 1 for i in range(ndays): d0 = t0 + datetime.timedelta(i) d0iso = d0.isocalendar() if (d0 != isoWeekToGregorian(d0iso[0],d0iso[1],d0iso[2])): print "failed: %s" % d0 else: print d0, d0iso
What prints correctly:
2004-12-26 00:00:00 (2004, 52, 7) 2004-12-27 00:00:00 (2004, 53, 1) 2004-12-28 00:00:00 (2004, 53, 2) 2004-12-29 00:00:00 (2004, 53, 3) 2004-12-30 00:00:00 (2004, 53, 4) 2004-12-31 00:00:00 (2004, 53, 5) 2005-01-01 00:00:00 (2004, 53, 6) 2005-01-02 00:00:00 (2004, 53, 7) 2005-01-03 00:00:00 (2005, 1, 1) 2005-01-04 00:00:00 (2005, 1, 2) 2005-01-05 00:00:00 (2005, 1, 3) 2005-01-06 00:00:00 (2005, 1, 4) 2005-01-07 00:00:00 (2005, 1, 5) 2005-01-08 00:00:00 (2005, 1, 6) 2005-01-09 00:00:00 (2005, 1, 7) 2005-01-10 00:00:00 (2005, 2, 1)