From: Sachin Paliwal Date: Tue, 1 Mar 2016 14:21:10 +0000 (+0530) Subject: dates: Add additional pattern for week day X-Git-Tag: 2.3.1~10^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d93b227de7b01320a05196968f0d4ed008ff820;p=thirdparty%2Fbabel.git dates: Add additional pattern for week day Added test cases and additional pattern for Weekday format --- diff --git a/babel/dates.py b/babel/dates.py index 6e45d92f..2d94f893 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -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): diff --git a/tests/test_dates.py b/tests/test_dates.py index 7d3236ac..ee73d9db 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -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'