From 01b4f31d59837459cc62d30332c096f6e1be79df Mon Sep 17 00:00:00 2001 From: SandyWalsh Date: Fri, 10 Aug 2012 15:35:34 -0300 Subject: Support for marshalling datetime while preserving microseconds. Needed for performance measurement via "inflight" service. Change-Id: I5c1a8395b4cbc5fc0f8649b3af8130f45dd401bb --- openstack/common/timeutils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'openstack') diff --git a/openstack/common/timeutils.py b/openstack/common/timeutils.py index 4416a3b..ae300e4 100644 --- a/openstack/common/timeutils.py +++ b/openstack/common/timeutils.py @@ -106,3 +106,21 @@ def advance_time_seconds(seconds): def clear_time_override(): """Remove the overridden time.""" utcnow.override_time = None + + +def marshall_now(now=None): + """Make an rpc-safe datetime with microseconds. + + Note: tzinfo is stripped, but not required for relative times.""" + if not now: + now = utcnow() + return dict(day=now.day, month=now.month, year=now.year, hour=now.hour, + minute=now.minute, second=now.second, + microsecond=now.microsecond) + + +def unmarshall_time(tyme): + """Unmarshall a datetime dict.""" + return datetime.datetime(day=tyme['day'], month=tyme['month'], + year=tyme['year'], hour=tyme['hour'], minute=tyme['minute'], + second=tyme['second'], microsecond=tyme['microsecond']) -- cgit