diff --git a/.hgignore b/.hgignore index cef8f3e..ffd82f1 100644 --- a/.hgignore +++ b/.hgignore @@ -7,3 +7,4 @@ syntax: glob *.egg* *.gz build/* +.cache/* diff --git a/README.rst b/README.rst index f116ee4..d3466c0 100644 --- a/README.rst +++ b/README.rst @@ -39,7 +39,7 @@ You really only need to worry about *human*. Here are all the available arguments and defaults:: - human(dt, precision=2, past_tense='{} ago', future_tense='in {}'): + human(dt, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate=False): dt either a datetime or timedelta object to become human readable, required @@ -53,6 +53,9 @@ past_tense future_tense format string used when dt is a future datetime, optional +abbreviate + boolean to abbreviate the units, defaults to False + Here is an example on how to use *human*:: diff --git a/ago.py b/ago.py index d85caff..694bfaf 100644 --- a/ago.py +++ b/ago.py @@ -13,7 +13,7 @@ def delta2dict( delta ): 'microsecond' : delta.microseconds } -def human(dt, precision=2, past_tense='{} ago', future_tense='in {}'): +def human(dt, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate=False): """Accept a datetime or timedelta, return a human readable delta string""" delta = dt if type(dt) is not type(timedelta()): @@ -24,15 +24,19 @@ def human(dt, precision=2, past_tense='{} ago', future_tense='in {}'): the_tense = future_tense d = delta2dict( delta ) + hlist = [] count = 0 units = ( 'year', 'day', 'hour', 'minute', 'second', 'microsecond' ) for unit in units: if count >= precision: break # met precision if d[ unit ] == 0: continue # skip 0's - s = '' if d[ unit ] == 1 else 's' # handle plurals - hlist.append( '%s %s%s' % ( d[unit], unit, s ) ) + if abbreviate: + abr = 'ms' if unit == 'microsecond' else unit[0] + hlist.append('{}{}'.format(d[unit], abr)) + else: + s = '' if d[ unit ] == 1 else 's' # handle plurals + hlist.append('{} {}{}'.format(d[unit], unit, s)) count += 1 - human_delta = ', '.join( hlist ) - return the_tense.format(human_delta) + return the_tense.format(', '.join(hlist)) diff --git a/setup.py b/setup.py index afce8d7..4d621ea 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup( name = 'ago', - version = '0.0.6', + version = '0.0.7', description = 'ago: Human readable timedeltas', keywords = 'ago human readable time deltas timedelta datetime', long_description = open('README.rst').read(), diff --git a/test_ago.py b/test_ago.py index 6db679e..bc37e48 100644 --- a/test_ago.py +++ b/test_ago.py @@ -55,6 +55,12 @@ def test_one_day_singular(): def test_two_day_plural(): assert 's' in human( timedelta(2) ) +def test_abbreviation(): + assert '2d ago' in human( timedelta(2), abbreviate=True ) + assert '3d, 12h ago' in human( timedelta(3.5), abbreviate=True ) + assert '2h, 24m ago' in human( timedelta(.1), abbreviate=True ) + assert '1y, 35d ago' in human( timedelta(400), abbreviate=True ) + def test_past_tense(): output = human( PAST, past_tense = 'titanic sunk {} ago', @@ -85,7 +91,6 @@ def test_valid_future_dict(): assert past_dict['minute'] == 27 assert past_dict['microsecond'] == 963 - def example_usage(): """Test and example usage"""