A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-checkins/2002-March/024708.html below:

[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.15,1.16 test_datetime.py,1.9,1.10

[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.15,1.16 test_datetime.py,1.9,1.10Tim Peters tim_one@users.sourceforge.net
Sat, 02 Mar 2002 18:10:26 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory usw-pr-cvs1:/tmp/cvs-serv23159

Modified Files:
	datetime.py test_datetime.py 
Log Message:
Broke dependence of _num2date and _date2num on datetime objects, and
renamed to _ord2ymd and _ymd2ord.  Proper testing of these functions
requires exercising negative ordinals and years, and using datetime
objects blocked that.

Added a datetime.fromordinal() constructor, and a toordinal() method.
"Something like that" is needed for people who want to build other
calendar systems on top of this basic one.

Changed test_ordinal_conversions() to do exhaustive testing on both
sides of the origin, and every 7th year across the datetime range.
This is less testing than it was doing before, but the test runs much
faster now than can be accounted for by that alone.  I suspect it's
avoiding object hair that makes the difference (the test uses _ord2ymd
and _ymd2ord directly now, never constructing a datetime object).


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** datetime.py	2 Mar 2002 23:33:38 -0000	1.15
--- datetime.py	3 Mar 2002 02:10:24 -0000	1.16
***************
*** 52,68 ****
  def _days_before_month(month, year):
      "month, year -> number of days in year preceeding first day of month."
      return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year))
  
! def _date2num(date):
!     "datetime object -> day ordinal, considering 01-Jan-0001 as day 1."
!     assert isinstance(date, datetime)
!     return (_days_before_year(date.year) +
!             _days_before_month(date.month, date.year) +
!             date.day)
  
  _DI400Y = _days_before_year(401)    # number of days in 400 years
  
! def _num2date(n):
!     "day ordinal -> datetime object."
  
      n400 = (n-1) // _DI400Y  # number of 400-year blocks preceding
--- 52,74 ----
  def _days_before_month(month, year):
      "month, year -> number of days in year preceeding first day of month."
+     if not 1 <= month <= 12:
+         raise ValueError('month must be in 1..12', month)
      return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year))
  
! def _ymd2ord(year, month, day):
!     "year, month, day -> ordinal, considering 01-Jan-0001 as day 1."
!     if not 1 <= month <= 12:
!         raise ValueError('month must be in 1..12', month)
!     dim = _days_in_month(month, year)
!     if not 1 <= day <= dim:
!         raise ValueError('day must be in 1..%d' % dim, day)
!     return (_days_before_year(year) +
!             _days_before_month(month, year) +
!             day)
  
  _DI400Y = _days_before_year(401)    # number of days in 400 years
  
! def _ord2ymd(n):
!     "ordinal -> (year, month, day), considering 01-Jan-0001 as day 1."
  
      n400 = (n-1) // _DI400Y  # number of 400-year blocks preceding
***************
*** 85,89 ****
          dbm -= _days_in_month(month, year)
      assert n > dbm
!     return datetime(year, month, n-dbm)
  
  class basetime(object):
--- 91,95 ----
          dbm -= _days_in_month(month, year)
      assert n > dbm
!     return year, month, n-dbm
  
  class basetime(object):
***************
*** 239,242 ****
--- 245,249 ----
      now(), utcnow()
      fromtimestamp(), utcfromtimestamp()
+     fromordinal()
  
      Operators:
***************
*** 251,254 ****
--- 258,262 ----
      ctime(), utcctime()
      strftime(), utcstrftime()
+     toordinal()
  
      Properties (readonly):
***************
*** 342,345 ****
--- 350,363 ----
      utcnow = classmethod(utcnow)
  
+     def fromordinal(cls, n):
+         """Contruct a datetime from a proleptic Gregorian ordinal.
+ 
+         January 1 of year 1 is day 1.  Only the year, month and day are
+         non-zero in the result.
+         """
+         y, m, d = _ord2ymd(n)
+         return cls(y, m, d)
+     fromordinal = classmethod(fromordinal)
+ 
      # Conversions to string
  
***************
*** 399,402 ****
--- 417,428 ----
          return _time.gmtime(self._mktime())
  
