Пример #1
0
def format_datetime(datetime=None, format='medium'):
    """Returns a datetime formated according to the given format in the context
    of current locale. If `datetime` is None, current time is assumed.

    :param datetime: an instance of :class:`~datetime.datetime` or None
    :param format: format pattern, one of `short`, `medium`, `long` or `full`.
    :returns: formated datetime as string
    """
    return dates.format_datetime(datetime, format=format, locale=get_locale())
Пример #2
0
def format_datetime(datetime=None, format='medium'):
    """Returns a datetime formated according to the given format in the context
    of current locale. If `datetime` is None, current time is assumed.

    :param datetime: an instance of :class:`~datetime.datetime` or None
    :param format: format pattern, one of `short`, `medium`, `long` or `full`.
    :returns: formated datetime as string
    """
    return dates.format_datetime(datetime, format=format, locale=get_locale())
Пример #3
0
def parse_decimal(string, force_decimal=False):
    """Parse a localized decimal string into a float if `force_decimal` is
    False, else into a real :class:`decimal.Decimal` value.

    :param string: localized decimal string value
    :param force_decimal: whether to return Decimal instead of float
    """
    locale = get_locale()
    sep = numbers.get_decimal_symbol(locale)
    num, decimals = string.rsplit(sep, 1)
    num = numbers.parse_number(num, locale=locale)
    string = '%s.%s' % (num, decimals)
    return Decimal(string) if force_decimal else float(string)
Пример #4
0
def parse_decimal(string, force_decimal=False):
    """Parse a localized decimal string into a float if `force_decimal` is
    False, else into a real :class:`decimal.Decimal` value.

    :param string: localized decimal string value
    :param force_decimal: whether to return Decimal instead of float
    """
    locale = get_locale()
    sep = numbers.get_decimal_symbol(locale)
    num, decimals = string.rsplit(sep, 1)
    num = numbers.parse_number(num, locale=locale)
    string = '%s.%s' % (num, decimals)
    return Decimal(string) if force_decimal else float(string)
Пример #5
0
def format_decimal(decimal, digits=2):
    """Returns a formatted decimal value in the context of current locale. The
    appropriate thousands grouping and the decimal separator are used according
    to the current locale.

    For example::

        >>> format_decimal(12345.4321, digits=2)
        ... '12,345.43'
        >>> format_decimal(Decimal('12345.4321'), digits=2)
        ... '12,345.43'


    :param decimal: a float or Decimal value
    :param digits: number of digits to the right of decimal seperator
    """
    locale = get_locale()
    value = ('%%.%df' % digits) % decimal
    num, decimals = value.split('.')
    num = numbers.format_number(int(num), locale=locale)
    if digits == 0:
        return num
    return num + numbers.get_decimal_symbol(locale) + decimals
Пример #6
0
def format_decimal(decimal, digits=2):
    """Returns a formatted decimal value in the context of current locale. The
    appropriate thousands grouping and the decimal separator are used according
    to the current locale.

    For example::

        >>> format_decimal(12345.4321, digits=2)
        ... '12,345.43'
        >>> format_decimal(Decimal('12345.4321'), digits=2)
        ... '12,345.43'


    :param decimal: a float or Decimal value
    :param digits: number of digits to the right of decimal seperator
    """
    locale = get_locale()
    value = ('%%.%df' % digits) % decimal
    num, decimals = value.split('.')
    num = numbers.format_number(int(num), locale=locale)
    if digits == 0:
        return num
    return num + numbers.get_decimal_symbol(locale) + decimals
Пример #7
0
def parse_datetime(string):
    """Parse a datetime from a string.
    """
    return dates.parse_datetime(string, locale=get_locale())
Пример #8
0
def format_number(number):
    """Returns a formatted decimal value in the context of current locale.

    :param number: an integer value
    """
    return dates.format_number(number, locale=get_locale())
Пример #9
0
def parse_number(string):
    """Parse a localized number string into a long integer.
    """
    return numbers.parse_number(string, locale=get_locale())
Пример #10
0
def parse_datetime(string):
    """Parse a datetime from a string.
    """
    return dates.parse_datetime(string, locale=get_locale())
Пример #11
0
def format_number(number):
    """Returns a formatted decimal value in the context of current locale.

    :param number: an integer value
    """
    return dates.format_number(number, locale=get_locale())
Пример #12
0
def parse_number(string):
    """Parse a localized number string into a long integer.
    """
    return numbers.parse_number(string, locale=get_locale())