Created
April 17, 2018 22:22
-
-
Save justinallen/35d96c84bf832529087e2ee6cdd0e354 to your computer and use it in GitHub Desktop.
Unix dates to human readable and back again, in Python
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
# cribbed / adapted from here http://partiallyattended.com/2011/10/13/managing-unix-time-in-python | |
from time import strftime | |
from datetime import datetime | |
from datetime import date | |
from time import mktime | |
# convert unix to human-readable | |
def unix_to_human(unix_date): | |
human_date = datetime.fromtimestamp(int(unix_date/1000.0)).strftime('%Y-%m-%d %H:%M:%S') | |
return human_date | |
# convert human-readable to unix | |
# from datetime tuple like date(2011, 9, 26) | |
def human_to_unix(human_date): | |
unix_date = mktime(human_date.timetuple()) | |
return unix_date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment