Prechádzať zdrojové kódy

PR #283 - Merge branch 'fix-missing-microseconds' of https://github.com/camh-/attic into merge

Thomas Waldmann 10 rokov pred
rodič
commit
2f71b39866
2 zmenil súbory, kde vykonal 30 pridanie a 3 odobranie
  1. 5 2
      attic/archive.py
  2. 25 1
      attic/testsuite/archive.py

+ 5 - 2
attic/archive.py

@@ -169,8 +169,11 @@ class Archive:
     @property
     @property
     def ts(self):
     def ts(self):
         """Timestamp of archive creation in UTC"""
         """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):
     def __repr__(self):
         return 'Archive(%r)' % self.name
         return 'Archive(%r)' % self.name

+ 25 - 1
attic/testsuite/archive.py

@@ -1,7 +1,10 @@
 import msgpack
 import msgpack
 from attic.testsuite import AtticTestCase
 from attic.testsuite import AtticTestCase
-from attic.archive import CacheChunkBuffer, RobustUnpacker
+from attic.testsuite.mock import Mock
+from attic.archive import Archive, CacheChunkBuffer, RobustUnpacker
 from attic.key import PlaintextKey
 from attic.key import PlaintextKey
+from attic.helpers import Manifest
+from datetime import datetime, timezone
 
 
 
 
 class MockCache:
 class MockCache:
@@ -14,6 +17,27 @@ class MockCache:
         return id, len(data), len(data)
         return id, len(data), len(data)
 
 
 
 
+class ArchiveTimestampTestCase(AtticTestCase):
+
+    def _test_timestamp_parsing(self, isoformat, expected):
+        repository = Mock()
+        key = PlaintextKey()
+        manifest = Manifest(repository, key)
+        a = Archive(repository, key, manifest, 'test', create=True)
+        a.metadata = {b'time': isoformat}
+        self.assert_equal(a.ts, expected)
+
+    def test_with_microseconds(self):
+        self._test_timestamp_parsing(
+            '1970-01-01T00:00:01.000001',
+            datetime(1970, 1, 1, 0, 0, 1, 1, timezone.utc))
+
+    def test_without_microseconds(self):
+        self._test_timestamp_parsing(
+            '1970-01-01T00:00:01',
+            datetime(1970, 1, 1, 0, 0, 1, 0, timezone.utc))
+
+
 class ChunkBufferTestCase(AtticTestCase):
 class ChunkBufferTestCase(AtticTestCase):
 
 
     def test(self):
     def test(self):