#NEWLINE# def format_timedelta( delta , granularity = 'second' , threshold = 0.85 ) : #NEWLINE# #INDENT# if isinstance ( delta , datetime ) : #NEWLINE# #INDENT# delta = ( datetime . utcnow ( ) - delta ) #NEWLINE# #UNINDENT# if isinstance ( delta , timedelta ) : #NEWLINE# #INDENT# seconds = int ( ( ( delta . days * 86400 ) + delta . seconds ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# seconds = delta #NEWLINE# #UNINDENT# for ( unit , secs_per_unit ) in TIMEDELTA_UNITS : #NEWLINE# #INDENT# value = ( abs ( seconds ) / secs_per_unit ) #NEWLINE# if ( ( value >= threshold ) or ( unit == granularity ) ) : #NEWLINE# #INDENT# if ( ( unit == granularity ) and ( value > 0 ) ) : #NEWLINE# #INDENT# value = max ( 1 , value ) #NEWLINE# #UNINDENT# value = int ( round ( value ) ) #NEWLINE# rv = ( '%s %s' % ( value , unit ) ) #NEWLINE# if ( value != 1 ) : #NEWLINE# #INDENT# rv += 's' #NEWLINE# #UNINDENT# return rv #NEWLINE# #UNINDENT# #UNINDENT# return ''