Missing microseconds in datetime objects [with patch]

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Missing microseconds in datetime objects [with patch]

Derek Payton
Hey all,

The company I worked for needed subsecond accuracy on datetime objects, but microseconds were getting lost in translation. To demonstrate, I used the CherryPy echo server and the following client:

----- Begin Client -----
#!/usr/bin/env python
from datetime import datetime
from pyamf.remoting.client import RemotingService

gw = RemotingService('http://localhost:8080/gateway/')
service = gw.getService('myservice')

dt = datetime.now()
print 'in:', dt
print 'out:', service.echo(dt)
----- End Client -----

$ python test-client.py
in:  2009-03-06 18:46:36.913483
out: 2009-03-06 18:46:36

Turns out, timetuples don't hold microseconds and we need to add them manually:

----- Begin diff -----
Index: util/__init__.py
===================================================================
--- util/__init__.py    (revision 2229)
+++ util/__init__.py    (working copy)
@@ -634,7 +634,8 @@
     if isinstance(d, datetime.date) and not isinstance(d, datetime.datetime):
         d = datetime.datetime.combine(d, datetime.time(0, 0, 0, 0))
 
-    return calendar.timegm(d.utctimetuple())
+    msec = str(d.microsecond).rjust(6, '0')
+    return float('%s.%s' % (calendar.timegm(d.utctimetuple()), msec))
 
 def get_datetime(secs):
     """
----- End diff -----

$ python test-client.py
in:  2009-03-06 18:48:33.054154
out: 2009-03-06 18:48:33.054154

I patched this internally months ago, sorry for only just now getting it out to the list.

--Derek

_______________________________________________
PyAMF dev mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Missing microseconds in datetime objects [with patch]

Thijs Triemstra
Thanks for the report and patch Derek, I verified the fix and opened a ticket for it:


Cheers,

Thijs

On 7 Mar 2009, at 03:32, Derek Payton wrote:

Hey all,

The company I worked for needed subsecond accuracy on datetime objects, but microseconds were getting lost in translation. To demonstrate, I used the CherryPy echo server and the following client:

----- Begin Client -----
#!/usr/bin/env python
from datetime import datetime
from pyamf.remoting.client import RemotingService

gw = RemotingService('http://localhost:8080/gateway/')
service = gw.getService('myservice')

dt = datetime.now()
print 'in:', dt
print 'out:', service.echo(dt)
----- End Client -----

$ python test-client.py
in:  2009-03-06 18:46:36.913483
out: 2009-03-06 18:46:36

Turns out, timetuples don't hold microseconds and we need to add them manually:

----- Begin diff -----
Index: util/__init__.py
===================================================================
--- util/__init__.py    (revision 2229)
+++ util/__init__.py    (working copy)
@@ -634,7 +634,8 @@
     if isinstance(d, datetime.date) and not isinstance(d, datetime.datetime):
         d = datetime.datetime.combine(d, datetime.time(0, 0, 0, 0))
 
-    return calendar.timegm(d.utctimetuple())
+    msec = str(d.microsecond).rjust(6, '0')
+    return float('%s.%s' % (calendar.timegm(d.utctimetuple()), msec))
 
 def get_datetime(secs):
     """
----- End diff -----

$ python test-client.py
in:  2009-03-06 18:48:33.054154
out: 2009-03-06 18:48:33.054154

I patched this internally months ago, sorry for only just now getting it out to the list.

--Derek
_______________________________________________
PyAMF dev mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/dev


_______________________________________________
PyAMF dev mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/dev

PGP.sig (201 bytes) Download Attachment
Loading...