Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rather:

if isinstance(d, Path):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rather:

if isinstance(d, Path):

s = d.name
elif type(d) == str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar here.

# in case input is just a string (for testing)
s = d
# get rid of trailing -??? numbers that BackInTime adds
s = s[:-p].strip()
Comment on lines +56 to +57
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is always "dash digits" you could also use s.rsplit('-', 1).

Comment on lines +56 to +57
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is always "dash digits" you could also use s.rsplit('-', 1).

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)
Comment on lines +63 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe reading the docs again gives a better way here. :-)

Comment on lines +63 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe reading the docs again gives a better way here. :-)

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