summaryrefslogtreecommitdiff
path: root/python/qpid
diff options
context:
space:
mode:
authorRafael H. Schloming <rhs@apache.org>2008-11-10 15:19:29 +0000
committerRafael H. Schloming <rhs@apache.org>2008-11-10 15:19:29 +0000
commit8be83d96ee667728d8a5da0db2e68e29c2177dda (patch)
tree8adb650a08d1eb5912970aa3e5142c4c74292abf /python/qpid
parent36a1702b48b62a9df223c1244e4ea7fbd466f771 (diff)
downloadqpid-python-8be83d96ee667728d8a5da0db2e68e29c2177dda.tar.gz
added a timestamp class to qpid.datatypes and modified codec to use it for AMQP's datetime type; this fixes support of datetime within maps where formerly decoding and reencoding the same value would switch an entry in a map from an AMQP datetime to an AMQP uint64
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@712673 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/qpid')
-rw-r--r--python/qpid/codec010.py11
-rw-r--r--python/qpid/datatypes.py50
-rw-r--r--python/qpid/spec010.py6
3 files changed, 60 insertions, 7 deletions
diff --git a/python/qpid/codec010.py b/python/qpid/codec010.py
index c2ea7ed512..f34025ef17 100644
--- a/python/qpid/codec010.py
+++ b/python/qpid/codec010.py
@@ -17,8 +17,9 @@
# under the License.
#
+import datetime
from packer import Packer
-from datatypes import serial, RangedSet, Struct
+from datatypes import serial, timestamp, RangedSet, Struct
class CodecException(Exception): pass
@@ -103,9 +104,11 @@ class Codec(Packer):
self.pack("!q", n)
def read_datetime(self):
- return self.read_uint64()
- def write_datetime(self, n):
- self.write_uint64(n)
+ return timestamp(self.read_uint64())
+ def write_datetime(self, t):
+ if isinstance(t, datetime.datetime):
+ t = timestamp(t)
+ self.write_uint64(t)
def read_double(self):
return self.unpack("!d")
diff --git a/python/qpid/datatypes.py b/python/qpid/datatypes.py
index 7150caded2..38fc163dd9 100644
--- a/python/qpid/datatypes.py
+++ b/python/qpid/datatypes.py
@@ -17,7 +17,7 @@
# under the License.
#
-import threading, struct
+import threading, struct, datetime, time
class Struct:
@@ -296,3 +296,51 @@ class UUID:
def __repr__(self):
return "UUID(%r)" % str(self)
+
+class timestamp(float):
+
+ def __new__(cls, obj=None):
+ if obj is None:
+ obj = time.time()
+ elif isinstance(obj, datetime.datetime):
+ obj = time.mktime(obj.timetuple()) + 1e-6 * obj.microsecond
+ return super(timestamp, cls).__new__(cls, obj)
+
+ def datetime(self):
+ return datetime.datetime.fromtimestamp(self)
+
+ def __add__(self, other):
+ if isinstance(other, datetime.timedelta):
+ return timestamp(self.datetime() + other)
+ else:
+ return timestamp(float(self) + other)
+
+ def __sub__(self, other):
+ if isinstance(other, datetime.timedelta):
+ return timestamp(self.datetime() - other)
+ else:
+ return timestamp(float(self) - other)
+
+ def __radd__(self, other):
+ if isinstance(other, datetime.timedelta):
+ return timestamp(self.datetime() + other)
+ else:
+ return timestamp(other + float(self))
+
+ def __rsub__(self, other):
+ if isinstance(other, datetime.timedelta):
+ return timestamp(self.datetime() - other)
+ else:
+ return timestamp(other - float(self))
+
+ def __neg__(self):
+ return timestamp(-float(self))
+
+ def __pos__(self):
+ return self
+
+ def __abs__(self):
+ return timestamp(abs(float(self)))
+
+ def __repr__(self):
+ return "timestamp(%r)" % float(self)
diff --git a/python/qpid/spec010.py b/python/qpid/spec010.py
index 23966e6176..cbc85a5e8b 100644
--- a/python/qpid/spec010.py
+++ b/python/qpid/spec010.py
@@ -17,7 +17,7 @@
# under the License.
#
-import os, cPickle, datatypes
+import os, cPickle, datatypes, datetime
from codec010 import StringCodec
from util import mtime, fill
@@ -477,7 +477,9 @@ class Spec(Node):
None.__class__: "void",
list: "list",
tuple: "list",
- dict: "map"
+ dict: "map",
+ datatypes.timestamp: "datetime",
+ datetime.datetime: "datetime"
}
def __init__(self, major, minor, port, children):