+     def toordinal(self):
+         """Return proleptic Gregorian ordinal for the year, month and day.
+ 
+         January 1 of year 1 is day 1.  Only the year, month and day values
+         contribute to the result.
+         """
+         return _ymd2ord(self.year, self.month, self.day)
+ 
      def __cmp__(self, other):
          "Three-way comparison."
***************
*** 491,495 ****
              carry_hh, mm = divmod(mm+carry_mm, 60)
              days, hh = divmod(hh+carry_hh, 24)
!             result = _num2date(days + other.days + _date2num(self))
              result.__hour = hh
              result.__minute = mm
--- 517,521 ----
              carry_hh, mm = divmod(mm+carry_mm, 60)
              days, hh = divmod(hh+carry_hh, 24)
!             result = datetime.fromordinal(days + other.days + self.toordinal())
              result.__hour = hh
              result.__minute = mm
***************
*** 507,512 ****
              return self + -other
          if isinstance(other, datetime):
!             days1 = _date2num(self)
!             days2 = _date2num(other)
              secs1 = (self.__second +
                       (self.__minute - self.__tzoffset) * 60 +
--- 533,538 ----
              return self + -other
          if isinstance(other, datetime):
!             days1 = self.toordinal()
!             days2 = other.toordinal()
              secs1 = (self.__second +
                       (self.__minute - self.__tzoffset) * 60 +

Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_datetime.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** test_datetime.py	2 Mar 2002 22:33:46 -0000	1.9
--- test_datetime.py	3 Mar 2002 02:10:24 -0000	1.10
***************
*** 28,54 ****
  
      def test_ordinal_conversions(self):
!         from datetime import _date2num, _num2date, MINYEAR, MAXYEAR
! 
!         # Verify 1 Jan 1 has ordinal 1.
!         one = datetime.new(1, 1, 1)
!         self.assertEqual(_date2num(one), 1)
!         self.assertEqual(_num2date(1), one)
  
!         # The first example in "Calendrical Calculations".
!         d = datetime.new(1945, 11, 12)
!         self.assertEqual(_date2num(d), 710347)
!         self.assertEqual(_num2date(710347), d)
  
!         # XXX To speed up this loop, use range(MINYEAR, MAXYEAR + 1, 11)
!         for year in range(MINYEAR, MAXYEAR + 1):
!             base = datetime(year, 1, 1)
!             ordinal = _date2num(base)
!             derived = _num2date(ordinal)
!             self.assertEqual(base, derived)
!             if year > MINYEAR:
!                 # Verify that moving back a day gets to the end of year-1.
!                 lastyear = datetime(year-1, 12, 31)
!                 derived = _num2date(ordinal - 1)
!                 self.assertEqual(lastyear, derived)
  
      def test_bad_constructor_arguments(self):
--- 28,52 ----
  
      def test_ordinal_conversions(self):
!         from datetime import _ymd2ord, _ord2ymd, MINYEAR, MAXYEAR
  
!         # Check some fixed values.
!         for y, m, d, n in [(1, 1, 1, 1),      # calendar origin
!                            (0, 12, 31, 0),
!                            (0, 12, 30, -1),
!                            # first example from "Calendrical Calculations"
!                            (1945, 11, 12, 710347)]:
!             self.assertEqual(n, _ymd2ord(y, m, d))
!             self.assertEqual((y, m, d), _ord2ymd(n))
  
!         # Check first and last days of year exhaustively across 2000 years
!         # centered at the origin, and spottily over the whole range of
!         # years datetime objects support.
!         for year in range(-1001, 1002) + range(MINYEAR, MAXYEAR+1, 7):
!             # Verify (year, 1, 1) -> ordinal -> y, m, d is identity.
!             n = _ymd2ord(year, 1, 1)
!             self.assertEqual((year, 1, 1), _ord2ymd(n))
!             # Verify that moving back a day gets to the end of year-1.
!             self.assertEqual((year-1, 12, 31), _ord2ymd(n-1))
!             self.assertEqual(_ymd2ord(year-1, 12, 31), n-1)
  
      def test_bad_constructor_arguments(self):




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4