Skip to content

Instantly share code, notes, and snippets.

@danieljfarrell
Created January 5, 2017 15:18
Show Gist options
  • Save danieljfarrell/edbd48113f2d724185824b77cd48cf1c to your computer and use it in GitHub Desktop.
Save danieljfarrell/edbd48113f2d724185824b77cd48cf1c to your computer and use it in GitHub Desktop.
Cheap and cheerful parser for iso8601 with zero flexibility.
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
@danieljfarrell
Copy link
Author

Problem with seconds when the value is >59.5 because it rounds to 60 which is invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment