Return to Snippet

Revision: 5816
at April 7, 2008 18:13 by wbowers


Updated Code
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time = []
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
		  (suffixes[1], 60 * 60 * 24 * 7),
		  (suffixes[2], 60 * 60 * 24),
		  (suffixes[3], 60 * 60),
		  (suffixes[4], 60),
		  (suffixes[5], 1)]
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length in parts:
		value = seconds / length
		if value > 0:
			seconds = seconds % length
			time.append('%s%s' % (str(value),
					       (suffix, (suffix, suffix + 's')[value > 1])[add_s]))
		if seconds < 1:
			break
	
	return separator.join(time)

if __name__ == '__main__':
	# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 1w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=', ')

Revision: 5815
at April 7, 2008 17:51 by wbowers


Updated Code
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time = []
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
		  (suffixes[1], 60 * 60 * 24 * 7),
		  (suffixes[2], 60 * 60 * 24),
		  (suffixes[3], 60 * 60),
		  (suffixes[4], 60),
		  (suffixes[5], 1)]
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length in parts:
		value = seconds / length
		if value > 0:
			seconds = seconds % length
			time.append('%s%s' % (str(value),
					     (suffix, (suffix, suffix + 's')[value > 1])[add_s]))
	
	return separator.join(time)

if __name__ == '__main__':
	# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 1w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=', ')

Revision: 5814
at April 7, 2008 17:50 by wbowers


Updated Code
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time = []
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
		  (suffixes[1], 60 * 60 * 24 * 7),
		  (suffixes[2], 60 * 60 * 24),
		  (suffixes[3], 60 * 60),
		  (suffixes[4], 60),
		  (suffixes[5], 1)]
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length in parts:
		value = seconds / length
		if value > 0:
			seconds = seconds % length
			time.append('%s%s' % (str(value),
					     (suffix, (suffix, suffix + 's')[value > 1])[add_s]))
	
	return separator.join(time).strip()

if __name__ == '__main__':
	# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 1w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=', ')

Revision: 5813
at April 7, 2008 17:45 by wbowers


Updated Code
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time = []
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
		  (suffixes[1], 60 * 60 * 24 * 7),
		  (suffixes[2], 60 * 60 * 24),
		  (suffixes[3], 60 * 60),
		  (suffixes[4], 60),
		  (suffixes[5], 1)]
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length in parts:
		value = seconds / length
		if value > 0:
			seconds = seconds % length
			time.append('%s%s' % (str(value),
										(suffix, (suffix, suffix + 's')[value > 1])[add_s]))
	
	return separator.join(time).strip()

if __name__ == '__main__':
	# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 1w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=', ')

Revision: 5812
at April 5, 2008 18:11 by wbowers


Updated Code
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=''):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time_string = ''
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
		  (suffixes[1], 60 * 60 * 24 * 7),
		  (suffixes[2], 60 * 60 * 24),
		  (suffixes[3], 60 * 60),
		  (suffixes[4], 60),
		  (suffixes[5], 1)]
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length in parts:
		value = seconds / length
		if value > 0:
			seconds = seconds % length
			time_string = '%s%s %s%s' % (time_string, 
										('',separator)[time_string.strip() != ''], 
										str(value), 
										(suffix, (suffix, suffix + 's')[value > 1])[add_s])
	
	return time_string.strip()

if __name__ == '__main__':
	# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 1w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=',')

Revision: 5811
at April 5, 2008 18:06 by wbowers


Updated Code
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=''):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time_string = ''
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
		 (suffixes[1], 60 * 60 * 24 * 7),
		 (suffixes[2], 60 * 60 * 24),
		 (suffixes[3], 60 * 60),
		 (suffixes[4], 60),
		 (suffixes[5], 1)]
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length in parts:
		value = seconds / length
		if value > 0:
			seconds = seconds % length
			time_string = '%s%s %s%s' % (time_string, 
										('',separator)[time_string.strip() != ''], 
										str(value), 
										(suffix, (suffix, suffix + 's')[value > 1])[add_s])
	
	return time_string.strip()

if __name__ == '__main__':
	# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 1w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
	print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=',')

Revision: 5810
at April 5, 2008 17:45 by wbowers


