Exemple #1
0
def datetime_fmt_tpl(ctx, fmt='medium'):
    loc = ctx.get('i18n', None)
    if loc:
        return get_datetime_format(fmt, loc).format(
            get_time_format(fmt, loc).pattern,
            get_date_format(fmt, loc).pattern)
    return get_datetime_format(fmt).format(
        get_time_format(fmt).pattern,
        get_date_format(fmt).pattern)
Exemple #2
0
def datetime_fmt_tpl(ctx, fmt='medium'):
	loc = ctx.get('i18n', None)
	if loc:
		return get_datetime_format(fmt, loc).format(
			get_time_format(fmt, loc).pattern,
			get_date_format(fmt, loc).pattern
		)
	return get_datetime_format(fmt).format(
		get_time_format(fmt).pattern,
		get_date_format(fmt).pattern
	)
Exemple #3
0
    def format(self, locale):
        if isinstance(self, datetime):
            selftz = _ensure_datetime_tzinfo(self,
                                             tzinfo=self.options.timeZone)
        else:
            selftz = self

        if self.options.dateStyle is None and self.options.timeStyle is None:
            return format_date(selftz, format='medium', locale=locale)
        elif self.options.dateStyle is None and self.options.timeStyle is not None:
            return format_time(selftz,
                               format=self.options.timeStyle,
                               locale=locale)
        elif self.options.dateStyle is not None and self.options.timeStyle is None:
            return format_date(selftz,
                               format=self.options.dateStyle,
                               locale=locale)
        else:
            # Both date and time. Logic copied from babel.dates.format_datetime,
            # with modifications.
            # Which datetime format do we pick? We arbitrarily pick dateStyle.

            return (get_datetime_format(
                self.options.dateStyle,
                locale=locale).replace("'", "").replace(
                    '{0}',
                    format_time(selftz,
                                self.options.timeStyle,
                                tzinfo=None,
                                locale=locale)).replace(
                                    '{1}',
                                    format_date(selftz,
                                                self.options.dateStyle,
                                                locale=locale)))
Exemple #4
0
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,
    }
Exemple #5
0
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,
    }
Exemple #6
0
def get_timepicker_separator_jquery_ui(req):
    locale = req.lc_time
    if locale == 'iso8601':
        return 'T'
    if babel and locale:
        return get_datetime_format('medium', locale=locale) \
               .replace('{0}', '').replace('{1}', '')
    return ' '
Exemple #7
0
 def format_13(self, value, locale):
     timestamp = isoparse(value)
     # precision: minute
     return get_datetime_format(format, locale=locale) \
         .replace("'", "") \
         .replace('{0}', format_time(timestamp, 'full', tzinfo=None,
                                     locale=locale)) \
         .replace('{1}', format_date(timestamp, 'short', locale=locale))
Exemple #8
0
def get_timepicker_separator_jquery_ui(req):
    locale = req.lc_time
    if locale == 'iso8601':
        return 'T'
    if babel and locale:
        return get_datetime_format('medium', locale=locale) \
               .replace('{0}', '').replace('{1}', '')
    return ' '
Exemple #9
0
    def get_datetime_format(self, format='medium'):
        """Return the datetime formatting pattern for the specified format

        In:
          - ``format`` -- 'full', 'long', 'medium' or 'short'

        Return:
          - the datetime formatting pattern
        """
        return dates.get_datetime_format(format, self)
Exemple #10
0
    def get_datetime_format(self, format='medium'):
        """Return the datetime formatting pattern for the specified format

        >>> Locale('en', 'US').get_datetime_format()
        u'{1} {0}'

        In:
          - ``format`` -- 'full', 'long', 'medium' or 'short'

        Return:
          - the datetime formatting pattern
        """
        return dates.get_datetime_format(format, self)
Exemple #11
0
def get_datetime_format_hint(locale=None):
    """Present the default format used by `format_datetime` in a human readable
    form.
    This is a format that will be recognized by `parse_date` when reading a
    date.
    """
    if locale == 'iso8601':
        return u'YYYY-MM-DDThh:mm:ss±hh:mm'
    if babel and locale:
        date_pattern = get_date_format('medium', locale=locale).pattern
        time_pattern = get_time_format('medium', locale=locale).pattern
        format = get_datetime_format('medium', locale=locale)
        return format.replace('{0}', time_pattern) \
                     .replace('{1}', date_pattern)
    return _libc_get_datetime_format_hint()
Exemple #12
0
def get_datetime_format_hint(locale=None):
    """Present the default format used by `format_datetime` in a human readable
    form.
    This is a format that will be recognized by `parse_date` when reading a
    date.
    """
    if locale == 'iso8601':
        return u'YYYY-MM-DDThh:mm:ss±hh:mm'
    if babel and locale:
        date_pattern = get_date_format('medium', locale=locale).pattern
        time_pattern = get_time_format('medium', locale=locale).pattern
        format = get_datetime_format('medium', locale=locale)
        return format.replace('{0}', time_pattern) \
                     .replace('{1}', date_pattern)
    return _libc_get_datetime_format_hint()
Exemple #13
0
def get_datetime_format_hint(locale=None):
    """Present the default format used by `format_datetime` in a human readable
    form.
    This is a format that will be recognized by `parse_date` when reading a
    date.
    """
    if locale == 'iso8601':
        return u'YYYY-MM-DDThh:mm:ss±hh:mm'
    if babel and locale:
        date_pattern = get_date_format('medium', locale=locale).pattern
        time_pattern = get_time_format('medium', locale=locale).pattern
        format = get_datetime_format('medium', locale=locale)
        return format.replace('{0}', time_pattern) \
                     .replace('{1}', date_pattern)

    t = datetime(1999, 10, 29, 23, 59, 58, tzinfo=utc)
    tmpl = format_datetime(t, tzinfo=utc)
    return tmpl.replace('1999', 'YYYY', 1).replace('99', 'YY', 1) \
               .replace('10', 'MM', 1).replace('29', 'DD', 1) \
               .replace('23', 'hh', 1).replace('11', 'hh', 1) \
               .replace('59', 'mm', 1).replace('58', 'ss', 1)
Exemple #14
0
def get_datetime_format_hint(locale=None):
    """Present the default format used by `format_datetime` in a human readable
    form.
    This is a format that will be recognized by `parse_date` when reading a
    date.
    """
    if locale == 'iso8601':
        return u'YYYY-MM-DDThh:mm:ss±hh:mm'
    if babel and locale:
        date_pattern = get_date_format('medium', locale=locale).pattern
        time_pattern = get_time_format('medium', locale=locale).pattern
        format = get_datetime_format('medium', locale=locale)
        return format.replace('{0}', time_pattern) \
                     .replace('{1}', date_pattern)

    t = datetime(1999, 10, 29, 23, 59, 58, tzinfo=utc)
    tmpl = format_datetime(t, tzinfo=utc)
    return tmpl.replace('1999', 'YYYY', 1).replace('99', 'YY', 1) \
               .replace('10', 'MM', 1).replace('29', 'DD', 1) \
               .replace('23', 'hh', 1).replace('11', 'hh', 1) \
               .replace('59', 'mm', 1).replace('58', 'ss', 1)
Exemple #15
0
def test_get_datetime_format():
    assert dates.get_datetime_format(locale='en_US') == u'{1}, {0}'
Exemple #16
0
 def datetime_format(format='medium', locale=LC_TIME):
     time_format = unicode(get_time_format(format, locale))
     date_format = unicode(get_date_format(format, locale))
     return convert_LDML_to_MySQL( get_datetime_format(format, locale)\
             .replace('{0}', time_format)\
             .replace('{1}', date_format) )
Exemple #17
0
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,
    }
Exemple #18
0
def test_get_datetime_format():
    assert dates.get_datetime_format(locale='en_US') == u'{1}, {0}'
Exemple #19
0
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({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,
    }
Exemple #20
0
 def datetime_format(format='medium', locale=LC_TIME):
     time_format = unicode(get_time_format(format, locale))
     date_format = unicode(get_date_format(format, locale))
     return convert_LDML_to_MySQL( get_datetime_format(format, locale)\
             .replace('{0}', time_format)\
             .replace('{1}', date_format) )
Exemple #21
0
--- tracwatchlist/util.py.orig	2013-08-15 04:15:56.000000000 +0800
+++ tracwatchlist/util.py	2013-08-15 04:16:05.000000000 +0800
@@ -198,8 +198,8 @@
 try:
     from  babel.dates        import  get_datetime_format, get_date_format, get_time_format
     def datetime_format(format='medium', locale=LC_TIME):
-        time_format = unicode(get_time_format(format, locale))
-        date_format = unicode(get_date_format(format, locale))
+        time_format = unicode(get_time_format(format, locale=locale))
+        date_format = unicode(get_date_format(format, locale=locale))
         return convert_LDML_to_MySQL( get_datetime_format(format, locale)\
                 .replace('{0}', time_format)\
                 .replace('{1}', date_format) )