]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
The `format_timedelta` function now returns, for example, “1 day” instead of “0 days...
authorChristopher Lenz <cmlenz@gmail.com>
Tue, 15 Jul 2008 16:02:17 +0000 (16:02 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Tue, 15 Jul 2008 16:02:17 +0000 (16:02 +0000)
ChangeLog
babel/dates.py
babel/tests/dates.py
doc/dates.txt

index 72086c4d43da992a072ed3c00e46fbb18f877dc0..d0e9c6edaded93e0fe8875c4578602bdcb2d3c32 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Version 1.0
+http://svn.edgewall.org/repos/babel/tags/1.0.0/
+(???, from branches/stable/1.0.x)
+
+ * Added support for the locale plural rules defined by the CLDR.
+ * Added `format_timedelta` function to support localized formatting of
+   relative times with strings such as "2 days" or "1 month" (ticket #126).
+
+
 Version 0.9.3
 http://svn.edgewall.org/repos/babel/tags/0.9.3/
 (Jul 9 2008, from branches/stable/0.9.x)
index 04557875d8a59bcf15dc5b11388cf2d68d1f3a6a..dc602099b5263fd4135bfc9eb2f3421b85edac15 100644 (file)
@@ -613,7 +613,7 @@ def format_timedelta(delta, granularity='second', threshold=.85, locale=LC_TIME)
     
     >>> format_timedelta(timedelta(hours=3), granularity='day',
     ...                  locale='en_US')
-    u'0 days'
+    u'1 day'
 
     The threshold parameter can be used to determine at which value the
     presentation switches to the next higher unit. A higher threshold factor
@@ -643,6 +643,8 @@ def format_timedelta(delta, granularity='second', threshold=.85, locale=LC_TIME)
     for unit, secs_per_unit in TIMEDELTA_UNITS:
         value = abs(seconds) / secs_per_unit
         if value >= threshold or unit == granularity:
+            if unit == granularity and value > 0:
+                value = max(1, value)
             value = int(round(value))
             plural_form = locale.plural_form(value)
             pattern = locale._data['unit_patterns'][unit][plural_form]
index 5ccecaeb73311629043e944a8ffce6a0bb5403d1..5f6abf1e29865d13db3ff9abf15b6e77fda0baa4 100644 (file)
@@ -238,6 +238,21 @@ class FormatTimeTestCase(unittest.TestCase):
                           "yyyy-MM-dd HH:mm", locale='en_US')
 
 
+class FormatTimedeltaTestCase(unittest.TestCase):
+
+    def test_zero_seconds(self):
+        string = dates.format_timedelta(timedelta(seconds=0), locale='en')
+        self.assertEqual('0 seconds', string)
+        string = dates.format_timedelta(timedelta(seconds=0),
+                                        granularity='hour', locale='en')
+        self.assertEqual('0 hours', string)
+
+    def test_small_value_with_granularity(self):
+        string = dates.format_timedelta(timedelta(seconds=42),
+                                        granularity='hour', locale='en')
+        self.assertEqual('1 hour', string)
+
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(dates))
@@ -246,5 +261,6 @@ def suite():
     suite.addTest(unittest.makeSuite(FormatTimeTestCase))
     return suite
 
+
 if __name__ == '__main__':
     unittest.main(defaultTest='suite')
index 515f0fc34287b50a91924dd647400c018e9ab400..6416cf9c9cf871c48d97edc4e93d989eb35e2ac2 100644 (file)
@@ -234,7 +234,7 @@ can limit the smallest unit to display:
     >>> format_timedelta(delta, threshold=1.2, locale='en_US')
     u'6 days'
     >>> format_timedelta(delta, granularity='month', locale='en_US')
-    u'0 months'
+    u'1 month'
 
 
 Time-zone Support