Created
October 4, 2011 12:21
-
-
Save vlcinsky/1261497 to your computer and use it in GitHub Desktop.
calculate_age_in_minutes explained
This file contains hidden or 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 calculate_age_in_minutes(rec_time, pub_time): | |
""" | |
pub_time .. string with datetime in ISO 8601 format. Time, when is the feed published (mostly current time). | |
rec_time .. string with datetime in ISO 8601 format. Time, when record was created. | |
sample ISO string: 2011-10-25T13:55:42.123Z | |
return .. integer with duration between pub_time and rec_time expressed in minutes | |
""" | |
#parsing pub_time to datetime structure. Ignoring timezone and fractions of seconds | |
pub_t = datetime.datetime.strptime(pub_time[:19],"%Y-%m-%dT%H:%M:%S") | |
#parsing rec_time to datetime structure. Ignoring timezone and fractions of seconds | |
rec_t = datetime.datetime.strptime(rec_time[:19],"%Y-%m-%dT%H:%M:%S") | |
#calculating duration between publication time and record time (as time delta structure) | |
td = pub_t - rec_t | |
#convert duration td to minutes. | |
return int(((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6) / 60) | |
#above calculation can be probably simplified - skip microseconds and get rid of multiply/divide by 10**6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment