Last active
April 14, 2017 13:48
-
-
Save l0kix2/56b1b782e7d3a234ed56fe014b07d518 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| EXAMPLE_DEBIAN_DATE_WITHOUT_TZ = 'Thu, 06 Apr 2017 20:44:53' | |
| EXAMPLE_DEBIAN_DATE_TZ = '+0300' | |
| FORMAT_WITHOUT_TZ = '%a, %d %b %Y %H:%M:%S' | |
| import datetime | |
| import pytz | |
| def parse_debian_date(date_str): | |
| "parse debian format date in naive datetime.datetime in UTC timezone" | |
| date_str_without_tz, tz_piece = date_str[:len(EXAMPLE_DEBIAN_DATE_WITHOUT_TZ)], date_str[-len(EXAMPLE_DEBIAN_DATE_TZ):] | |
| parsed = datetime.datetime.strptime(date_str_without_tz, FORMAT_WITHOUT_TZ) | |
| tz_hours, tz_minutes = int(tz_piece[1:2]), int(tz_piece[3:4]) | |
| delta = datetime.timedelta(hours=tz_hours, minutes=tz_minutes) | |
| if tz_piece[0] == '+': | |
| parsed -= delta | |
| else: | |
| parsed += delta | |
| return parsed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment