]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Add format_timedelta(format='narrow') support
authorAlex Willmer <alex@moreati.org.uk>
Thu, 10 Sep 2015 00:41:15 +0000 (01:41 +0100)
committerAlex Willmer <alex@moreati.org.uk>
Thu, 10 Sep 2015 10:25:42 +0000 (11:25 +0100)
babel/dates.py
tests/test_dates.py

index 388da6d25a704bddfee49e2246128319c999967a..f9498d80a6c654ff02b2b58dfed17f297705dcb1 100644 (file)
@@ -739,6 +739,13 @@ def format_timedelta(delta, granularity='second', threshold=.85,
     >>> format_timedelta(timedelta(hours=-1), add_direction=True, locale='en')
     u'1 hour ago'
 
+    The format parameter controls how compact or wide the presentation is:
+
+    >>> format_timedelta(timedelta(hours=3), format='short', locale='en')
+    u'3 hrs'
+    >>> format_timedelta(timedelta(hours=3), format='narrow', locale='en')
+    u'3h'
+
     :param delta: a ``timedelta`` object representing the time difference to
                   format, or the delta in seconds as an `int` value
     :param granularity: determines the smallest unit that should be displayed,
@@ -751,13 +758,13 @@ def format_timedelta(delta, granularity='second', threshold=.85,
                           positive timedelta will include the information about
                           it being in the future, a negative will be information
                           about the value being in the past.
-    :param format: the format (currently only "long" and "short" are supported,
+    :param format: the format, can be "narrow", "short" or "long". (
                    "medium" is deprecated, currently converted to "long" to
                    maintain compatibility)
     :param locale: a `Locale` object or a locale identifier
     """
-    if format not in ('short', 'medium', 'long'):
-        raise TypeError('Format can only be one of "short" or "medium"')
+    if format not in ('narrow', 'short', 'medium', 'long'):
+        raise TypeError('Format must be one of "narrow", "short" or "long"')
     if format == 'medium':
         warnings.warn('"medium" value for format param of format_timedelta'
                       ' is deprecated. Use "long" instead',
index 1f6e7e7dd130ea60c535b8aae55a98604bc25f86..34b8a89a233b2aa9dd44964a40991e0d7d81c19e 100644 (file)
@@ -310,6 +310,14 @@ class FormatTimedeltaTestCase(unittest.TestCase):
                                         add_direction=True)
         self.assertEqual('1 hour ago', string)
 
+    def test_format_narrow(self):
+        string = dates.format_timedelta(timedelta(hours=1),
+                                        locale='en', format='narrow')
+        self.assertEqual('1h', string)
+        string = dates.format_timedelta(timedelta(hours=-2),
+                                        locale='en', format='narrow')
+        self.assertEqual('2h', string)
+
 
 class TimeZoneAdjustTestCase(unittest.TestCase):
     def _utc(self):