[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.49,1.50 datetime.py,1.84,1.85 doc.txt,1.34,1.35 obj_date.c,1.42,1.43 obj_datetime.c,1.41,1.42 test_both.py,1.55,1.56

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Mon, 09 Dec 2002 08:37:21 -0800


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

Modified Files:
	datetime.c datetime.py doc.txt obj_date.c obj_datetime.c 
	test_both.py 
Log Message:
Closed open issue:  timetuple() methods now return a time.struct_time
instead of a 9-tuple.


Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.49
retrieving revision 1.50
diff -C2 -d -r1.49 -r1.50
*** datetime.c	9 Dec 2002 01:51:10 -0000	1.49
--- datetime.c	9 Dec 2002 16:37:15 -0000	1.50
***************
*** 142,159 ****
  };
  
- /* Call time.time() and return its result (a Python float). */
- static PyObject *
- time_time()
- {
- 	PyObject *result = NULL;
- 	PyObject *time = PyImport_ImportModule("time");
- 
- 	if (time != NULL) {
- 		result = PyObject_CallMethod(time, "time", "()");
- 		Py_DECREF(time);
- 	}
- 	return result;
- }
- 
  /* year -> 1 if leap year, else 0. */
  static int
--- 142,145 ----
***************
*** 362,367 ****
  	PyObject *result;
  
- 	assert(PyTuple_Size(tuple) == 9);
- 
  	time = PyImport_ImportModule("time");
  	if (time == NULL)
--- 348,351 ----
***************
*** 508,511 ****
--- 492,534 ----
  	assert(*m > 0);
  	assert(*d > 0);
+ }
+ 
+ /* Wrap functions from the time module.  These aren't directly available
+  * from C.  Perhaps they should be.
+  */
+ 
+ /* Call time.time() and return its result (a Python float). */
+ static PyObject *
+ time_time()
+ {
+ 	PyObject *result = NULL;
+ 	PyObject *time = PyImport_ImportModule("time");
+ 
+ 	if (time != NULL) {
+ 		result = PyObject_CallMethod(time, "time", "()");
+ 		Py_DECREF(time);
+ 	}
+ 	return result;
+ }
+ 
+ /* Build a time.struct_time.  The DST flag is always -1. */
+ static PyObject *
+ build_struct_time(int y, int m, int d, int hh, int mm, int ss)
+ {
+ 	PyObject *time;
+ 	PyObject *result = NULL;
+ 
+ 	time = PyImport_ImportModule("time");
+ 	if (time != NULL) {
+ 		result = PyObject_CallMethod(time, "struct_time",
+ 					     "((iiiiiiiii))",
+ 					     y, m, d,
+ 					     hh, mm, ss,
+ 				 	     weekday(y, m, d),
+ 				 	     days_before_month(y, m) + d,
+ 				 	     -1);
+ 		Py_DECREF(time);
+ 	}
+ 	return result;
  }
  

Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.84
retrieving revision 1.85
diff -C2 -d -r1.84 -r1.85
*** datetime.py	9 Dec 2002 02:20:34 -0000	1.84
--- datetime.py	9 Dec 2002 16:37:16 -0000	1.85
***************
*** 150,153 ****
--- 150,157 ----
  _DAYNAMES = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  
+ 
+ def _build_struct_time(y, m, d, hh, mm, ss, wday, dnum, dstflag):
+     return _time.struct_time((y, m, d, hh, mm, ss, wday, dnum, dstflag))
+ 
  # This is a start at a struct tm workalike.  Goals:
  #
***************
*** 579,584 ****
      def timetuple(self):
          "Return local time tuple compatible with time.localtime()."
!         return (self.__year, self.__month, self.__day,
!                 0, 0, 0, self.weekday(), self._yday(), -1)
  
      def toordinal(self):
--- 583,589 ----
      def timetuple(self):
          "Return local time tuple compatible with time.localtime()."
!         return _build_struct_time(self.__year, self.__month, self.__day,
!                                   0, 0, 0,
!                                   self.weekday(), self._yday(), -1)
  
      def toordinal(self):
***************
*** 1175,1181 ****
      def timetuple(self):
          "Return local time tuple compatible with time.localtime()."
!         return (self.__year, self.__month, self.__day,
!                 self.__hour, self.__minute, self.__second,
!                 self.weekday(), self._yday(), -1)
  
      def date(self):
