[Python-checkins] python/nondist/sandbox/datetime doc.txt,1.18,1.19 obj_date.c,1.30,1.31 obj_datetime.c,1.25,1.26 test_both.py,1.43,1.44

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Thu, 05 Dec 2002 21:13:43 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv1161

Modified Files:
	doc.txt obj_date.c obj_datetime.c test_both.py 
Log Message:
Gave datetime a correct timetuple implementation.  The tests didn't catch
that it was still using the date timetuple() (which zeroes out hours,
minutes, and seconds), so beefed up the test of that too.

Changed date.strftime() to call the correct timetuple() method for the
object passed to it.  Added a new test to ensure that strftime is seeing
all the fields it should see for a datetime.strftime().

Updated the datetime docs, to near completion.  There are still a couple
methods that aren't right.


Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** doc.txt	6 Dec 2002 02:42:06 -0000	1.18
--- doc.txt	6 Dec 2002 05:13:40 -0000	1.19
***************
*** 311,315 ****
    - strftime(format)
      Return a string representing the date, controlled by an explicit
!     format string.  d.strftime(f) is the same as
      time.strftime(f, d.timetuple()).
  
--- 311,316 ----
    - strftime(format)
      Return a string representing the date, controlled by an explicit
!     format string.  Formats referring to hours, minutes or seconds will
!     see 0 values.  d.strftime(f) is the same as
      time.strftime(f, d.timetuple()).
  
***************
*** 438,452 ****
      - pickling
  
- XXX NOTHING BELOW THIS POINT HAS BEEN CHECKED, AND MUCH IS CERTAINLY
- XXX WRONG (CUT & PASTE ERRORS MOSTLY) AND/OR MISIMPLEMENTED.
- 
  Instance methods:
  
    - timetuple()
      Return a 9-element tuple of the form returned by time.localtime().
