Beispiel #1
0
def _format_datetime(t, format, tzinfo, locale, hint):
    t = to_datetime(t, tzinfo or localtz)

    if format == 'iso8601':
        return _format_datetime_iso8601(t, 'long', hint)
    if format in ('iso8601date', 'iso8601time'):
        return _format_datetime_iso8601(t, 'long', format[7:])
    if locale == 'iso8601':
        if format is None:
            format = 'long'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'long'
        if format in ('short', 'medium', 'long', 'full'):
            return _format_datetime_iso8601(t, format, hint)
        return _format_datetime_without_babel(t, format)

    if babel and locale:
        if format is None:
            format = 'medium'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'medium'
        if format in ('short', 'medium', 'long', 'full'):
            if hint == 'datetime':
                return babel_format_datetime(t, format, None, locale)
            if hint == 'date':
                return babel_format_date(t, format, locale)
            if hint == 'time':
                return babel_format_time(t, format, None, locale)

    format = _BABEL_FORMATS[hint].get(format, format)
    return _format_datetime_without_babel(t, format)
Beispiel #2
0
def format_datetime(t=None, format='%x %X', tzinfo=None, locale=None):
    """Format the `datetime` object `t` into an `unicode` string

    If `t` is None, the current time will be used.
    
    The formatting will be done using the given `format`, which consist
    of conventional `strftime` keys. In addition the format can be 'iso8601'
    to specify the international date format (compliant with RFC 3339).

    `tzinfo` will default to the local timezone if left to `None`.
    """
    if locale == 'iso8601':
        format = _ISO8601_FORMATS['datetime'].get(format, format)
        return _format_datetime_without_babel(t, format, tzinfo)
    if babel and locale:
        if format == '%x':
            return babel_format_date(t, 'medium', locale)
        if format == '%X':
            t = to_datetime(t, tzinfo)
            return babel_format_time(t, 'medium', None, locale)
        if format in ('%x %X', None):
            format = 'medium'
        if format in _BABEL_FORMATS['datetime']:
            t = to_datetime(t, tzinfo)
            return babel_format_datetime(t, format, None, locale)
    format = _BABEL_FORMATS['datetime'].get(format, format)
    return _format_datetime_without_babel(t, format, tzinfo)
Beispiel #3
0
def format_datetime(t=None, format='%x %X', tzinfo=None, locale=None):
    """Format the `datetime` object `t` into an `unicode` string

    If `t` is None, the current time will be used.
    
    The formatting will be done using the given `format`, which consist
    of conventional `strftime` keys. In addition the format can be 'iso8601'
    to specify the international date format (compliant with RFC 3339).

    `tzinfo` will default to the local timezone if left to `None`.
    """
    if locale == 'iso8601':
        format = _ISO8601_FORMATS['datetime'].get(format, format)
        return _format_datetime_without_babel(t, format, tzinfo)
    if babel and locale:
        if format == '%x':
            return babel_format_date(t, 'medium', locale)
        if format == '%X':
            t = to_datetime(t, tzinfo)
            return babel_format_time(t, 'medium', None, locale)
        if format in ('%x %X', None):
            format = 'medium'
        if format in _BABEL_FORMATS['datetime']:
            t = to_datetime(t, tzinfo)
            return babel_format_datetime(t, format, None, locale)
    format = _BABEL_FORMATS['datetime'].get(format, format)
    return _format_datetime_without_babel(t, format, tzinfo)
Beispiel #4
0
def _format_datetime(t, format, tzinfo, locale, hint):
    t = to_datetime(t, tzinfo or localtz)

    if format == 'iso8601':
        return _format_datetime_iso8601(t, 'long', hint)
    if format in ('iso8601date', 'iso8601time'):
        return _format_datetime_iso8601(t, 'long', format[7:])
    if locale == 'iso8601':
        if format is None:
            format = 'long'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'long'
        if format in ('short', 'medium', 'long', 'full'):
            return _format_datetime_iso8601(t, format, hint)
        return _format_datetime_without_babel(t, format)

    if babel and locale:
        if format is None:
            format = 'medium'
        elif format in _STRFTIME_HINTS:
            hint = _STRFTIME_HINTS[format]
            format = 'medium'
        if format in ('short', 'medium', 'long', 'full'):
            if hint == 'datetime':
                return babel_format_datetime(t, format, None, locale)
            if hint == 'date':
                return babel_format_date(t, format, locale)
            if hint == 'time':
                return babel_format_time(t, format, None, locale)

    format = _BABEL_FORMATS[hint].get(format, format)
    return _format_datetime_without_babel(t, format)
Beispiel #5
0
def format_time(t=None, format='%X', tzinfo=None, locale=None):
    """Convenience method for formatting the time part of a `datetime` object.
    See `format_datetime` for more details.
    """
    if locale == 'iso8601':
        format = _ISO8601_FORMATS['time'].get(format, format)
        return _format_datetime_without_babel(t, format, tzinfo)
    if format == 'iso8601':
        format = 'iso8601time'
    if babel and locale:
        if format in ('%X', None):
            format = 'medium'
        if format in _BABEL_FORMATS['time']:
            t = to_datetime(t, tzinfo)
            return babel_format_time(t, format, None, locale)
    format = _BABEL_FORMATS['time'].get(format, format)
    return _format_datetime_without_babel(t, format, tzinfo)
Beispiel #6
0
def format_time(t=None, format='%X', tzinfo=None, locale=None):
    """Convenience method for formatting the time part of a `datetime` object.
    See `format_datetime` for more details.
    """
    if locale == 'iso8601':
        format = _ISO8601_FORMATS['time'].get(format, format)
        return _format_datetime_without_babel(t, format, tzinfo)
    if format == 'iso8601':
        format = 'iso8601time'
    if babel and locale:
        if format in ('%X', None):
            format = 'medium'
        if format in _BABEL_FORMATS['time']:
            t = to_datetime(t, tzinfo)
            return babel_format_time(t, format, None, locale)
    format = _BABEL_FORMATS['time'].get(format, format)
    return _format_datetime_without_babel(t, format, tzinfo)
Beispiel #7
0
def _format_html_time_tag(datetime, what_to_display):
    if what_to_display == "date-only":
        content = babel_format_date(datetime, locale=_get_user_locale())
    elif what_to_display == "time-only":
        content = babel_format_time(datetime,
                                    format="short",
                                    locale=_get_user_locale())
    elif what_to_display == "date-and-time":
        content = babel_format_datetime(datetime,
                                        tzinfo=UTC,
                                        locale=_get_user_locale())
        # While this could be done with a format string, that would
        # hinder internationalization and honestly why bother.
        content += " UTC"
    else:
        raise ValueError("what_to_display argument invalid")

    isoformat = datetime.isoformat()

    return Markup(
        '<time datetime="{}" data-what_to_display="{}">{}</time>'.format(
            isoformat, what_to_display, content, content))
Beispiel #8
0
def format_time(value, request, time_format='short'):
    localizer = get_localizer(request)
    return babel_format_time(value, time_format, locale=localizer.locale_name)