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

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sat, 14 Dec 2002 10:48:57 -0800


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

Modified Files:
	obj_datetimetz.c test_both.py 
Log Message:
Implemented datetimetz.now with its optional tzinfo arg.  Added a test.


Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** obj_datetimetz.c	14 Dec 2002 18:04:12 -0000	1.17
--- obj_datetimetz.c	14 Dec 2002 18:48:54 -0000	1.18
***************
*** 21,26 ****
--- 21,42 ----
  /*
   * Constructors.
+  * These the datetime methods of the same names, but allow an optional
+  * tzinfo argument.
   */
  
+ /* Internal helper.
+  * self is a datetimetz.  Replace its tzinfo member.
+  */
+ void
+ replace_tzinfo(PyObject *self, PyObject *newtzinfo)
+ {
+ 	assert(self != NULL);
+ 	assert(newtzinfo != NULL);
+ 	assert(PyDateTimeTZ_Check(self));
+ 	Py_INCREF(newtzinfo);
+ 	Py_DECREF(((PyDateTime_DateTimeTZ *)self)->tzinfo);
+ 	((PyDateTime_DateTimeTZ *)self)->tzinfo = newtzinfo;
+ }
+ 
  static PyObject *
  datetimetz_new(PyTypeObject *type, PyObject *args, PyObject *kw)
***************
*** 170,176 ****
   */
  static PyObject *
! datetimetz_now(PyObject *cls, PyObject *dummy)
  {
! 	return datetimetz_best_possible(cls, localtime);
  }
  
--- 186,204 ----
   */
  static PyObject *
! datetimetz_now(PyObject *cls, PyObject *args, PyObject *kw)
  {
! 	PyObject *self = NULL;
! 	PyObject *tzinfo = Py_None;
! 	static char *keywords[] = {"tzinfo", NULL};
! 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords,
! 					&tzinfo)) {
! 		if (check_tzinfo_subclass(tzinfo, "tzinfo argument") < 0)
! 			return NULL;
! 		self = datetime_best_possible(cls, localtime);
! 		if (self != NULL)
! 			replace_tzinfo(self, tzinfo);
! 	}
! 	return self;
  }
  
***************
*** 491,501 ****
  	/* Inherited: combine(). */
  
! 	/* XXX Inherited: now(), utcnow(), fromtimestamp(), utcfromtimestamp().
  	   XXX But they shouldn't be:  these take a frickin' optional tzinfo
  	   XXX argument in the datetimetz flavors.
- 
- 	{"now",         (PyCFunction)datetimetz_now,
- 	 METH_NOARGS | METH_CLASS,
- 	 PyDoc_STR("Return a new datetime representing local day and time.")},
  
  	{"utcnow",         (PyCFunction)datetimetz_utcnow,
--- 519,529 ----
  	/* Inherited: combine(). */
  
! 	{"now",         (PyCFunction)datetimetz_now,
! 	 METH_KEYWORDS | METH_CLASS,
! 	 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,

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.82
retrieving revision 1.83
diff -C2 -d -r1.82 -r1.83
*** test_both.py	14 Dec 2002 16:50:16 -0000	1.82
--- test_both.py	14 Dec 2002 18:48:55 -0000	1.83
***************
*** 1943,1946 ****
--- 1943,1965 ----
                                    timedelta(minutes=2*1439))
  
+     def test_tzinfo_now(self):
+         now = self.theclass.now
+         # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
+         base = now()
+         # Try with and without naming the keyword.
+         off42 = FixedOffset(42, "42")
+         another = now(off42)
+         again = now(tzinfo=off42)
+         self.failUnless(another.tzinfo is again.tzinfo)
+         self.assertEqual(another.utcoffset(), 42)
+         # Bad argument with and w/o naming the keyword.
+         self.assertRaises(TypeError, now, 16)
+         self.assertRaises(TypeError, now, tzinfo=16)
+         # Bad keyword name.
+         self.assertRaises(TypeError, now, tinfo=off42)
+         # Too many args.
+         self.assertRaises(TypeError, now, off42, off42)
+ 
+ 
  def test_suite():
      allsuites = [unittest.makeSuite(klass, 'test')