Skip to content

Commit

Permalink
added datetime_from_dir() for BackInTime dirs (and UTC conversion)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoenig committed Feb 1, 2022
1 parent 4f1d7ce commit 7aad923
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/borg_import/helpers/timestamps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from datetime import datetime, timedelta
import time


def datetime_from_mtime(path):
Expand Down Expand Up @@ -40,6 +41,37 @@ def datetime_from_string(s):
raise ValueError('could not parse %r' % s)


def datetime_from_dir(d, p=4):
"""
parse datetime from part of a directory name
returns a datetime object if the format could be parsed.
raises ValueError if not.
"""
if type(d).__name__ == 'PosixPath':
s = d.name
elif type(d) == str:
# in case input is just a string (for testing)
s = d
# get rid of trailing -??? numbers that BackInTime adds
s = s[:-p].strip()
for ts_format in [
# Back In Time format
'%Y%m%d-%H%M%S',
]:
try:
dt = datetime.strptime(s, ts_format)
# adjust time zone offset to get UTC
tz = int(time.strftime('%z')[:-2])
ut = dt - timedelta(hours=tz)
return ut
except ValueError:
# didn't work with this format, try next
pass
else:
raise ValueError('could not parse %r' % s)


def datetime_from_file(path):
"""
discover backup timestamp from contents of a file.
Expand Down

0 comments on commit 7aad923

Please sign in to comment.