Skip to content

Instantly share code, notes, and snippets.

@justinallen
Created April 17, 2018 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinallen/35d96c84bf832529087e2ee6cdd0e354 to your computer and use it in GitHub Desktop.
Save justinallen/35d96c84bf832529087e2ee6cdd0e354 to your computer and use it in GitHub Desktop.
Unix dates to human readable and back again, in Python
# 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