[Python-checkins] python/nondist/sandbox/datetime doc.txt,1.17,1.18 obj_datetime.c,1.22,1.23 test_both.py,1.41,1.42
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
Thu, 05 Dec 2002 18:42:09 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv25138
Modified Files:
doc.txt obj_datetime.c test_both.py
Log Message:
Implemented timedelta.utc_now(), and wrote a test, docs, etc.
Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** doc.txt 6 Dec 2002 02:17:42 -0000 1.17
--- doc.txt 6 Dec 2002 02:42:06 -0000 1.18
***************
*** 346,350 ****
Return the current local datetime. This is equivalent to
datetime.fromtimestamp(time.time()).
! See also now().
- now()
--- 346,350 ----
Return the current local datetime. This is equivalent to
datetime.fromtimestamp(time.time()).
! See also now(), fromtimestamp().
- now()
***************
*** 353,358 ****
possible, supplies more precision than can be gotten from going
through a time.time() timestamp.
! See also today().
! XXX It currently doesn't.
- fromtimestamp(timestamp)
--- 353,364 ----
possible, supplies more precision than can be gotten from going
through a time.time() timestamp.
! XXX It currently doesn't. To the contrary, it's currently worse.
! See also today(), utcnow().
!
! - utcnow()
!
! Return the current UTC datetime. This is like now(), but returns
! the current UTC date and time.
! See also now().
- fromtimestamp(timestamp)
***************
*** 379,385 ****
unless 1 <= ordinal <= datetime.max.toordinal(). The hour, minute,
second and microsecond of the result are all 0.
-
- XXX THERE ARE A SLEW OF OTHER CONSTRUCTORS IN THE PYTHON IMPLEMENTATION
- XXX THAT DON'T EXIST YET IN THE C IMPLEMENTATION.
Class attributes:
--- 385,388 ----
Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** obj_datetime.c 6 Dec 2002 02:17:42 -0000 1.22
--- obj_datetime.c 6 Dec 2002 02:42:06 -0000 1.23
***************
*** 198,201 ****
--- 198,219 ----
}
+ static PyObject *
+ datetime_utcnow(PyObject *self, PyObject *cls)
+ {
+ /* XXX Like datetime_now, this would like to do better than
+ * XXX 1-second resolution.
+ */
+ struct tm *tm;
+ time_t timet;
+
+ time(&timet);
+ tm = gmtime(&timet);
+
+ return PyObject_CallFunction(cls, "llllll",
+ tm->tm_year + 1900, tm->tm_mon + 1,
+ tm->tm_mday, tm->tm_hour, tm->tm_min,
+ tm->tm_sec);
+ }
+
/* 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
***************
*** 541,557 ****
static PyMethodDef datetime_methods[] = {
/* Class methods: */
! {"now", (PyCFunction)datetime_now, METH_O | METH_CLASS,
! "Return a new datetime that represents the current time."},
! {"today", (PyCFunction)datetime_today, METH_O | METH_CLASS,
! "Return a new datetime that represents the current date."},
! {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, METH_VARARGS |
! METH_CLASS,
"timestamp -> local datetime from a POSIX timestamp "
"(like time.time())."},
{"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
! METH_VARARGS | METH_CLASS,
"timestamp -> UTC datetime from a POSIX timestamp "
"(like time.time())."},
--- 559,581 ----
static PyMethodDef datetime_methods[] = {
/* Class methods: */
! {"now", (PyCFunction)datetime_now,
! METH_O | METH_CLASS,
! "Return a new datetime representing local day and time."},
! {"utcnow", (PyCFunction)datetime_utcnow,
! 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,
! METH_VARARGS | METH_CLASS,
"timestamp -> local datetime from a POSIX timestamp "
"(like time.time())."},
{"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
! METH_VARARGS | METH_CLASS,
"timestamp -> UTC datetime from a POSIX timestamp "
"(like time.time())."},
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** test_both.py 6 Dec 2002 02:17:42 -0000 1.41
--- test_both.py 6 Dec 2002 02:42:06 -0000 1.42
***************
*** 1063,1066 ****
--- 1063,1079 ----
self.verify_field_equality(expected, got)
+ def test_utcnow(self):
+ import time
+
+ # Call it a succes if utcnow*( and utcfromtimestamp() are within
+ # a second of each other.
+ tolerance = timedelta(seconds=1)
+ for dummy in range(3):
+ from_now = self.theclass.utcnow()
+ from_timestamp = self.theclass.utcfromtimestamp(time.time())
+ if abs(from_timestamp - from_now) <= tolerance:
+ break
+ # Else try again a few times.
+ self.failUnless(abs(from_timestamp - from_now) <= tolerance)
def test_suite():