[Python-checkins] python/nondist/sandbox/datetime obj_datetimetz.c,1.18,1.19 test_both.py,1.83,1.84

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sat, 14 Dec 2002 11:02:08 -0800


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

Modified Files:
	obj_datetimetz.c test_both.py 
Log Message:
Made datetimetz inherit datetime's utcnow() class method.  Unlike now(),
this does not accept a tzinfo argument.  It's hard to know whether that
was intended, since there weren't any relevant docs or tests; it's simply
what the Python implementation did.

Added a test for this.

Removed a pile of unused code (& there's more to come).


Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** obj_datetimetz.c	14 Dec 2002 18:48:54 -0000	1.18
--- obj_datetimetz.c	14 Dec 2002 19:02:02 -0000	1.19
***************
*** 73,79 ****
  }
  
- /* TM_FUNC is the shared type of localtime() and gmtime(). */
- typedef struct tm *(*TM_FUNC)(const time_t *timer);
- 
  /* Internal helper.
   * Build datetime from a time_t and a distinct count of microseconds.
--- 73,76 ----
***************
*** 119,161 ****
  }
  
- /* Internal helper.
-  * Build most accurate possible datetime for current time.  Pass localtime or
-  * gmtime for f as appropriate.
-  */
- static PyObject *
- datetimetz_best_possible(PyObject *cls, TM_FUNC f)
- {
- #ifdef HAVE_GETTIMEOFDAY
- 	struct timeval t;
- 
- #ifdef GETTIMEOFDAY_NO_TZ
- 	gettimeofday(&t);
- #else
- 	gettimeofday(&t, (struct timezone *)NULL);
- #endif
- 	return datetimetz_from_timet_and_us(cls, f, t.tv_sec, t.tv_usec);
- 
- #else	/* ! HAVE_GETTIMEOFDAY */
- 	/* No flavor of gettimeofday exists on this platform.  Python's
- 	 * time.time() does a lot of other platform tricks to get the
- 	 * best time it can on the platform, and we're not going to do
- 	 * better than that (if we could, the better code would belong
- 	 * in time.time()!)  We're limited by the precision of a double,
- 	 * though.
- 	 */
- 	PyObject *time;
- 	double dtime;
- 
- 	time = time_time();
-     	if (time == NULL)
-     		return NULL;
- 	dtime = PyFloat_AsDouble(time);
- 	Py_DECREF(time);
- 	if (dtime == -1.0 && PyErr_Occurred())
- 		return NULL;
- 	return datetimetz_from_timestamp(cls, f, dtime);
- #endif	/* ! HAVE_GETTIMEOFDAY */
- }
- 
  /* Return new local datetime from timestamp (Python timestamp -- a double). */
  static PyObject *
--- 116,119 ----
***************
*** 203,214 ****
  }
  
! /* Return best possible UTC time -- this isn't constrained by the
!  * precision of a timestamp.
!  */
! static PyObject *
! datetimetz_utcnow(PyObject *cls, PyObject *dummy)
! {
! 	return datetimetz_best_possible(cls, gmtime);
! }
  
  /*
--- 161,166 ----
  }
  
! /* Note:  utcnow() is inherited, and doesn't accept tzinfo. */
! 
  
  /*
***************
*** 517,521 ****
  static PyMethodDef datetimetz_methods[] = {
  	/* Class methods: */
! 	/* Inherited: combine(). */
  
  	{"now",         (PyCFunction)datetimetz_now,
--- 469,473 ----
  static PyMethodDef datetimetz_methods[] = {
  	/* Class methods: */
! 	/* Inherited: combine(), utcnow(). */
  
  	{"now",         (PyCFunction)datetimetz_now,
***************
*** 523,533 ****
  	 PyDoc_STR("[tzinfo] -> new datetimetz with local day and time.")},
  
! 	/* XXX Inherited: utcnow(), fromtimestamp(), utcfromtimestamp().
  	   XXX But they shouldn't be:  these take a frickin' optional tzinfo
  	   XXX argument in the datetimetz flavors.
- 
- 	{"utcnow",         (PyCFunction)datetimetz_utcnow,
- 	 METH_NOARGS | METH_CLASS,
- 	 PyDoc_STR("Return a new datetime representing UTC day and time.")},
  
  	{"fromtimestamp", (PyCFunction)datetimetz_fromtimestamp,
--- 475,481 ----
  	 PyDoc_STR("[tzinfo] -> new datetimetz with local day and time.")},
  
! 	/* XXX Inherited: fromtimestamp(), utcfromtimestamp().
  	   XXX But they shouldn't be:  these take a frickin' optional tzinfo
  	   XXX argument in the datetimetz flavors.
  
  	{"fromtimestamp", (PyCFunction)datetimetz_fromtimestamp,

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.83
retrieving revision 1.84
diff -C2 -d -r1.83 -r1.84
*** test_both.py	14 Dec 2002 18:48:55 -0000	1.83
--- test_both.py	14 Dec 2002 19:02:03 -0000	1.84
***************
*** 1961,1964 ****
--- 1961,1973 ----
          self.assertRaises(TypeError, now, off42, off42)
  
+     def test_tzinfo_utcnow(self):
+         utcnow = self.theclass.utcnow
+         # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
+         base = utcnow()
+         # Try with and without naming the keyword; for whatever reason,
+         # utcnow() doesn't accept a tzinfo argument.
+         off42 = FixedOffset(42, "42")
+         self.assertRaises(TypeError, utcnow, off42)
+         self.assertRaises(TypeError, utcnow, tzinfo=off42)
  
  def test_suite():