Skip to content

Instantly share code, notes, and snippets.

@vlcinsky
Created October 4, 2011 12:21
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 vlcinsky/1261497 to your computer and use it in GitHub Desktop.
Save vlcinsky/1261497 to your computer and use it in GitHub Desktop.
calculate_age_in_minutes explained
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