diff -r 0e56d79fa2ab Lib/_strptime.py --- a/Lib/_strptime.py Tue May 24 18:29:46 2011 +0200 +++ b/Lib/_strptime.py Wed May 25 14:23:15 2011 -0500 @@ -201,6 +201,8 @@ 'S': r"(?P6[0-1]|[0-5]\d|\d)", 'U': r"(?P5[0-3]|[0-4]\d|\d)", 'w': r"(?P[0-6])", + 'u': r"(?P[1-7])", + 'V': r"(?P5[0-3]|0[1-9]|[1-4]\d|\d)", # W is set below by using 'U' 'y': r"(?P\d\d)", #XXX: Does 'Y' need to worry about having less or more than @@ -294,6 +296,22 @@ days_to_week = week_0_length + (7 * (week_of_year - 1)) return 1 + days_to_week + day_of_week +def _calc_julian_from_V(year, iso_week, iso_weekday): + """Calculate the Julian day based on the ISO 8601 year, week, and weekday. + ISO weeks start on Mondays, with week 01 being the week containing 4 Jan. + ISO week days range from 1 (Monday) to 7 (Sunday). + + """ + correction = datetime_date(year,1,4).isoweekday() + 3 + ordinal = (iso_week * 7) + iso_weekday - correction + print(ordinal) + # ordinal may be negative or 0 now, which means the date is in the previous + # calendar year + if ordinal < 1: + ordinal += dateteime_date(year,1,1).toordinal() - datetime_date(year-1,1,1) + return year - 1, ordinal + else: + return year, ordinal def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"): """Return a 2-tuple consisting of a time struct and an int containing @@ -348,6 +366,8 @@ # though week_of_year = -1 week_of_year_start = -1 + iso_weekday = -1 + iso_weekday = -1 # weekday and julian defaulted to -1 so as to signal need to calculate # values weekday = julian = -1 @@ -414,6 +434,14 @@ weekday = 6 else: weekday -= 1 + iso_weekday = weekday + 1 + elif group_key == 'u': + iso_weekday = int(found_dict['u']) + weekday = iso_weekday % 7 + if weekday == 0: + weekday = 6 + else: + weekday -=1 elif group_key == 'j': julian = int(found_dict['j']) elif group_key in ('U', 'W'): @@ -424,6 +452,8 @@ else: # W starts week on Monday. week_of_year_start = 0 + elif group_key == 'V': + iso_week = int(found_dict['V']) elif group_key == 'z': z = found_dict['z'] tzoffset = int(z[1:3]) * 60 + int(z[3:5]) @@ -450,6 +480,8 @@ week_starts_Mon = True if week_of_year_start == 0 else False julian = _calc_julian_from_U_or_W(year, week_of_year, weekday, week_starts_Mon) + elif julian == -1 and iso_week != -1 and iso_weekday != -1: + year, julian = _calc_julian_from_V(year, iso_week, iso_weekday) # Cannot pre-calculate datetime_date() since can change in Julian # calculation and thus could have different value for the day of the week # calculation.