From 0a3b685af80dc92e039241b17e712b15994f5769 Mon Sep 17 00:00:00 2001 From: Pete Vander Giessen Date: Tue, 13 Dec 2016 17:13:41 -0500 Subject: [PATCH] Improved datetime parsing for unit.py Now uses dateutils lib to handle timezones, nanoseconds, and other headaches. --- juju/unit.py | 13 ++++--------- requirements.txt | 1 + setup.py | 1 + 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/juju/unit.py b/juju/unit.py index cad4bb7..dc5fa63 100644 --- a/juju/unit.py +++ b/juju/unit.py @@ -1,5 +1,6 @@ import logging -from datetime import datetime + +from dateutil.parser import parse as parse_date from . import model from .client import client @@ -20,10 +21,7 @@ class Unit(model.ModelEntity): """Get the time when the `agent_status` was last updated. """ - since = self.data['agent-status']['since'] - # Juju gives us nanoseconds, but Python only supports microseconds - since = since[:26] - return datetime.strptime(since, "%Y-%m-%dT%H:%M:%S.%f") + return parse_date(self.data['agent-status']['since']) @property def agent_status_message(self): @@ -44,10 +42,7 @@ class Unit(model.ModelEntity): """Get the time when the `workload_status` was last updated. """ - since = self.data['workload-status']['since'] - # Juju gives us nanoseconds, but Python only supports microseconds - since = since[:26] - return datetime.strptime(since, "%Y-%m-%dT%H:%M:%S.%f") + return parse_date(self.data['workload-status']['since']) @property def workload_status_message(self): diff --git a/requirements.txt b/requirements.txt index 4fe52fa..6c000e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ # until https://github.com/juju/theblues/pull/46 and https://github.com/juju/theblues/pull/47 # are merged, this depends on the following fork: git+https://github.com/johnsca/theblues@libjuju#egg=theblues +python-dateutil==2.6.0 diff --git a/setup.py b/setup.py index 507f344..fe53cb0 100644 --- a/setup.py +++ b/setup.py @@ -25,6 +25,7 @@ setup( 'websockets', 'pyyaml', 'theblues', + 'python-dateutil' ], include_package_data=True, maintainer='Juju Ecosystem Engineering', -- 2.25.1