From 237a6f508a904f289a2919c226e77f6e5e71294b Mon Sep 17 00:00:00 2001 From: RussellBallestrini Date: Mon, 7 Jan 2013 21:12:29 -0500 Subject: [PATCH] Altered human to support future dates We should move tests into its own file --- ago.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/ago.py b/ago.py index ecf36c5..16a0284 100644 --- a/ago.py +++ b/ago.py @@ -28,26 +28,31 @@ def human( d1, precision = 2 ): from datetime import datetime d2 = datetime.now() delta = d2 - d1 + if d2 < d1: delta = d1 - d2 return delta2human( delta, precision ) def test(): """Test and example usage""" from datetime import datetime - d1 = datetime( - year = 2009, - month=5, - day=4, - hour=6, - minute=54, - second=33, - microsecond=4000 - ) - d2 = datetime.now() - delta = d2 - d1 - print 'This is just the ago.py test:' - print delta2dict( delta ) - print 'Commented ' + delta2human( delta, 3 ) + ' ago' - print 'Commented ' + human( d1 ) + ' ago' + from datetime import timedelta + + present = datetime.now() + past = present - timedelta( 492, 58711, 45 ) # days, secs, ms + future = present + timedelta( 2, 12447, 963 ) # days, secs, ms + + pdelta = present - past + fdelta = future - present + + print '\nTesting past:\n' + print delta2dict( pdelta ) + print 'Commented ' + delta2human( pdelta, 3 ) + ' ago' + print 'Commented ' + human( past ) + ' ago' + + print '\nTesting future:\n' + print delta2dict( fdelta ) + print 'Shutdown in ' + delta2human( fdelta, 3 ) + print 'Shutdown in ' + human( future ) + print '' if __name__ == "__main__": test()