]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
dates: Add additional pattern for week day 347/head
authorSachin Paliwal <sachin.pali146@gmail.com>
Tue, 1 Mar 2016 14:21:10 +0000 (19:51 +0530)
committerSachin Paliwal <sachin.pali146@gmail.com>
Tue, 1 Mar 2016 14:21:10 +0000 (19:51 +0530)
Added test cases and additional pattern for Weekday format

babel/dates.py
tests/test_dates.py

index 6e45d92fd77a54b65681fdd99d11d143ba220195..2d94f893b363896a896d43eb1d8a3de1dfd82e7d 100644 (file)
@@ -263,12 +263,14 @@ def get_day_names(width='wide', context='format', locale=LC_TIME):
 
     >>> get_day_names('wide', locale='en_US')[1]
     u'Tuesday'
+    >>> get_day_names('short', locale='en_US')[1]
+    u'Tu'
     >>> get_day_names('abbreviated', locale='es')[1]
     u'mar.'
     >>> get_day_names('narrow', context='stand-alone', locale='de_DE')[1]
     u'D'
 
-    :param width: the width to use, one of "wide", "abbreviated", or "narrow"
+    :param width: the width to use, one of "wide", "abbreviated", "short" or "narrow"
     :param context: the context, either "format" or "stand-alone"
     :param locale: the `Locale` object, or a locale string
     """
@@ -1267,15 +1269,44 @@ class DateTimeFormat(object):
                 week = self.get_week_number(date.day, date.weekday())
             return '%d' % week
 
-    def format_weekday(self, char, num):
+    def format_weekday(self, char='E', num=4):
+        """
+        Return weekday from parsed datetime according to format pattern.
+
+        >>> format = DateTimeFormat(date(2016, 2, 28), Locale.parse('en_US'))
+        >>> format.format_weekday()
+        u'Sunday'
+
+        'E': Day of week - Use one through three letters for the abbreviated day name, four for the full (wide) name,
+             five for the narrow name, or six for the short name.
+        >>> format.format_weekday('E',2)
+        u'Sun'
+
+        'e': Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the
+             week, using one or two letters. For this example, Monday is the first day of the week.
+        >>> format.format_weekday('e',2)
+        '01'
+
+        'c': Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the
+             abbreviated day name, four for the full (wide) name, five for the narrow name, or six for the short name.
+        >>> format.format_weekday('c',1)
+        '1'
+
+        :param char: pattern format character ('e','E','c')
+        :param num: count of format character
+
+        """
         if num < 3:
             if char.islower():
                 value = 7 - self.locale.first_week_day + self.value.weekday()
                 return self.format(value % 7 + 1, num)
             num = 3
         weekday = self.value.weekday()
-        width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num]
-        context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num]
+        width = {3: 'abbreviated', 4: 'wide', 5: 'narrow', 6: 'short'}[num]
+        if char == 'c':
+            context = 'stand-alone'
+        else:
+            context = 'format'
         return get_day_names(width, context, self.locale)[weekday]
 
     def format_day_of_year(self, num):
index 7d3236ace0264da3fad944e752c1d8ab1605bcdc..ee73d9db94b2214674c604f41a3339108502c44e 100644 (file)
@@ -160,6 +160,32 @@ class DateTimeFormatTestCase(unittest.TestCase):
         fmt = dates.DateTimeFormat(d, locale='bn_BD')
         self.assertEqual('4', fmt['c']) # friday is first day of week
 
+    def test_pattern_day_of_week(self):
+        dt = datetime(2016,2,6)
+        fmt = dates.DateTimeFormat(dt, locale='en_US')
+        self.assertEqual('7', fmt['c'])
+        self.assertEqual('Sat', fmt['ccc'])
+        self.assertEqual('Saturday', fmt['cccc'])
+        self.assertEqual('S', fmt['ccccc'])
+        self.assertEqual('Sa', fmt['cccccc'])
+        self.assertEqual('7', fmt['e'])
+        self.assertEqual('07', fmt['ee'])
+        self.assertEqual('Sat', fmt['eee'])
+        self.assertEqual('Saturday', fmt['eeee'])
+        self.assertEqual('S', fmt['eeeee'])
+        self.assertEqual('Sa', fmt['eeeeee'])
+        self.assertEqual('Sat', fmt['E'])
+        self.assertEqual('Sat', fmt['EE'])
+        self.assertEqual('Sat', fmt['EEE'])
+        self.assertEqual('Saturday', fmt['EEEE'])
+        self.assertEqual('S', fmt['EEEEE'])
+        self.assertEqual('Sa', fmt['EEEEEE'])
+        fmt = dates.DateTimeFormat(dt, locale='uk')
+        self.assertEqual('6', fmt['c'])
+        self.assertEqual('6', fmt['e'])
+        self.assertEqual('06', fmt['ee'])
+
+
     def test_fractional_seconds(self):
         t = time(8, 3, 9, 799)
         fmt = dates.DateTimeFormat(t, locale='en_US')
@@ -491,6 +517,7 @@ def test_get_period_names():
 
 def test_get_day_names():
     assert dates.get_day_names('wide', locale='en_US')[1] == u'Tuesday'
+    assert dates.get_day_names('short', locale='en_US')[1] == u'Tu'
     assert dates.get_day_names('abbreviated', locale='es')[1] == u'mar.'
     de = dates.get_day_names('narrow', context='stand-alone', locale='de_DE')
     assert de[1] == u'D'