Updated Code
def elapsed_time(seconds):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time_string = ''
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	# - the third piece is the maximum times that piece can exist before it
	#   goes the next level up (there can only be 6 days before it becomes a week)
	parts = [('y', 60 * 60 * 24 * 7 * 52, 499),
		  ('w', 60 * 60 * 24 * 7,      51),
		  ('d', 60 * 60 * 24,          6),
		  ('h', 60 * 60,               23),
		  ('m', 60,                    59),
		  ('s', 1,                     59)]
	
	def _get_value(seconds, length, max_amount):
		"""
		Takes an amount of seconds, a length in seconds for the item in question (day,
		hour, minute, etc) and the max number of that item, and returns the number of
		times that item occurs within the seconds given, and the remainder.
		"""
		value = seconds / length
		if value <= max_amount:
			return (value, seconds % length)
		else:
			return (0, seconds)
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length, max_amount in parts:
		value, seconds = _get_value(seconds, length, max_amount)
		if value > 0:
			time_string = '%s %s%s' % (time_string, str(value), suffix)
	
	return time_string.strip()

if __name__ == '__main__':
	# 2 years, 4 weeks, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 4w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 4) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)

Revision: 5809
at April 5, 2008 17:45 by wbowers


Updated Code
def elapsed_time(seconds):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time_string = ''
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	# - the third piece is the maximum times that piece can exist before it
	#   goes the next level up (there can only be 6 days before it becomes a week)
	parts = [('y', 60 * 60 * 24 * 7 * 52, 499),
		 ('w', 60 * 60 * 24 * 7,      51),
		 ('d', 60 * 60 * 24,          6),
		 ('h', 60 * 60,               23),
		 ('m', 60,                    59),
		 ('s', 1,                     59)]
	
	def _get_value(seconds, length, max_amount):
		"""
		Takes an amount of seconds, a length in seconds for the item in question (day,
		hour, minute, etc) and the max number of that item, and returns the number of
		times that item occurs within the seconds given, and the remainder.
		"""
		value = seconds / length
		if value <= max_amount:
			return (value, seconds % length)
		else:
			return (0, seconds)
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length, max_amount in parts:
		value, seconds = _get_value(seconds, length, max_amount)
		if value > 0:
			time_string = '%s %s%s' % (time_string, str(value), suffix)
	
	return time_string.strip()

if __name__ == '__main__':
	# 2 years, 4 weeks, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 4w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 4) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)

Revision: 5808
at April 5, 2008 17:44 by wbowers


Updated Code
def elapsed_time(seconds):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time_string = ''
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	# - the first piece in each tuple is the suffix (d, h, w)
	# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
	# - the third piece is the maximum times that piece can exist before it
	#   goes the next level up (there can only be 6 days before it becomes a week)
	parts = [('y', 60 * 60 * 24 * 7 * 52, 499),
			 ('w', 60 * 60 * 24 * 7,	  51),
			 ('d', 60 * 60 * 24,		  6),
			 ('h', 60 * 60,				  23),
			 ('m', 60,					  59),
			 ('s', 1,					  59)]
	
	def _get_value(seconds, length, max_amount):
		"""
		Takes an amount of seconds, a length in seconds for the item in question (day,
		hour, minute, etc) and the max number of that item, and returns the number of
		times that item occurs within the seconds given, and the remainder.
		"""
		value = seconds / length
		if value <= max_amount:
			return (value, seconds % length)
		else:
			return (0, seconds)
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length, max_amount in parts:
		value, seconds = _get_value(seconds, length, max_amount)
		if value > 0:
			time_string = '%s %s%s' % (time_string, str(value), suffix)
	
	return time_string.strip()

if __name__ == '__main__':
	# 2 years, 4 weeks, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 4w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 4) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)

Revision: 5807
at April 5, 2008 17:42 by wbowers


