From 9084f94ba28de0e4f1a9ba468480bcee5190f9f0 Mon Sep 17 00:00:00 2001 From: Milkey Mouse Date: Thu, 5 Nov 2020 00:51:08 -0800 Subject: [PATCH 1/2] Remove redundant copy of timestamp helper --- src/borg/helpers/parseformat.py | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/borg/helpers/parseformat.py b/src/borg/helpers/parseformat.py index 6acb2a0384..b17da50553 100644 --- a/src/borg/helpers/parseformat.py +++ b/src/borg/helpers/parseformat.py @@ -88,26 +88,6 @@ def interval(s): return hours -def timestamp(s): - """Convert a --timestamp=s argument to a datetime object""" - try: - # is it pointing to a file / directory? - ts = safe_s(os.stat(s).st_mtime) - return datetime.utcfromtimestamp(ts) - 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', - '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S', - '%Y-%m-%dT%H:%M', '%Y-%m-%d %H:%M', - '%Y-%m-%d', '%Y-%j', - ): - try: - return datetime.strptime(s, format) - except ValueError: - continue - raise ValueError - - def ChunkerParams(s): params = s.strip().split(',') count = len(params) From 47143cae76f7d9bdc4b5eee7181e5a60514e4de0 Mon Sep 17 00:00:00 2001 From: Milkey Mouse Date: Thu, 5 Nov 2020 00:59:00 -0800 Subject: [PATCH 2/2] Make timestamp helper timezone-aware --- src/borg/helpers/time.py | 4 ++-- src/borg/testsuite/helpers.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/borg/helpers/time.py b/src/borg/helpers/time.py index 9c525ecfcc..39abffdc4e 100644 --- a/src/borg/helpers/time.py +++ b/src/borg/helpers/time.py @@ -24,7 +24,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', @@ -33,7 +33,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 55571653be..f3d80b250a 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -190,7 +190,7 @@ def test_user_parsing(self): "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):