[Python-checkins] python/nondist/sandbox/datetime obj_date.c,1.29,1.30 obj_datetime.c,1.24,1.25

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Thu, 05 Dec 2002 20:17:50 -0800


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

Modified Files:
	obj_date.c obj_datetime.c 
Log Message:
Got rid of the C function datetime_today.  date_today now passes on the
value of time.time() to the correct fromtimestamp class method.  This is
less efficient, and much less efficient for date.today(), but I don't
care -- it's the right way to do it, and should work smoothly with
subclasses without more hacks.


Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** obj_date.c	5 Dec 2002 21:29:36 -0000	1.29
--- obj_date.c	6 Dec 2002 04:17:45 -0000	1.30
***************
*** 224,235 ****
  }
  
! /* Return new date from current time. */
  static PyObject *
  date_today(PyObject *self, PyObject *cls)
  {
! 	time_t timet;
  
! 	time(&timet);
! 	return date_local_from_time_t(cls, timet);
  }
  
--- 224,263 ----
  }
  
! /* Return new date from current time.
!  * We say this is equivalent to fromtimestamp(time.time()), and the
!  * only way to be sure of that is to *call* time.time().  That's not
!  * generally the same as calling C's time.
!  * XXX Expose the time module's C code in a saner way.
!  */
  static PyObject *
  date_today(PyObject *self, PyObject *cls)
  {
! 	PyObject *time;		/* the time module, and later time.time() */
! 	PyObject *time_time;	/* time.time */
! 	PyObject *result;
  
! 	time = PyImport_ImportModule("time");
! 	if (time == NULL)
! 		return NULL;
! 
! 	time_time = PyObject_GetAttrString(time, "time");
! 	Py_DECREF(time);
! 	if (time_time == NULL)
! 		return NULL;
! 
!    	time = PyObject_CallObject(time_time, NULL);
!     	Py_DECREF(time_time);
!     	if (time == NULL)
!     		return NULL;
! 
! 	/* Note well:  today() is a class method, so this may not call
! 	 * date.fromtimestamp.  For example, it may call
! 	 * datetime.fromtimestamp.  That's why we need all the accuracy
! 	 * time.time() delivers; if someone were gonzo about optimization,
! 	 * date.today() could get away with plain C time().
! 	 */
! 	result = PyObject_CallMethod(cls, "fromtimestamp", "O", time);
! 	Py_DECREF(time);
! 	return result;
  }
  
***************
*** 457,461 ****
  
  	{"today",         (PyCFunction)date_today,	METH_O | METH_CLASS,
! 	 "Construct local date corresponing to current day."},
  
  	/* Instance methods: */
--- 485,490 ----
  
  	{"today",         (PyCFunction)date_today,	METH_O | METH_CLASS,
! 	 "Current date or datetime:  same as "
! 	 "self.__class__.fromtimestamp(time.time())."},
  
  	/* Instance methods: */

Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** obj_datetime.c	6 Dec 2002 02:49:20 -0000	1.24
--- obj_datetime.c	6 Dec 2002 04:17:48 -0000	1.25
***************
*** 216,252 ****
  }
  
- /* Well, we say this is equivalent to fromtimestamp(time.time()), and the
-  * only way to be sure of that is to *call* time.time().  That's not
-  * generally the same as calling C's time.
-  * XXX Expose the time module's C code in a saner way.
-  */
- static PyObject *
- datetime_today(PyObject *self, PyObject *cls)
- {
- 	PyObject *time;
- 	PyObject *time_time;
- 	double timestamp;
- 
- 	time = PyImport_ImportModule("time");
- 	if (time == NULL)
- 		return NULL;
- 
- 	time_time = PyObject_GetAttrString(time, "time");
- 	Py_DECREF(time);
- 	if (time_time == NULL)
- 		return NULL;
- 
-    	time = PyObject_CallObject(time_time, NULL);
-     	Py_DECREF(time_time);
-     	if (time == NULL)
-     		return NULL;
- 
- 	timestamp = PyFloat_AsDouble(time);
- 	Py_DECREF(time);
- 	if (timestamp == -1.0 && PyErr_Occurred())
- 		return NULL;
-     	return datetime_from_timestamp(cls, localtime, timestamp);
- }
- 
  /* datetime arithmetic. */
  
--- 216,219 ----
***************
*** 565,572 ****
  	 METH_O | METH_CLASS,
  	 "Return a new datetime representing UTC day and time."},
- 
- 	{"today",         (PyCFunction)datetime_today,
- 	 METH_O | METH_CLASS,
- 	 "Return a new datetime representing local day and time."},
  
  	{"fromtimestamp", (PyCFunction)datetime_fromtimestamp,
--- 532,535 ----