[Python-checkins] python/nondist/sandbox/datetime obj_datetimetz.c,1.19,1.20 test_both.py,1.84,1.85
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
Sat, 14 Dec 2002 11:11:54 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv11636
Modified Files:
obj_datetimetz.c test_both.py
Log Message:
datetimetz inherits utcfromtimestamp() too. Again this doesn't accept a
tzinfo arg. Added test. Removed more unused code.
Index: obj_datetimetz.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetimetz.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** obj_datetimetz.c 14 Dec 2002 19:02:02 -0000 1.19
--- obj_datetimetz.c 14 Dec 2002 19:11:50 -0000 1.20
***************
*** 73,143 ****
}
- /* Internal helper.
- * Build datetime from a time_t and a distinct count of microseconds.
- * Pass localtime or gmtime for f, to control the interpretation of timet.
- */
- static PyObject *
- datetimetz_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us)
- {
- struct tm *tm;
- PyObject *result = NULL;
-
- tm = f(&timet);
- if (tm)
- result = PyObject_CallFunction(cls, "iiiiiii",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- us);
- else
- PyErr_SetString(PyExc_ValueError,
- "timestamp out of range for "
- "platform localtime()/gmtime() function");
- return result;
- }
-
- /* Internal helper.
- * Build datetime from a Python timestamp. Pass localtime or gmtime for f,
- * to control the interpretation of the timestamp. Since a double doesn't
- * have enough bits to cover a datetime's full range of precision, it's
- * better to call datetimetz_from_timet_and_us provided you have a way
- * to get that much precision (e.g., C time() isn't good enough).
- */
- static PyObject *
- datetimetz_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp)
- {
- time_t timet = (time_t)timestamp;
- int us = (int)((timestamp - (double)timet) * 1e6);
-
- return datetimetz_from_timet_and_us(cls, f, timet, us);
- }
-
- /* Return new local datetime from timestamp (Python timestamp -- a double). */
- static PyObject *
- datetimetz_fromtimestamp(PyObject *cls, PyObject *args)
- {
- double timestamp;
- PyObject *result = NULL;
-
- if (PyArg_ParseTuple(args, "d:fromtimestamp", ×tamp))
- result = datetimetz_from_timestamp(cls, localtime, timestamp);
- return result;
- }
-
- /* Return new UTC datetime from timestamp (Python timestamp -- a double). */
- static PyObject *
- datetimetz_utcfromtimestamp(PyObject *cls, PyObject *args)
- {
- double timestamp;
- PyObject *result = NULL;
-
- if (PyArg_ParseTuple(args, "d:utcfromtimestamp", ×tamp))
- result = datetimetz_from_timestamp(cls, gmtime, timestamp);
- return result;
- }
-
/* Return best possible local time -- this isn't constrained by the
* precision of a timestamp.
--- 73,76 ----
***************
*** 161,165 ****
}
! /* Note: utcnow() is inherited, and doesn't accept tzinfo. */
--- 94,100 ----
}
! /* Note: utcnow() is inherited, and doesn't accept tzinfo.
! * Ditto utcfromtimestamp().
! */
***************
*** 469,473 ****
static PyMethodDef datetimetz_methods[] = {
/* Class methods: */
! /* Inherited: combine(), utcnow(). */
{"now", (PyCFunction)datetimetz_now,
--- 404,408 ----
static PyMethodDef datetimetz_methods[] = {
/* Class methods: */
! /* Inherited: combine(), utcnow(), utcfromtimestamp() */
{"now", (PyCFunction)datetimetz_now,
***************
*** 475,479 ****
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.
--- 410,414 ----
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.
***************
*** 482,490 ****
METH_VARARGS | METH_CLASS,
PyDoc_STR("timestamp -> local datetime from a POSIX timestamp "
- "(like time.time()).")},
-
- {"utcfromtimestamp", (PyCFunction)datetimetz_utcfromtimestamp,
- METH_VARARGS | METH_CLASS,
- PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
"(like time.time()).")},
*/
--- 417,420 ----
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.84
retrieving revision 1.85
diff -C2 -d -r1.84 -r1.85
*** test_both.py 14 Dec 2002 19:02:03 -0000 1.84
--- test_both.py 14 Dec 2002 19:11:51 -0000 1.85
***************
*** 1944,1973 ****
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_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():
--- 1944,1985 ----
def test_tzinfo_now(self):
! meth = self.theclass.now
# Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
! base = meth()
# Try with and without naming the keyword.
off42 = FixedOffset(42, "42")
! another = meth(off42)
! again = meth(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, 16)
! self.assertRaises(TypeError, meth, tzinfo=16)
# Bad keyword name.
! self.assertRaises(TypeError, meth, tinfo=off42)
# Too many args.
! self.assertRaises(TypeError, meth, off42, off42)
def test_tzinfo_utcnow(self):
! meth = self.theclass.utcnow
# Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
! base = meth()
# Try with and without naming the keyword; for whatever reason,
# utcnow() doesn't accept a tzinfo argument.
off42 = FixedOffset(42, "42")
! self.assertRaises(TypeError, meth, off42)
! self.assertRaises(TypeError, meth, tzinfo=off42)
!
! def test_tzinfo_utcfromtimestamp(self):
! import time
! meth = self.theclass.utcfromtimestamp
! 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; for whatever reason,
! # utcfromtimestamp() doesn't accept a tzinfo argument.
! off42 = FixedOffset(42, "42")
! self.assertRaises(TypeError, meth, ts, off42)
! self.assertRaises(TypeError, meth, ts, tzinfo=off42)
def test_suite():