Created
July 9, 2012 20:49
-
-
Save squioc/3078803 to your computer and use it in GitHub Desktop.
conversion between iso8601 date format and unix epoch datetime
This file contains 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
from datetime import datetime | |
import calendar | |
def epoch_to_iso8601(timestamp): | |
""" | |
epoch_to_iso8601 - convert the unix epoch time into a iso8601 formatted date | |
>>> epoch_to_iso8601(1341866722) | |
'2012-07-09T22:45:22' | |
""" | |
return datetime.fromtimestamp(timestamp).isoformat() | |
def iso8601_to_epoch(datestring): | |
""" | |
iso8601_to_epoch - convert the iso8601 date into the unix epoch time | |
>>> iso8601_to_epoch("2012-07-09T22:27:50.272517") | |
1341872870 | |
""" | |
return calendar.timegm(datetime.strptime(datestring, "%Y-%m-%dT%H:%M:%S.%f").timetuple()) | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
You can use that:
>>> from dateutil.parser import parse >>> parse('2017-03-08T14:55:24Z').timestamp() 1488984924.0
Works well for me. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use that: