Created
September 17, 2015 11:21
-
-
Save julienr/9b694e79483fb34fcbc2 to your computer and use it in GitHub Desktop.
Python timestamp
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
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