[Python-checkins] python/dist/src/Lib _strptime.py,1.12,1.13

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Sat, 08 Mar 2003 23:44:44 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv22888

Modified Files:
	_strptime.py 
Log Message:
SF patch #691928: Use datetime in _strptime
Contributed by Brett Cannon.

To prevent code duplication, I patched _strptime to use datetime's date 
object to do Julian day, Gregorian, and day of the week calculations.

Patch also includes new regression tests to test results and the
calculation gets triggered.

Very minor comment changes and the contact email are also changed. 



Index: _strptime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/_strptime.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** _strptime.py	29 Jan 2003 03:49:43 -0000	1.12
--- _strptime.py	9 Mar 2003 07:44:41 -0000	1.13
***************
*** 7,22 ****
  
  FUNCTIONS:
!     firstjulian -- Calculates the Julian date up to the first of the specified
!                     year
!     gregorian -- Calculates the Gregorian date based on the Julian day and
!                     year
!     julianday -- Calculates the Julian day since the first of the year based
!                     on the Gregorian date
!     dayofweek -- Calculates the day of the week from the Gregorian date.
      strptime -- Calculates the time struct represented by the passed-in string
  
! Requires Python 2.2.1 or higher.
  Can be used in Python 2.2 if the following line is added:
!     >>> True = 1; False = 0
  """
  import time
--- 7,16 ----
  
  FUNCTIONS:
!     _getlang -- Figure out what language is being used for the locale
      strptime -- Calculates the time struct represented by the passed-in string
  
! Requires Python 2.2.1 or higher (mainly because of the use of property()).
  Can be used in Python 2.2 if the following line is added:
!     True = 1; False = 0
  """
  import time
***************
*** 25,36 ****
  from re import compile as re_compile
  from re import IGNORECASE
  
  __author__ = "Brett Cannon"
! __email__ = "drifty@bigfoot.com"
  
  __all__ = ['strptime']
  
- RegexpType = type(re_compile(''))
- 
  def _getlang():
      # Figure out what the current language is set to.
--- 19,29 ----
  from re import compile as re_compile
  from re import IGNORECASE
+ from datetime import date as datetime_date
  
  __author__ = "Brett Cannon"
! __email__ = "brett@python.org"
  
  __all__ = ['strptime']
  
  def _getlang():
      # Figure out what the current language is set to.
***************
*** 426,430 ****
      hour = minute = second = 0
      tz = -1
!     # Defaulted to -1 so as to signal using functions to calc values
      weekday = julian = -1
      found_dict = found.groupdict()
--- 419,423 ----
      hour = minute = second = 0
      tz = -1
!     # weekday and julian defaulted to -1 so as to signal need to calculate values
      weekday = julian = -1
      found_dict = found.groupdict()
***************
*** 496,509 ****
              elif locale_time.timezone[2].lower() == found_zone:
                  tz = -1
!     #XXX <bc>: If calculating fxns are never exposed to the general
!     #populous then just inline calculations.  Also might be able to use
!     #``datetime`` and the methods it provides.
      if julian == -1:
!         julian = julianday(year, month, day)
!     else:  # Assuming that if they bothered to include Julian day it will
             #be accurate
!         year, month, day = gregorian(julian, year)
      if weekday == -1:
!         weekday = dayofweek(year, month, day)
      return time.struct_time((year, month, day,
                               hour, minute, second,
--- 489,507 ----
              elif locale_time.timezone[2].lower() == found_zone:
                  tz = -1
!     # Cannot pre-calculate datetime_date() since can change in Julian
!     #calculation and thus could have different value for the day of the week
!     #calculation
      if julian == -1:
!         # Need to add 1 to result since first day of the year is 1, not 0.
!         julian = datetime_date(year, month, day).toordinal() - \
!                   datetime_date(year, 1, 1).toordinal() + 1
!     else:  # Assume that if they bothered to include Julian day it will
             #be accurate
!         datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
!         year = datetime_result.year
!         month = datetime_result.month
!         day = datetime_result.day
      if weekday == -1:
!         weekday = datetime_date(year, month, day).weekday()
      return time.struct_time((year, month, day,
                               hour, minute, second,
***************
*** 523,560 ****
          raise ValueError("value not in list")
  
- def firstjulian(year):
-     """Calculate the Julian date up until the first of the year."""
-     return ((146097 * (year + 4799)) // 400) - 31738
- 
- def julianday(year, month, day):
-     """Calculate the Julian day since the beginning of the year.
-     Calculated from the Gregorian date.
-     """
-     a = (14 - month) // 12
-     return (day - 32045
-             + (((153 * (month + (12 * a) - 3)) + 2) // 5)
-             + ((146097 * (year + 4800 - a)) // 400)) - firstjulian(year) + 1
- 
- def gregorian(julian, year):
-     """Return 3-item list containing Gregorian date based on the Julian day."""
-     a = 32043 + julian + firstjulian(year)
-     b = ((4 * a) + 3) // 146097
-     c = a - ((146097 * b) // 4)
-     d = ((4 * c) + 3) // 1461
-     e = c - ((1461 * d) // 4)
-     m = ((5 * e) + 2) // 153
-     day = 1 + e - (((153 * m) + 2) // 5)
-     month = m + 3 - (12 * (m // 10))
-     year = (100 * b) + d - 4800 + (m // 10)
-     return [year, month, day]
- 
- def dayofweek(year, month, day):
-     """Calculate the day of the week (Monday is 0)."""
-     a = (14 - month) // 12
-     y = year - a
-     weekday = (day + y + ((97 * y) // 400)
-                + ((31 * (month + (12 * a) -2 )) // 12)) % 7
-     if weekday == 0:
-         return 6
-     else:
-         return weekday-1
--- 521,522 ----