#NEWLINE# @ app . template_filter ( ) #NEWLINE# def timesince( d , now = None ) : #NEWLINE# #INDENT# '\n    Takes two datetime objects and returns the time between d and now\n    as a nicely formatted string, e.g. "10 minutes".  If d occurs after now,\n    then "0 minutes" is returned.\n\n    Units used are years, months, weeks, days, hours, and minutes.\n    Seconds and microseconds are ignored.  Up to two adjacent units will be\n    displayed.  For example, "2 weeks, 3 days" and "1 year, 3 months" are\n    possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not.\n\n    Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since\n    ' #NEWLINE# if ( not d ) : #NEWLINE# #INDENT# return 'Never' #NEWLINE# #UNINDENT# if ( d < ( datetime . datetime . now ( ) - datetime . timedelta ( days = 5 ) ) ) : #NEWLINE# #INDENT# return d . date ( ) #NEWLINE# #UNINDENT# chunks = ( ( ( ( ( 60 * 60 ) * 24 ) * 365 ) , ( lambda n : ngettext ( 'year' , 'years' , n ) ) ) , ( ( ( ( 60 * 60 ) * 24 ) * 30 ) , ( lambda n : ngettext ( 'month' , 'months' , n ) ) ) , ( ( ( ( 60 * 60 ) * 24 ) * 7 ) , ( lambda n : ngettext ( 'week' , 'weeks' , n ) ) ) , ( ( ( 60 * 60 ) * 24 ) , ( lambda n : ngettext ( 'day' , 'days' , n ) ) ) , ( ( 60 * 60 ) , ( lambda n : ngettext ( 'hour' , 'hours' , n ) ) ) , ( 60 , ( lambda n : ngettext ( 'minute' , 'minutes' , n ) ) ) ) #NEWLINE# if ( not isinstance ( d , datetime . datetime ) ) : #NEWLINE# #INDENT# d = datetime . datetime ( d . year , d . month , d . day ) #NEWLINE# #UNINDENT# if ( now and ( not isinstance ( now , datetime . datetime ) ) ) : #NEWLINE# #INDENT# now = datetime . datetime ( now . year , now . month , now . day ) #NEWLINE# #UNINDENT# if ( not now ) : #NEWLINE# #INDENT# if d . tzinfo : #NEWLINE# #INDENT# now = datetime . datetime . now ( d . tzinfo ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# now = datetime . datetime . now ( ) #NEWLINE# #UNINDENT# #UNINDENT# delta = ( now - ( d - datetime . timedelta ( 0 , 0 , d . microsecond ) ) ) #NEWLINE# since = ( ( ( ( delta . days * 24 ) * 60 ) * 60 ) + delta . seconds ) #NEWLINE# if ( since <= 0 ) : #NEWLINE# #INDENT# return d . date ( ) #NEWLINE# #UNINDENT# for ( i , ( seconds , name ) ) in enumerate ( chunks ) : #NEWLINE# #INDENT# count = ( since // seconds ) #NEWLINE# if ( count != 0 ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# s = gettext ( '%(number)d %(type)s' , number = count , type = name ( count ) ) #NEWLINE# if ( s == '0 minutes' ) : #NEWLINE# #INDENT# return 'Just now' #NEWLINE# #UNINDENT# if ( s == '1 day' ) : #NEWLINE# #INDENT# return 'Yesterday' #NEWLINE# #UNINDENT# return ( s + ' ago' )