Browse Source

archive: Fix parsing with missing microseconds.

Archive timestamps are stored as the output of datetime.isoformat().
This function omits microseconds in the string output if the
microseconds are zero (as documented and explained at
https://bugs.python.org/issue7342).

Parsing of timestamps assumes there are always microseconds present
after a decimal point. This is not always true. Handle this case where
it is not true by explicitly using '0' microseconds when not present.

This commit fixes #282
Cam Hutchison 10 years ago
parent
commit
9f99aa1abf
1 changed files with 5 additions and 2 deletions
  1. 5 2
      attic/archive.py

+ 5 - 2
attic/archive.py

@@ -163,8 +163,11 @@ class Archive:
     @property
     def ts(self):
         """Timestamp of archive creation in UTC"""
-        t, f = self.metadata[b'time'].split('.', 1)
-        return datetime.strptime(t, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) + timedelta(seconds=float('.' + f))
+        t = self.metadata[b'time'].split('.', 1)
+        dt = datetime.strptime(t[0], '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc)
+        if len(t) > 1:
+            dt += timedelta(seconds=float('.' + t[1]))
+        return dt
 
     def __repr__(self):
         return 'Archive(%r)' % self.name