diff --git a/README.rst b/README.rst index d3466c0..de248db 100644 --- a/README.rst +++ b/README.rst @@ -17,9 +17,9 @@ Testing future tense:: How to install =================== -There are a number of ways to install this package:: +There are a number of ways to install this package. - easy_install ago +You could run this ad hoc command:: pip install ago diff --git a/ago.py b/ago.py index 1022a36..b988e2a 100644 --- a/ago.py +++ b/ago.py @@ -1,10 +1,12 @@ -from datetime import datetime -from datetime import timedelta +from datetime import ( + datetime, + timedelta, +) def delta2dict( delta ): """Accepts a delta, returns a dictionary of units""" delta = abs( delta ) - return { + return { 'year' : int(delta.days / 365), 'day' : int(delta.days % 365), 'hour' : int(delta.seconds / 3600), @@ -14,21 +16,26 @@ def delta2dict( delta ): } def human(dt, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate=False): - """Accept a datetime or timedelta, return a human readable delta string""" + """Accept a datetime or timedelta, return a human readable delta string.""" + + # if dt is a datetime object, get a timedelta object from it. delta = dt if type(dt) is not type(timedelta()): dt_no_tz = dt.replace(tzinfo=None) delta = datetime.now() - dt_no_tz - + + # determine if the_tense is past_tense or future_tense. the_tense = past_tense if delta < timedelta(0): the_tense = future_tense d = delta2dict( delta ) - hlist = [] + hlist = [] count = 0 units = ( 'year', 'day', 'hour', 'minute', 'second', 'microsecond' ) + + # start building up the output in the hlist. for unit in units: if count >= precision: break # met precision if d[ unit ] == 0: continue # skip 0's @@ -40,4 +47,4 @@ def human(dt, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate hlist.append('{} {}{}'.format(d[unit], unit, s)) count += 1 - return the_tense.format(', '.join(hlist)) + return the_tense.format(', '.join(hlist)) diff --git a/setup.py b/setup.py index 4d621ea..fc89e3c 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,10 @@ -# installation: easy_install ago +# installation: pip install ago from setuptools import setup setup( name = 'ago', - version = '0.0.7', + version = '0.0.8', description = 'ago: Human readable timedeltas', keywords = 'ago human readable time deltas timedelta datetime', long_description = open('README.rst').read(),