use format_time from quodlibet source

git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@3131 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2006-05-03 16:35:37 +00:00
parent e6f00e093b
commit 58993644d8
2 changed files with 85 additions and 17 deletions

View file

@ -14,6 +14,10 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Some functions have been taken and adjusted from the quodlibet
# source. Quodlibet is (C) 2004-2005 Joe Wreschnig, Michael Urman
# and licensed under the GNU General Public License version 2.
"""
Various string utility functions. Note that these functions are not
necessarily optimised for large strings, so use with care.
@ -23,6 +27,7 @@ import re
import textwrap
import codecs
import os
import math
import time
import urlparse
import pydoc
@ -219,18 +224,61 @@ def strtime (t):
strtimezone()
# from quodlibet
def strduration (duration):
"""
Return translated and formatted time duration.
"""
name = _("seconds")
if duration >= 60:
duration /= 60.0
name = _("minutes")
if duration >= 60:
duration /= 60.0
name = _("hours")
return u"%.3f %s" % (duration, name)
"""Turn a time value in seconds into hh:mm:ss or mm:ss."""
if duration < 0:
duration = abs(duration)
prefix = "-"
else:
prefix = ""
duration = math.ceil(duration)
if duration >= 3600: # 1 hour
# time, in hours:minutes:seconds
return "%s%02d:%02d:%02d" % (prefix, duration // 3600,
(duration % 3600) // 60, duration % 60)
else:
# time, in minutes:seconds
return "%s%02d:%02d" % (prefix, duration // 60, duration % 60)
# from quodlibet
def strduration_long (duration):
"""Turn a time value in seconds into x hours, x minutes, etc."""
if duration < 0:
duration = abs(duration)
prefix = "-"
else:
prefix = ""
if duration < 1:
return _("%s%.02f seconds") % (prefix, duration)
# translation dummies
_n("%d second", "%d seconds", 1)
_n("%d minute", "%d minutes", 1)
_n("%d hour", "%d hours", 1)
_n("%d day", "%d days", 1)
_n("%d year", "%d years", 1)
cutoffs = [
(60, "%d second", "%d seconds"),
(60, "%d minute", "%d minutes"),
(24, "%d hour", "%d hours"),
(365, "%d day", "%d days"),
(None, "%d year", "%d years"),
]
time_str = []
for divisor, single, plural in cutoffs:
if duration < 1:
break
if divisor is None:
duration, unit = 0, duration
else:
duration, unit = divmod(duration, divisor)
if unit:
time_str.append(_n(single, plural, unit) % unit)
time_str.reverse()
if len(time_str) > 2:
time_str.pop()
return "%s%s" % (prefix, ", ".join(time_str))
def strtimezone ():

View file

@ -124,17 +124,37 @@ class TestStrFormat (unittest.TestCase):
self.assertEqual(linkcheck.strformat.limit("1", 1), "1")
self.assertEqual(linkcheck.strformat.limit("11", 1), "1...")
def test_time (self):
def test_strtime (self):
zone = linkcheck.strformat.strtimezone()
t = linkcheck.strformat.strtime(0)
self.assertEqual(t, "1970-01-01 01:00:00"+zone)
def test_duration (self):
strduration = linkcheck.strformat.strduration
self.assertEqual(strduration(0), "0.000 seconds")
self.assertEqual(strduration(1), "1.000 seconds")
self.assertEqual(strduration(120), "2.000 minutes")
self.assertEqual(strduration(60*60), "1.000 hours")
duration = linkcheck.strformat.strduration
self.assertEqual(duration(-0.5), "-00:01")
self.assertEqual(duration(0), "00:00")
self.assertEqual(duration(0.9), "00:01")
self.assertEqual(duration(1), "00:01")
self.assertEqual(duration(2), "00:02")
self.assertEqual(duration(60), "01:00")
self.assertEqual(duration(120), "02:00")
self.assertEqual(duration(60*60), "01:00:00")
self.assertEqual(duration(60*60*24), "24:00:00")
def test_duration_long (self):
duration = linkcheck.strformat.strduration_long
self.assertEqual(duration(-0.5), "-0.50 seconds")
self.assertEqual(duration(0), "0.00 seconds")
self.assertEqual(duration(0.9), "0.90 seconds")
self.assertEqual(duration(1), "1 second")
self.assertEqual(duration(2), "2 seconds")
self.assertEqual(duration(60), "1 minute")
self.assertEqual(duration(120), "2 minutes")
self.assertEqual(duration(60*60), "1 hour")
self.assertEqual(duration(60*60*24), "1 day")
self.assertEqual(duration(60*60*24*365), "1 year")
self.assertEqual(duration(60*60*24*365 + 60*60*24 + 2),
"1 year, 1 day")
def test_linenumber (self):
get_line_number = linkcheck.strformat.get_line_number