fix millisecs versus microsecs

changed ago.py
changed setup.py
changed test_ago.py
This commit is contained in:
russellballestrini 2017-10-14 20:49:50 -04:00
parent ddcd765260
commit 36102bba88
3 changed files with 14 additions and 7 deletions

12
ago.py
View file

@ -3,7 +3,7 @@ from datetime import (
timedelta,
)
units = ('year', 'day', 'hour', 'minute', 'second', 'microsecond')
units = ('year', 'day', 'hour', 'minute', 'second', 'millisecond', 'microsecond')
def get_delta_from_subject(subject):
subject_type = type(subject)
@ -28,7 +28,8 @@ def delta2dict(delta):
'hour' : int(delta.seconds / 3600),
'minute' : int(delta.seconds / 60) % 60,
'second' : delta.seconds % 60,
'microsecond' : delta.microseconds
'millisecond' : delta.microseconds/1000,
'microsecond' : delta.microseconds%1000,
}
def human(subject, precision=2, past_tense='{} ago', future_tense='in {}', abbreviate=False):
@ -56,7 +57,12 @@ def human(subject, precision=2, past_tense='{} ago', future_tense='in {}', abbre
if count >= precision: break # met precision
if d[ unit ] == 0: continue # skip 0's
if abbreviate:
abr = 'mu' if unit == 'microsecond' else unit[0]
if unit == 'millisecond':
abr = 'ms'
elif unit == 'microsecond':
abr = 'um'
else:
abr = unit[0]
hlist.append('{}{}'.format(d[unit], abr))
else:
s = '' if d[ unit ] == 1 else 's' # handle plurals

View file

@ -4,7 +4,7 @@ from setuptools import setup
setup(
name = 'ago',
version = '0.0.91',
version = '0.0.92',
description = 'ago: Human readable timedeltas',
keywords = 'ago human readable time deltas timedelta datetime timestamp',
long_description = open('README.rst').read(),

View file

@ -8,8 +8,8 @@ from ago import delta2dict
# datetime objects
PRESENT = datetime.now()
PAST = PRESENT - timedelta( 492, 58711, 45 ) # days, secs, ms
FUTURE = PRESENT + timedelta( 2, 12447, 963 ) # days, secs, ms
PAST = PRESENT - timedelta( 492, 58711, 45 ) # days, secs, microseconds
FUTURE = PRESENT + timedelta( 2, 12447, 967742 ) # days, secs, microseconds
# timedelta objects
PAST_DELTA = PRESENT - PAST
@ -101,7 +101,8 @@ def test_valid_future_dict():
assert past_dict['day'] == 2
assert past_dict['hour'] == 3
assert past_dict['minute'] == 27
assert past_dict['microsecond'] == 963
assert past_dict['millisecond'] == 967
assert past_dict['microsecond'] == 742
def example_usage():
"""Test and example usage"""