From 168b6985aa54366e0809c8ec13fb4f8a85c00e61 Mon Sep 17 00:00:00 2001 From: Milkey Mouse Date: Thu, 5 Nov 2020 00:59:00 -0800 Subject: [PATCH] Make timestamp helper timezone-aware --- src/borg/helpers.py | 4 ++-- src/borg/testsuite/helpers.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/borg/helpers.py b/src/borg/helpers.py index 617d7941d..f61e15b71 100644 --- a/src/borg/helpers.py +++ b/src/borg/helpers.py @@ -606,7 +606,7 @@ def timestamp(s): try: # is it pointing to a file / directory? ts = safe_s(os.stat(s).st_mtime) - return datetime.utcfromtimestamp(ts) + return datetime.fromtimestamp(ts, tz=timezone.utc) except OSError: # didn't work, try parsing as timestamp. UTC, no TZ, no microsecs support. for format in ('%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%dT%H:%M:%S+00:00', @@ -615,7 +615,7 @@ def timestamp(s): '%Y-%m-%d', '%Y-%j', ): try: - return datetime.strptime(s, format) + return datetime.strptime(s, format).replace(tzinfo=timezone.utc) except ValueError: continue raise ValueError diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 92ef64245..43ccb1d9b 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -190,7 +190,7 @@ class TestLocationWithoutEnv: "Location(proto='ssh', user=None, host='host', port=None, path='/path', archive='2016-12-31@23:59:59')" def test_with_timestamp(self): - assert repr(Location('path::archive-{utcnow}').with_timestamp(datetime(2002, 9, 19))) == \ + assert repr(Location('path::archive-{utcnow}').with_timestamp(datetime(2002, 9, 19, tzinfo=timezone.utc))) == \ "Location(proto='file', user=None, host=None, port=None, path='path', archive='archive-2002-09-19T00:00:00')" def test_underspecified(self, monkeypatch):