[Python-checkins] python/dist/src/Lib _strptime.py, 1.23.4.5, 1.23.4.6

bcannon at users.sourceforge.net bcannon at users.sourceforge.net
Mon Oct 18 03:56:18 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27014/Lib

Modified Files:
      Tag: release23-maint
	_strptime.py 
Log Message:
Add support for using %U and %W in strptime when year and day of the week are
also specified.

Closes bug #1045381.


Index: _strptime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/_strptime.py,v
retrieving revision 1.23.4.5
retrieving revision 1.23.4.6
diff -u -d -r1.23.4.5 -r1.23.4.6
--- _strptime.py	6 Oct 2004 02:16:45 -0000	1.23.4.5
+++ _strptime.py	18 Oct 2004 01:56:16 -0000	1.23.4.6
@@ -311,7 +311,7 @@
             # W is set below by using 'U'
             'y': r"(?P<y>\d\d)",
             'Y': r"(?P<Y>\d\d\d\d)"})
-        base.__setitem__('W', base.__getitem__('U'))
+        base.__setitem__('W', base.__getitem__('U').replace('U', 'W'))
         if locale_time:
             self.locale_time = locale_time
         else:
@@ -431,6 +431,8 @@
     month = day = 1
     hour = minute = second = 0
     tz = -1
+    week_of_year = -1
+    week_of_year_start = -1
     # weekday and julian defaulted to -1 so as to signal need to calculate values
     weekday = julian = -1
     found_dict = found.groupdict()
@@ -490,6 +492,14 @@
                 weekday -= 1
         elif group_key == 'j':
             julian = int(found_dict['j'])
+        elif group_key in ('U', 'W'):
+            week_of_year = int(found_dict[group_key])
+            if group_key == 'U':
+                # U starts week on Sunday
+                week_of_year_start = 6
+            else:
+                # W starts week on Monday
+                week_of_year_start = 0
         elif group_key == 'Z':
             # Since -1 is default value only need to worry about setting tz if
             # it can be something other than -1.
@@ -505,7 +515,32 @@
             elif time.daylight and \
                 locale_time.timezone[3].lower() == found_zone:
                 tz = 1
-
+    # If we know the week of the year and what day of that week, we can figure
+    # out the Julian day of the year
+    # Calculations below assume 0 is a Monday
+    if julian == -1 and week_of_year != -1 and weekday != -1 and year != -1:
+        # Adjust for U directive so that calculations are not dependent on
+        # directive used to figure out week of year
+        if weekday == 6 and week_of_year_start == 6:
+            week_of_year -= 1
+        # For some reason when Dec 31 falls on a Monday the week of the year is
+        # off by a week; verified on both OS X and Solaris.
+        elif weekday == 0 and week_of_year_start == 6 and week_of_year >= 52:
+            week_of_year += 1
+        # Calculate how many days in week 0
+        first_weekday = datetime_date(year, 1, 1).weekday()
+        preceeding_days = 7 - first_weekday
+        if preceeding_days == 7:
+            preceeding_days = 0
+        # If in week 0, then just figure out how many days from Jan 1 to day of
+        # week specified, else calculate by multiplying week of year by 7,
+        # adding in days in week 0, and the number of days from Monday to the
+        # day of the week
+        if not week_of_year:
+            julian = 1 + weekday - first_weekday
+        else:
+            days_to_week = preceeding_days + (7 * (week_of_year - 1))
+            julian = 1 + days_to_week + 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



More information about the Python-checkins mailing list