-
Notifications
You must be signed in to change notification settings - Fork 27
added new datetime_from_dir() function to parse BackInTime folder names #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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): | ||
|
|
@@ -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': | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rather: |
||
| s = d.name | ||
| elif type(d) == str: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it is always "dash digits" you could also use
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it is always "dash digits" you could also use |
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rather: