mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-03-25 21:17:37 +00:00
teach abbreviate_time to understand timedelta
original review wanted to do this instead of depending on "humanize"
This commit is contained in:
parent
c97e8520f4
commit
6e17328eb5
@ -3,6 +3,7 @@ def foo(): pass # keep the line number constant
|
||||
|
||||
import os, time, sys
|
||||
from StringIO import StringIO
|
||||
from datetime import timedelta
|
||||
from twisted.trial import unittest
|
||||
from twisted.internet import defer, reactor
|
||||
from twisted.python.failure import Failure
|
||||
@ -75,6 +76,42 @@ class HumanReadable(unittest.TestCase):
|
||||
hr(e) == "<NoArgumentException>" # python-2.4
|
||||
or hr(e) == "NoArgumentException()") # python-2.5
|
||||
|
||||
def test_abbrev_time_1s(self):
|
||||
diff = timedelta(seconds=1)
|
||||
s = abbreviate.abbreviate_time(diff)
|
||||
self.assertEqual('1 second ago', s)
|
||||
|
||||
def test_abbrev_time_25s(self):
|
||||
diff = timedelta(seconds=25)
|
||||
s = abbreviate.abbreviate_time(diff)
|
||||
self.assertEqual('25 seconds ago', s)
|
||||
|
||||
def test_abbrev_time_future_5_minutes(self):
|
||||
diff = timedelta(minutes=-5)
|
||||
s = abbreviate.abbreviate_time(diff)
|
||||
self.assertEqual('5 minutes in the future', s)
|
||||
|
||||
def test_abbrev_time_hours(self):
|
||||
diff = timedelta(hours=4)
|
||||
s = abbreviate.abbreviate_time(diff)
|
||||
self.assertEqual('4 hours ago', s)
|
||||
|
||||
def test_abbrev_time_day(self):
|
||||
diff = timedelta(hours=49) # must be more than 2 days
|
||||
s = abbreviate.abbreviate_time(diff)
|
||||
self.assertEqual('2 days ago', s)
|
||||
|
||||
def test_abbrev_time_month(self):
|
||||
diff = timedelta(days=91)
|
||||
s = abbreviate.abbreviate_time(diff)
|
||||
self.assertEqual('3 months ago', s)
|
||||
|
||||
def test_abbrev_time_year(self):
|
||||
diff = timedelta(weeks=(5 * 52) + 1)
|
||||
s = abbreviate.abbreviate_time(diff)
|
||||
self.assertEqual('5 years ago', s)
|
||||
|
||||
|
||||
|
||||
class MyList(list):
|
||||
pass
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
import re
|
||||
from datetime import timedelta
|
||||
|
||||
HOUR = 3600
|
||||
DAY = 24*3600
|
||||
@ -8,24 +9,40 @@ MONTH = 30*DAY
|
||||
YEAR = 365*DAY
|
||||
|
||||
def abbreviate_time(s):
|
||||
postfix = ''
|
||||
if isinstance(s, timedelta):
|
||||
# this feels counter-intuitive that positive numbers in a
|
||||
# time-delta are "the past"; but if you "do math" on two
|
||||
# datetime instances as below, you get a "positve seconds"
|
||||
# timedelta instance:
|
||||
# a = datetime.utcnow()
|
||||
# time.sleep(1)
|
||||
# b = datetime.utcnow()
|
||||
# print(b - a) # 0:00:01.001203
|
||||
s = s.total_seconds()
|
||||
if s >= 0.0:
|
||||
postfix = ' ago'
|
||||
else:
|
||||
postfix = ' in the future'
|
||||
s = -s
|
||||
def _plural(count, unit):
|
||||
count = int(count)
|
||||
if count == 1:
|
||||
return "%d %s" % (count, unit)
|
||||
return "%d %ss" % (count, unit)
|
||||
return "%d %s%s" % (count, unit, postfix)
|
||||
return "%d %ss%s" % (count, unit, postfix)
|
||||
if s is None:
|
||||
return "unknown"
|
||||
if s < 120:
|
||||
return _plural(s, "second")
|
||||
if s < 3*HOUR:
|
||||
return _plural(s/60, "minute")
|
||||
return _plural(s / 60, "minute")
|
||||
if s < 2*DAY:
|
||||
return _plural(s/HOUR, "hour")
|
||||
return _plural(s / HOUR, "hour")
|
||||
if s < 2*MONTH:
|
||||
return _plural(s/DAY, "day")
|
||||
return _plural(s / DAY, "day")
|
||||
if s < 4*YEAR:
|
||||
return _plural(s/MONTH, "month")
|
||||
return _plural(s/YEAR, "year")
|
||||
return _plural(s / MONTH, "month")
|
||||
return _plural(s / YEAR, "year")
|
||||
|
||||
def abbreviate_space(s, SI=True):
|
||||
if s is None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user