diff options
Diffstat (limited to 'python/qpid')
| -rw-r--r-- | python/qpid/codec010.py | 11 | ||||
| -rw-r--r-- | python/qpid/datatypes.py | 50 | ||||
| -rw-r--r-- | python/qpid/spec010.py | 6 |
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): |
