Improved datetime parsing for unit.py
authorPete Vander Giessen <petevg@gmail.com>
Tue, 13 Dec 2016 22:13:41 +0000 (17:13 -0500)
committerPete Vander Giessen <petevg@gmail.com>
Wed, 14 Dec 2016 00:08:14 +0000 (19:08 -0500)
Now uses dateutils lib to handle timezones, nanoseconds, and other
headaches.

juju/unit.py
requirements.txt
setup.py

index cad4bb7..dc5fa63 100644 (file)
@@ -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):
index 4fe52fa..6c000e9 100644 (file)
@@ -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
index 507f344..fe53cb0 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -25,6 +25,7 @@ setup(
         'websockets',
         'pyyaml',
         'theblues',
+        'python-dateutil'
     ],
     include_package_data=True,
     maintainer='Juju Ecosystem Engineering',