File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- from datetime import datetime
1+ from datetime import datetime , timedelta
2+ import time
23
34
45def datetime_from_mtime (path ):
@@ -40,6 +41,37 @@ def datetime_from_string(s):
4041 raise ValueError ('could not parse %r' % s )
4142
4243
44+ def datetime_from_dir (d , p = 4 ):
45+ """
46+ parse datetime from part of a directory name
47+
48+ returns a datetime object if the format could be parsed.
49+ raises ValueError if not.
50+ """
51+ if type (d ).__name__ == 'PosixPath' :
52+ s = d .name
53+ elif type (d ) == str :
54+ # in case input is just a string (for testing)
55+ s = d
56+ # get rid of trailing -??? numbers that BackInTime adds
57+ s = s [:- p ].strip ()
58+ for ts_format in [
59+ # Back In Time format
60+ '%Y%m%d-%H%M%S' ,
61+ ]:
62+ try :
63+ dt = datetime .strptime (s , ts_format )
64+ # adjust time zone offset to get UTC
65+ tz = int (time .strftime ('%z' )[:- 2 ])
66+ ut = dt - timedelta (hours = tz )
67+ return ut
68+ except ValueError :
69+ # didn't work with this format, try next
70+ pass
71+ else :
72+ raise ValueError ('could not parse %r' % s )
73+
74+
4375def datetime_from_file (path ):
4476 """
4577 discover backup timestamp from contents of a file.
You can’t perform that action at this time.
0 commit comments