--- 1180,1186 ----
      def timetuple(self):
          "Return local time tuple compatible with time.localtime()."
!         return _build_struct_time(self.__year, self.__month, self.__day,
!                                   self.__hour, self.__minute, self.__second,
!                                   self.weekday(), self._yday(), -1)
  
      def date(self):

Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -d -r1.34 -r1.35
*** doc.txt	9 Dec 2002 15:42:41 -0000	1.34
--- doc.txt	9 Dec 2002 16:37:17 -0000	1.35
***************
*** 34,40 ****
  - The type objects aren't exposed, due to headaches with DLL/.so.
  
- - timetuple() should probably return a struct_time tuple+struct combo,
-   like time.localtime() returns.  Is the type exposed from C?
- 
  - LaTeXize the docs.
  
--- 34,37 ----

Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -C2 -d -r1.42 -r1.43
*** obj_date.c	9 Dec 2002 02:20:34 -0000	1.42
--- obj_date.c	9 Dec 2002 16:37:17 -0000	1.43
***************
*** 303,307 ****
  	if (tuple == NULL)
  		return NULL;
- 	assert(PyTuple_Size(tuple) == 9);
  	result = format_strftime(format, tuple);
  	Py_DECREF(tuple);
--- 303,306 ----
***************
*** 366,379 ****
  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);
  }
  
--- 365,372 ----
  date_timetuple(PyDateTime_Date *self)
  {
! 	return build_struct_time(GET_YEAR(self),
! 				 GET_MONTH(self),
! 				 GET_DAY(self),
! 				 0, 0, 0);
  }
  

Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** obj_datetime.c	9 Dec 2002 02:20:34 -0000	1.41
--- obj_datetime.c	9 Dec 2002 16:37:17 -0000	1.42
***************
*** 512,527 ****
  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,
! 			     DATE_GET_HOUR(self),
! 			     DATE_GET_MINUTE(self),
! 			     DATE_GET_SECOND(self),
! 			     weekday(year, month, day),
! 			     days_before_month(year, month) + day,
! 			     -1);
  }
  
--- 512,521 ----
  datetime_timetuple(PyDateTime_DateTime *self)
  {
! 	return build_struct_time(GET_YEAR(self),
! 				 GET_MONTH(self),
! 				 GET_DAY(self),
! 				 DATE_GET_HOUR(self),
! 				 DATE_GET_MINUTE(self),
! 				 DATE_GET_SECOND(self));
  }
  

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.55
retrieving revision 1.56
diff -C2 -d -r1.55 -r1.56
*** test_both.py	8 Dec 2002 19:21:32 -0000	1.55
--- test_both.py	9 Dec 2002 16:37:18 -0000	1.56
***************
*** 726,729 ****
--- 726,738 ----
              t = d.timetuple()
              self.assertEqual(t, (1956, 3, 1+i, 0, 0, 0, (3+i)%7, 61+i, -1))
+             self.assertEqual(t.tm_year, 1956)
+             self.assertEqual(t.tm_mon, 3)
+             self.assertEqual(t.tm_mday, 1+i)
+             self.assertEqual(t.tm_hour, 0)
+             self.assertEqual(t.tm_min, 0)
+             self.assertEqual(t.tm_sec, 0)
+             self.assertEqual(t.tm_wday, (3+i)%7)
+             self.assertEqual(t.tm_yday, 61+i)
+             self.assertEqual(t.tm_isdst, -1)
  
      def test_pickling(self):
***************
*** 1119,1122 ****
--- 1128,1142 ----
                            t.toordinal() - date(t.year, 1, 1).toordinal() + 1,
                            -1))
+         tt = t.timetuple()
+         self.assertEqual(tt.tm_year, t.year)
+         self.assertEqual(tt.tm_mon, t.month)
+         self.assertEqual(tt.tm_mday, t.day)
+         self.assertEqual(tt.tm_hour, t.hour)
+         self.assertEqual(tt.tm_min, t.minute)
+         self.assertEqual(tt.tm_sec, t.second)
+         self.assertEqual(tt.tm_wday, t.weekday())
+         self.assertEqual(tt.tm_yday, t.toordinal() -
+                                      date(t.year, 1, 1).toordinal() + 1)
+         self.assertEqual(tt.tm_isdst, -1)
  
      def test_more_strftime(self):