!     The hours, minutes and seconds are 0, and the DST flag is -1.
!     d.timetuple() is equivalent to
          (d.year, d.month, d.day,
!          0, 0, 0,  # h, m, s
           d.weekday(),  # 0 is Monday
           d.toordinal() - date(d.year, 1, 1).toordinal() + 1, # day of year
--- 439,449 ----
      - pickling
  
  Instance methods:
  
    - timetuple()
      Return a 9-element tuple of the form returned by time.localtime().
!     The DST flag is -1.   d.timetuple() is equivalent to
          (d.year, d.month, d.day,
!          d.hour, d.minute.d.second,
           d.weekday(),  # 0 is Monday
           d.toordinal() - date(d.year, 1, 1).toordinal() + 1, # day of year
***************
*** 455,464 ****
    - toordinal()
      Return the proleptic Gregorian ordinal of the date, where January 1
!     of year 1 has ordinal 1.  For any date object d,
!     date.fromordinal(d.toordinal()) == d.
  
    - weekday()
      Return the day of the week as an integer, where Monday is 0 and
!     Sunday is 6.  For example, date(2002, 12, 4).weekday() == 2, a
      Wednesday.
      See also isoweekday().
--- 452,460 ----
    - toordinal()
      Return the proleptic Gregorian ordinal of the date, where January 1
!     of year 1 has ordinal 1.
  
    - weekday()
      Return the day of the week as an integer, where Monday is 0 and
!     Sunday is 6.  For example, datetime(2002, 12, 4).weekday() == 2, a
      Wednesday.
      See also isoweekday().
***************
*** 466,470 ****
    - isoweekday()
      Return the day of the week as an integer, where Monday is 1 and
!     Sunday is 7.  For example, date(2002, 12, 4).isoweekday() == 3, a
      Wednesday.
      See also weekday() and isocalendar().
--- 462,466 ----
    - isoweekday()
      Return the day of the week as an integer, where Monday is 1 and
!     Sunday is 7.  For example, datetime(2002, 12, 4).isoweekday() == 3, a
      Wednesday.
      See also weekday() and isocalendar().
***************
*** 487,493 ****
      2004, so that
  
!     date(2003, 12, 29).isocalendar() == (2004, 1, 1)
!     date(2004, 1, 4).isocalendar() == (2004, 1, 7)
  
    - isoformat()
      Return a string representing the date in ISO 8601 format,
--- 483,490 ----
      2004, so that
  
!     datetime(2003, 12, 29).isocalendar() == (2004, 1, 1)
!     datetime(2004, 1, 4).isocalendar() == (2004, 1, 7)
  
+   XXX isoformat() needs work.
    - isoformat()
      Return a string representing the date in ISO 8601 format,
***************
*** 496,499 ****
--- 493,497 ----
      str(d) is equivalent to d.isoformat().
  
+   XXX ctime() needs work.
    - ctime()
      Return a string representing the date, for example
***************
*** 502,507 ****
  
    - strftime(format)
!     Return a string representing the date, controlled by an explicit
!     format string.  d.strftime(f) is the same as
      time.strftime(f, d.timetuple()).
  
--- 500,505 ----
  
    - strftime(format)
!     Return a string representing the date and time, controlled by an
!     explicit format string.  d.strftime(f) is the same as
      time.strftime(f, d.timetuple()).
  

Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -C2 -d -r1.30 -r1.31
*** obj_date.c	6 Dec 2002 04:17:45 -0000	1.30
--- obj_date.c	6 Dec 2002 05:13:40 -0000	1.31
***************
*** 136,155 ****
  date_timetuple(PyDateTime_Date *self)
  {
- 	PyObject *result;
  	const int year = GET_YEAR(self);
  	const int month = GET_MONTH(self);
  	const int day = GET_DAY(self);
  
! 	result = Py_BuildValue("iiiiiiiii",
! 				year,
! 				month,
! 				day,
! 				0,
! 				0,
! 				0,
! 				weekday(year, month, day),
! 				days_before_month(year, month) + day,
! 				-1);
! 	return result;
  }
  
--- 136,149 ----
  date_timetuple(PyDateTime_Date *self)
  {
  	const int year = GET_YEAR(self);
  	const int month = GET_MONTH(self);
  	const int day = GET_DAY(self);
  
! 	return Py_BuildValue("iiiiiiiii",
! 			     year, month, day,
! 			     0, 0, 0,
! 			     weekday(year, month, day),
! 			     days_before_month(year, month) + day,
! 			     -1);
  }
  
***************
*** 394,402 ****
  date_strftime(PyDateTime_Date *self, PyObject *format)
  {
  	PyObject *result;
! 	PyObject *tuple = date_timetuple(self);
  
  	if (tuple == NULL)
  		return NULL;
  	result = format_strftime(format, tuple);
  	Py_DECREF(tuple);
--- 388,400 ----
  date_strftime(PyDateTime_Date *self, PyObject *format)
  {
+ 	/* This method can be inherited, and needs to call the
+ 	 * timetuple() method appropriate to self's class.
+ 	 */
  	PyObject *result;
! 	PyObject *tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()");
  
  	if (tuple == NULL)
  		return NULL;
+ 	assert(PyTuple_Size(tuple) == 9);
  	result = format_strftime(format, tuple);
  	Py_DECREF(tuple);

Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -d -r1.25 -r1.26
*** obj_datetime.c	6 Dec 2002 04:17:48 -0000	1.25
--- obj_datetime.c	6 Dec 2002 05:13:40 -0000	1.26
***************
*** 466,469 ****
--- 466,486 ----
  }
  
+ static PyObject *
+ datetime_timetuple(PyDateTime_DateTime *self)
+ {
+ 	const int year = GET_YEAR(self);
+ 	const int month = GET_MONTH(self);
+ 	const int day = GET_DAY(self);
+ 
+ 	return Py_BuildValue("iiiiiiiii",
+ 			     year, month, day,
+ 			     GET_HOUR(self),
+ 			     GET_MINUTE(self),
+ 			     GET_SECOND(self),
+ 			     weekday(year, month, day),
+ 			     days_before_month(year, month) + day,
+ 			     -1);
+ }
+ 
  /* Pickle support.  Quite a maze! */
  
***************
*** 544,547 ****
--- 561,567 ----
  
  	/* Instance methods: */
+ 	{"timetuple",   (PyCFunction)datetime_timetuple, METH_NOARGS,
+          "Return time tuple, compatible with time.localtime()."},
+ 
  	{"ctime",       (PyCFunction)datetime_ctime,	METH_NOARGS,
  	 "Return ctime() style string."},

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.43
retrieving revision 1.44
diff -C2 -d -r1.43 -r1.44
*** test_both.py	6 Dec 2002 02:43:02 -0000	1.43
--- test_both.py	6 Dec 2002 05:13:40 -0000	1.44
***************
*** 13,16 ****
--- 13,20 ----
  # work under both implementations will be *moved* from test_datetime and
  # test_cdatetime into this file.
+ # Later:  test_cdatetime.py no longer exists.  test_datetime.py still
+ # contains tests of things that exist in the Python implementation but
+ # not yet in the C implementation.  I don't think anyone was within a
+ # factor of 20 of guessing how many of those there were!
  
  import sys
***************
*** 1066,1070 ****
          import time
  
!         # Call it a succes if utcnow() and utcfromtimestamp() are within
          # a second of each other.
          tolerance = timedelta(seconds=1)
--- 1070,1074 ----
          import time
  
!         # Call it a success if utcnow() and utcfromtimestamp() are within
          # a second of each other.
          tolerance = timedelta(seconds=1)
***************
*** 1076,1079 ****
--- 1080,1100 ----
              # Else try again a few times.
          self.failUnless(abs(from_timestamp - from_now) <= tolerance)
+ 
+     def test_more_timetuple(self):
+         # This tests fields beyond those tested by the TestDate.test_timetuple.
+         t = self.theclass(2004, 12, 31, 6, 22, 33)
+         self.assertEqual(t.timetuple(), (2004, 12, 31, 6, 22, 33, 4, 366, -1))
+         self.assertEqual(t.timetuple(),
+                          (t.year, t.month, t.day,
+                           t.hour, t.minute, t.second,
+                           t.weekday(),
+                           t.toordinal() - date(t.year, 1, 1).toordinal() + 1,
+                           -1))
+ 
+     def test_more_strftime(self):
+         # This tests fields beyond those tested by the TestDate.test_strftime.
+         t = self.theclass(2004, 12, 31, 6, 22, 33)
+         self.assertEqual(t.strftime("%m %d %y %S %M %H %j"),
+                                     "12 31 04 33 22 06 366")
  
  def test_suite():