Updated Code
def elapsed_time(seconds):
    """
    Takes an amount of seconds and turns it into a human-readable amount of time.
    """
    # the formatted time string to be returned
    time_string = ''
    
    # the pieces of time to iterate over (days, hours, minutes, etc)
    # - the first piece in each tuple is the suffix (d, h, w)
    # - the second piece is the length in seconds (a day is 60s * 60m * 24h)
    # - the third piece is the maximum times that piece can exist before it
    #   goes the next level up (there can only be 6 days before it becomes a week)
    parts = [('y', 60 * 60 * 24 * 7 * 52, 499),
             ('w', 60 * 60 * 24 * 7,      51),
             ('d', 60 * 60 * 24,          6),
             ('h', 60 * 60,               23),
             ('m', 60,                    59),
             ('s', 1,                     59)]
    
    def _get_value(seconds, length, max_amount):
    """
    Takes an amount of seconds, a length in seconds for the item in question (day,
    hour, minute, etc) and the max number of that item, and returns the number of
    times that item occurs within the seconds given, and the remainder.
    """
    value = seconds / length
    if value <= max_amount:
        return (value, seconds % length)
    else:
    return (0, seconds)
    
    # for each time piece, grab the value and remaining seconds, and add it to
    # the time string
    for suffix, length, max_amount in parts:
        value, seconds = _get_value(seconds, length, max_amount)
        if value > 0:
            time_string = '%s %s%s' % (time_string, str(value), suffix)
    
    return time_string.strip()

if __name__ == '__main__':
    # 2 years, 4 weeks, 6 days, 2 hours, 59 minutes, 23 seconds
    # 2y 4w 6d 2h 59m 23s
    seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 4) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
    print elapsed_time(seconds)

Revision: 5806
at April 5, 2008 17:37 by wbowers


Updated Code
def elapsed_time(seconds):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time_string = ''
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	parts = [('y', 60 * 60 * 24 * 7 * 52, 499),
		 ('w', 60 * 60 * 24 * 7,      51),
		 ('d', 60 * 60 * 24,	      6),
		 ('h', 60 * 60,		      23),
		 ('m', 60,		      59),
		 ('s', 1,		      59)]
	
	def _get_value(seconds, length, max_amount):
		"""
		Takes an amount of seconds, a length in seconds for the item in question (day,
		hour, minute, etc) and the max number of that item, and returns the number of
		times that item occurs within the seconds given, and the remainder.
		"""
		value = seconds / length
		if value <= max_amount:
			return (value, seconds % length)
		else:
			return (0, seconds)
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length, max_amount in parts:
		value, seconds = _get_value(seconds, length, max_amount)
		if value > 0:
			time_string = '%s %s%s' % (time_string, str(value), suffix)
	
	return time_string.strip()


if __name__ == '__main__':
	# 2 years, 4 weeks, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 4w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 4) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)

Revision: 5805
at April 5, 2008 17:35 by wbowers


Initial Code
def elapsed_time(seconds):
	"""
	Takes an amount of seconds and turns it into a human-readable amount of time.
	"""
	# the formatted time string to be returned
	time_string = ''
	
	# the pieces of time to iterate over (days, hours, minutes, etc)
	parts = [('y', 60 * 60 * 24 * 7 * 52, 499),
			 ('w', 60 * 60 * 24 * 7,	  51),
			 ('d', 60 * 60 * 24,		  6),
			 ('h', 60 * 60,				  23),
			 ('m', 60,					  59),
			 ('s', 1,					  59)]
	
	def _get_value(seconds, length, max_amount):
		"""
		Takes an amount of seconds, a length in seconds for the item in question (day,
		hour, minute, etc) and the max number of that item, and returns the number of
		times that item occurs within the seconds given, and the remainder.
		"""
		value = seconds / length
		if value <= max_amount:
			return (value, seconds % length)
		else:
			return (0, seconds)
	
	# for each time piece, grab the value and remaining seconds, and add it to
	# the time string
	for suffix, length, max_amount in parts:
		value, seconds = _get_value(seconds, length, max_amount)
		if value > 0:
			time_string = '%s %s%s' % (time_string, str(value), suffix)
	
	return time_string.strip()


if __name__ == '__main__':
	# 2 years, 4 weeks, 6 days, 2 hours, 59 minutes, 23 seconds
	# 2y 4w 6d 2h 59m 23s
	seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 4) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
	print elapsed_time(seconds)

Initial URL


Initial Description
This function takes an amount of time in seconds and returns a human readable time span (i.e.,  4h 5m 23s). The suffixes (d for day, h for hour) are configurable to whatever you want (like day, hour, week, etc). Read the comments within the code for more detail.

Initial Title
Python: elapsed_time (human readable time span given total seconds)

Initial Tags
python, function

Initial Language
Python