Created
January 5, 2017 15:18
-
-
Save danieljfarrell/edbd48113f2d724185824b77cd48cf1c to your computer and use it in GitHub Desktop.
Cheap and cheerful parser for iso8601 with zero flexibility.
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
import datetime | |
import time | |
now = datetime.datetime.now() | |
now_iso = now.isoformat() | |
def parse_iso8601_badly(iso_str): | |
date_str, time_str = iso_str.split('T') | |
d = dict() | |
d.update(zip(['hour', 'minute', 'second'], | |
map(int, map(round, (map(float, time_str.split(':'))))))) | |
d.update(zip(['year', 'month', 'day'], map(int, date_str.split('-')))) | |
return datetime.datetime(**d) | |
if __name__ == '__main__': | |
d = parse_iso8601_badly(now_iso) | |
print type(d), d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem with seconds when the value is >59.5 because it rounds to 60 which is invalid.