[pylucene-dev] Date objects

Andi Vajda vajda at osafoundation.org
Fri Sep 8 14:19:13 PDT 2006


On Fri, 8 Sep 2006, Ken Kinder wrote:

> Is there a standard and reliable way of converting a PyLucene Date
> object into a python standard library date object?

Indeed there is with the attached path fixing the bug of using PyLong_FromLong 
instead of PyLong_FromLongLong for long long values in getTime() which was 
truncating the returned values. This patch is also checked in.

>>> from PyLucene import Date
>>> from datetime import datetime
>>> d = Date()
>>> d
<Date: Fri Sep 08 14:01:51 PDT 2006>
>>> dt = datetime.utcfromtimestamp(d.getTime() / 1000.0)
>>> str(dt)
>>> '2006-09-08 21:01:51.753000'   # a GMT value: 14:01 PDT is 21:01 GMT

If you want to express the python datetime in a specific timezone as well, you 
can use datetime.fromtimestamp(d.getTime() / 1000.0, tzinfo) as documented 
here: http://www.python.org/doc/2.4.3/lib/datetime-datetime.html#l2h-1887

I hope this helps

Andi..
-------------- next part --------------
Index: java.cpp
===================================================================
--- java.cpp	(revision 277)
+++ java.cpp	(working copy)
@@ -3167,7 +3167,7 @@
     long long time;
 
     OBJ_CALL(time = self->object->getTime());
-    return PyLong_FromLong(time);
+    return PyLong_FromLongLong(time);
 }
 
 static PyObject *j_date_setTime(j_date *self, PyObject *arg)
@@ -3255,7 +3255,7 @@
     long long time;
 
     OBJ_CALL(time = self->object->getTimeInMillis());
-    return PyLong_FromLong(time);
+    return PyLong_FromLongLong(time);
 }
 
 static PyObject *j_calendar_setTimeInMillis(j_calendar *self, PyObject *arg)


More information about the pylucene-dev mailing list