mirror of https://github.com/borgbackup/borg.git
Make timestamp helper timezone-aware
This commit is contained in:
parent
9084f94ba2
commit
47143cae76
|
@ -24,7 +24,7 @@ def timestamp(s):
|
||||||
try:
|
try:
|
||||||
# is it pointing to a file / directory?
|
# is it pointing to a file / directory?
|
||||||
ts = safe_s(os.stat(s).st_mtime)
|
ts = safe_s(os.stat(s).st_mtime)
|
||||||
return datetime.utcfromtimestamp(ts)
|
return datetime.fromtimestamp(ts, tz=timezone.utc)
|
||||||
except OSError:
|
except OSError:
|
||||||
# didn't work, try parsing as timestamp. UTC, no TZ, no microsecs support.
|
# 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',
|
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',
|
'%Y-%m-%d', '%Y-%j',
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
return datetime.strptime(s, format)
|
return datetime.strptime(s, format).replace(tzinfo=timezone.utc)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
|
@ -190,7 +190,7 @@ class TestLocationWithoutEnv:
|
||||||
"Location(proto='ssh', user=None, host='host', port=None, path='/path', archive='2016-12-31@23:59:59')"
|
"Location(proto='ssh', user=None, host='host', port=None, path='/path', archive='2016-12-31@23:59:59')"
|
||||||
|
|
||||||
def test_with_timestamp(self):
|
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')"
|
"Location(proto='file', user=None, host=None, port=None, path='path', archive='archive-2002-09-19T00:00:00')"
|
||||||
|
|
||||||
def test_underspecified(self, monkeypatch):
|
def test_underspecified(self, monkeypatch):
|
||||||
|
|
Loading…
Reference in New Issue