Skip to content

Instantly share code, notes, and snippets.

@julienr
Created September 17, 2015 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save julienr/9b694e79483fb34fcbc2 to your computer and use it in GitHub Desktop.
Save julienr/9b694e79483fb34fcbc2 to your computer and use it in GitHub Desktop.
Python timestamp
def date_from_timestamp_ms(timestamp_ms):
date = datetime.utcfromtimestamp(timestamp_ms / 1000.0)
return date
def format_date(timestamp_ms, sep=None):
if sep is None:
sep = '_'
date = date_from_timestamp_ms(timestamp_ms)
return date.strftime('%Y{0}%m{0}%d'.format(sep))
def parse_date(datestr, sep=None):
if sep is None:
sep = '_'
date = datetime.strptime(datestr, '%Y{0}%m{0}%d'.format(sep))
timestamp_ms = int(calendar.timegm(date.timetuple()) * 1000.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment