Пример #1
0
def get_datetime_format(kind="datetime"):
    """Get local datetime format.

    @param kind: type (date, time or datetime)
    @return: string

    @todo: cache formats to improve performance.
    """
    if 'lang' in cherrypy.session:
        # server-defined formatting
        if kind == 'time':
            return cherrypy.session['lang']['time_format']
        elif kind == 'date':
            return cherrypy.session['lang']['date_format']
        else:
            return "%(date_format)s %(time_format)s"% cherrypy.session['lang']

    # TODO: correctly convert from LDML to POSIX datetime formatting
    # current converter is trivial and lame and probably very easy to break
    date_format = _to_posix_format(dates.get_date_format(
            format='short', locale=get_locale())).format
    if kind == 'time':
        # Should use dates.get_time_format(locale=get_locale())
        return '%H:%M:%S'
    elif kind == 'date':
        return date_format
    else:
        # Should use dates.get_datetime_format, but that one returns
        # a 2.6-style formats
        return "%s %s" % (date_format, '%H:%M:%S')
Пример #2
0
def get_datetime_format(kind="datetime"):
    """Get local datetime format.

    @param kind: type (date, time or datetime)
    @return: string

    @todo: cache formats to improve performance.
    """
    if 'lang' in cherrypy.session:
        # server-defined formatting
        if kind == 'time':
            return cherrypy.session['lang']['time_format']
        elif kind == 'date':
            return cherrypy.session['lang']['date_format']
        else:
            return "%(date_format)s %(time_format)s" % cherrypy.session['lang']

    # TODO: correctly convert from LDML to POSIX datetime formatting
    # current converter is trivial and lame and probably very easy to break
    date_format = _to_posix_format(
        dates.get_date_format(format='short', locale=get_locale())).format
    if kind == 'time':
        # Should use dates.get_time_format(locale=get_locale())
        return '%H:%M:%S'
    elif kind == 'date':
        return date_format
    else:
        # Should use dates.get_datetime_format, but that one returns
        # a 2.6-style formats
        return "%s %s" % (date_format, '%H:%M:%S')
Пример #3
0
def format_decimal(value, digits=2):
    locale = get_locale()
    v = ("%%.%df" % digits) % value
    if not digits:
        return numbers.format_number(value, locale=locale)
    num, decimals = v.split(".", 1)

    if num == "-0":
        val = "-0"
    else:
        val = numbers.format_number(int(num), locale=locale)

    return val + unicode(numbers.get_decimal_symbol(locale) + decimals)
Пример #4
0
def parse_decimal(value):
    
    locale = cherrypy.session['lang'].get('code') or get_locale()
    if isinstance(value, basestring):
        
        value = ustr(value)
        
        grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False)
        #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char)
        value = value.replace(' ', '')
        value = value.replace(thousands_sep, '').replace(decimal_point, '.')

    if not isinstance(value, float):
        return float(value)

    return value
Пример #5
0
def parse_decimal(value):

    if isinstance(value, basestring):

        value = ustr(value)

        #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char)
        value = value.replace(' ', '')
        try:
            value = numbers.parse_decimal(value, locale=get_locale())
        except ValueError:
            pass

    if not isinstance(value, float):
        return float(value)

    return value
Пример #6
0
def parse_decimal(value):

    locale = cherrypy.session['lang'].get('code') or get_locale()
    if isinstance(value, basestring):

        value = ustr(value)

        grouping, thousands_sep, decimal_point = get_lang_float_format(
            locale, monetary=False)
        #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char)
        value = value.replace(' ', '')
        value = value.replace(thousands_sep, '').replace(decimal_point, '.')

    if not isinstance(value, float):
        return float(value)

    return value
Пример #7
0
def format_decimal(value, digits=2, grouping=True, monetary=False):
    locale = cherrypy.session['lang'].get('code') or get_locale()
    formatted = ("%%.%df" % digits) % value
    lang_grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False)
    
    seps = 0
    parts = formatted.split('.')

    if grouping:
        parts[0], seps = group(parts[0], monetary=monetary, grouping=lang_grouping, thousands_sep=thousands_sep)

    formatted = decimal_point.join(parts)
    while seps:
        sp = formatted.find(' ')
        if sp == -1: break
        formatted = formatted[:sp] + formatted[sp+1:]
        seps -= 1
    return formatted
Пример #8
0
def _gettext(key, locale=None, domain=None):
    """Get the gettext value for key.

    Added to builtins as '_'. Returns Unicode string.

    @param key: text to be translated
    @param locale: locale code to be used.
        If locale is None, gets the value provided by get_locale.

    """
    if locale is None:
        locale = get_locale()
    elif not isinstance(locale, babel.Locale):
        locale = babel.Locale.parse(locale)
    if key == "":
        return ""  # special case
    try:
        return get_translations(locale, domain).ugettext(key)
    except KeyError:
        return key
Пример #9
0
def _gettext(key, locale=None, domain=None):
    """Get the gettext value for key.

    Added to builtins as '_'. Returns Unicode string.

    @param key: text to be translated
    @param locale: locale code to be used.
        If locale is None, gets the value provided by get_locale.

    """
    if locale is None:
        locale = get_locale()
    elif not isinstance(locale, babel.Locale):
        locale = babel.Locale.parse(locale)
    if key == '':
        return ''  # special case
    try:
        return get_translations(locale, domain).ugettext(key)
    except KeyError:
        return key
Пример #10
0
def format_decimal(value, digits=2, grouping=True, monetary=False):
    locale = cherrypy.session['lang'].get('code') or get_locale()
    formatted = ("%%.%df" % digits) % value
    lang_grouping, thousands_sep, decimal_point = get_lang_float_format(
        locale, monetary=False)

    seps = 0
    parts = formatted.split('.')

    if grouping:
        parts[0], seps = group(parts[0],
                               monetary=monetary,
                               grouping=lang_grouping,
                               thousands_sep=thousands_sep)

    formatted = decimal_point.join(parts)
    while seps:
        sp = formatted.find(' ')
        if sp == -1: break
        formatted = formatted[:sp] + formatted[sp + 1:]
        seps -= 1
    return formatted
Пример #11
0
def format_date_custom(dt, fmt="y-M-d"):
    return dates.format_date(dt, format=fmt, locale=get_locale())
Пример #12
0
def format_date_custom(dt, fmt="y-M-d"):
    return dates.format_date(dt, format=fmt, locale=get_locale())