[Python-checkins] python/nondist/sandbox/datetime obj_datetimetz.c,1.20,1.21 test_both.py,1.85,1.86

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2002年12月14日 16:42:10 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv32612
Modified Files:
	obj_datetimetz.c test_both.py 
Log Message:
Gave datetimetz fromtimestamp(), w/ optional tzinfo arg. Added test.
Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** obj_datetimetz.c	14 Dec 2002 19:11:50 -0000	1.20
--- obj_datetimetz.c	15 Dec 2002 00:42:04 -0000	1.21
***************
*** 21,26 ****
 /*
 * Constructors.
! * These the datetime methods of the same names, but allow an optional
! * tzinfo argument.
 */
 
--- 21,26 ----
 /*
 * Constructors.
! * These are like the datetime methods of the same names, but allow an
! * optional tzinfo argument.
 */
 
***************
*** 94,99 ****
 }
 
 /* Note: utcnow() is inherited, and doesn't accept tzinfo.
! * Ditto utcfromtimestamp().
 */
 
--- 94,119 ----
 }
 
+ /* Return new local datetime from timestamp (Python timestamp -- a double). */
+ static PyObject *
+ datetimetz_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
+ {
+ 	PyObject *self = NULL;
+ 	double timestamp;
+ 	PyObject *tzinfo = Py_None;
+ 	static char *keywords[] = {"timestamp", "tzinfo", NULL};
+ 
+ 	if (PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp",
+ 					keywords, &timestamp, &tzinfo)) {
+ 		if (check_tzinfo_subclass(tzinfo, "tzinfo argument") < 0)
+ 			return NULL;
+ 		self = datetime_from_timestamp(cls, localtime, timestamp);
+ 		if (self != NULL)
+ 			replace_tzinfo(self, tzinfo);
+ 	}
+ 	return self;
+ }
+ 
 /* Note: utcnow() is inherited, and doesn't accept tzinfo.
! * Ditto utcfromtimestamp(). Ditto combine().
 */
 
***************
*** 410,422 ****
 	 PyDoc_STR("[tzinfo] -> new datetimetz with local day and time.")},
 
- 	/* XXX Inherited: fromtimestamp().
- 	 XXX But they shouldn't be: these take a frickin' optional tzinfo
- 	 XXX argument in the datetimetz flavors.
- 
 	{"fromtimestamp", (PyCFunction)datetimetz_fromtimestamp,
! 	 METH_VARARGS | METH_CLASS,
! 	 PyDoc_STR("timestamp -> local datetime from a POSIX timestamp "
! 	 	 "(like time.time()).")},
! 	*/
 
 	/* Instance methods: */
--- 430,436 ----
 	 PyDoc_STR("[tzinfo] -> new datetimetz with local day and time.")},
 
 	{"fromtimestamp", (PyCFunction)datetimetz_fromtimestamp,
! 	 METH_KEYWORDS | METH_CLASS,
! 	 PyDoc_STR("timestamp[, tzinfo] -> local time from POSIX timestamp.")},
 
 	/* Instance methods: */
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.85
retrieving revision 1.86
diff -C2 -d -r1.85 -r1.86
*** test_both.py	14 Dec 2002 19:11:51 -0000	1.85
--- test_both.py	15 Dec 2002 00:42:05 -0000	1.86
***************
*** 1961,1964 ****
--- 1961,1986 ----
 self.assertRaises(TypeError, meth, off42, off42)
 
+ def test_tzinfo_fromtimestamp(self):
+ import time
+ meth = self.theclass.fromtimestamp
+ ts = time.time()
+ # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
+ base = meth(ts)
+ # Try with and without naming the keyword.
+ off42 = FixedOffset(42, "42")
+ another = meth(ts, off42)
+ again = meth(ts, 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, meth, ts, 16)
+ self.assertRaises(TypeError, meth, ts, tzinfo=16)
+ # Bad keyword name.
+ self.assertRaises(TypeError, meth, ts, tinfo=off42)
+ # Too many args.
+ self.assertRaises(TypeError, meth, ts, off42, off42)
+ # Too few args.
+ self.assertRaises(TypeError, meth)
+ 
 def test_tzinfo_utcnow(self):
 meth = self.theclass.utcnow

AltStyle によって変換されたページ (->オリジナル) /