def get_period_names(self): """Return the names for day periods (AM/PM) >>> Locale('en', 'US').get_period_names()['am'] u'AM' """ return dates.get_period_names(locale=self)
def _i18n_parse_date_pattern(locale): format_keys = { 'y': ('y', 'Y'), 'M': ('M',), 'd': ('d',), 'h': ('h', 'H'), 'm': ('m',), 's': ('s',), } regexp = [r'[0-9]+'] date_format = get_date_format('medium', locale=locale) time_format = get_time_format('medium', locale=locale) datetime_format = get_datetime_format('medium', locale=locale) formats = ( datetime_format.replace('{0}', time_format.format) \ .replace('{1}', date_format.format), date_format.format) orders = [] for format in formats: order = [] for key, chars in format_keys.iteritems(): for char in chars: idx = format.find('%(' + char) if idx != -1: order.append((idx, key)) break order.sort() order = dict((key, idx) for idx, (_, key) in enumerate(order)) orders.append(order) month_names = { 'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6, 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12, } if formats[0].find('%(MMM)s') != -1: for width in ('wide', 'abbreviated'): names = get_month_names(width, locale=locale) for num, name in names.iteritems(): name = name.lower() month_names[name] = num regexp.extend(month_names.iterkeys()) period_names = {'am': 'am', 'pm': 'pm'} if formats[0].find('%(a)s') != -1: names = get_period_names(locale=locale) for period, name in names.iteritems(): if period in ('am', 'pm'): name = name.lower() period_names[name] = period regexp.extend(period_names.iterkeys()) return { 'orders': orders, 'regexp': re.compile('(%s)' % '|'.join(regexp), re.IGNORECASE | re.UNICODE), 'month_names': month_names, 'period_names': period_names, }
def _i18n_parse_date_pattern(locale): format_keys = { 'y': ('y', 'Y'), 'M': ('M',), 'd': ('d',), 'h': ('h', 'H'), 'm': ('m',), 's': ('s',), } regexp = [r'[0-9]+'] date_format = get_date_format('medium', locale=locale) time_format = get_time_format('medium', locale=locale) datetime_format = get_datetime_format('medium', locale=locale) formats = ( datetime_format.replace('{0}', time_format.format) \ .replace('{1}', date_format.format), date_format.format) orders = [] for format in formats: order = [] for key, chars in format_keys.iteritems(): for char in chars: idx = format.find('%(' + char) if idx != -1: order.append((idx, key)) break order.sort() order = dict((key, idx) for idx, (_, key) in enumerate(order)) orders.append(order) month_names = { 'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6, 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12, } if formats[0].find('%(MMM)s') != -1: for width in ('wide', 'abbreviated'): names = get_month_names(width, locale=locale) for num, name in names.iteritems(): name = name.lower() month_names[name] = num regexp.extend(month_names.iterkeys()) period_names = {'am': 'am', 'pm': 'pm'} if formats[0].find('%(a)s') != -1: names = get_period_names(locale=locale) for period, name in names.iteritems(): name = name.lower() period_names[name] = period regexp.extend(period_names.iterkeys()) return { 'orders': orders, 'regexp': re.compile('(%s)' % '|'.join(regexp), re.IGNORECASE | re.UNICODE), 'month_names': month_names, 'period_names': period_names, }
def get_period_names(self, width='wide', context='stand-alone'): """Return the names for day periods (AM/PM) In: - ``width`` -- 'abbreviated', 'narrow' or 'wide' - ``context`` -- 'format' or 'stand-alone' Return: - the names for day periods (AM/PM) """ return dates.get_period_names(width, context, locale=self)
def get_period_names_jquery_ui(req): # allow to use always English am/pm markers english_names = {'am': 'AM', 'pm': 'PM'} locale = req.lc_time if locale == 'iso8601': return {'am': [english_names['am']], 'pm': [english_names['pm']]} if babel and locale: names = get_period_names(locale=locale) return dict((period, [names[period], english_names[period]]) for period in ('am', 'pm')) else: # retrieve names of am/pm from libc names = {} for period, hour in (('am', 11), ('pm', 23)): t = datetime(1999, 10, 29, hour, tzinfo=utc) names[period] = [format_datetime(t, '%p', tzinfo=utc), english_names[period]] return names
def test_get_period_names(): assert dates.get_period_names(locale='en_US')['am'] == u'AM'
def _i18n_parse_date_pattern(locale): format_keys = { 'y': ('y', 'Y'), 'M': ('M',), 'd': ('d',), 'h': ('h', 'H'), 'm': ('m',), 's': ('s',), } if locale is None: formats = (_libc_get_datetime_format_hint(format=True), _libc_get_date_format_hint(format=True)) else: date_format = get_date_format('medium', locale=locale) time_format = get_time_format('medium', locale=locale) datetime_format = get_datetime_format('medium', locale=locale) formats = (datetime_format.replace('{0}', time_format.format) \ .replace('{1}', date_format.format), date_format.format) orders = [] for format in formats: order = [] for key, chars in format_keys.iteritems(): for char in chars: idx = format.find('%(' + char) if idx != -1: order.append((idx, key)) break order.sort() orders.append(dict((key, idx) for idx, (_, key) in enumerate(order))) # always allow using English names regardless of locale month_names = dict(zip(('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec',), xrange(1, 13))) period_names = {'am': 'am', 'pm': 'pm'} if locale is None: for num in xrange(1, 13): t = datetime(1999, num, 1, tzinfo=utc) names = format_date(t, '%b\t%B', utc).split('\t') month_names.update((name.lower(), num) for name in names if str(num) not in name) for num, period in ((11, 'am'), (23, 'pm')): t = datetime(1999, 1, 1, num, tzinfo=utc) name = format_datetime(t, '%p', utc) if name: period_names[name.lower()] = period else: if formats[0].find('%(MMM)s') != -1: for width in ('wide', 'abbreviated'): names = get_month_names(width, locale=locale) month_names.update((name.lower(), num) for num, name in names.iteritems()) if formats[0].find('%(a)s') != -1: names = get_period_names(locale=locale) period_names.update((name.lower(), period) for period, name in names.iteritems() if period in ('am', 'pm')) regexp = ['[0-9]+'] regexp.extend(re.escape(name) for name in month_names) regexp.extend(re.escape(name) for name in period_names) return { 'orders': orders, 'regexp': re.compile('(%s)' % '|'.join(regexp), re.IGNORECASE), 'month_names': month_names, 'period_names': period_names, }