Пример #1
0
def format_datetime(dt, format='medium', locale=None, timezone=None):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    if not locale:
        locale = currentLocale()

    return _format_datetime(dt, format=format, locale=locale, tzinfo=timezone).encode('utf-8')
Пример #2
0
def format_datetime(datetime, format=MEDIUM):
    """Format datetime using current locale"""

    if not datetime:
        return datetime

    locale = get_current_locale_code()

    return _format_datetime(datetime, format=format, locale=locale)
Пример #3
0
def format_datetime(dt, format='medium', locale=None, timezone=None, server_tz=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    if not locale:
        locale = currentLocale()
    if not timezone and dt.tzinfo:
        timezone = DisplayTZ().getDisplayTZ()
    elif server_tz:
        timezone = HelperMaKaCInfo.getMaKaCInfoInstance().getTimezone()

    return _format_datetime(dt, format=format, locale=locale, tzinfo=timezone).encode('utf-8')
Пример #4
0
def format_datetime(datetime=None, format='medium', tzinfo=None):
    """Return a date formatted according to the given pattern.

    This uses the locale of the current request's ``pylons.translator``.

    :param datetime: the `datetime` object; if `None`, the current date and
                     time is used
    :param format: one of "full", "long", "medium", or "short", or a custom
                   date/time pattern
    :param tzinfo: the timezone to apply to the time for display
    :rtype: `unicode`
    """
    return _format_datetime(datetime, format, tzinfo, translator.locale)
Пример #5
0
def format_datetime(dt, format='medium', locale=None, timezone=None):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    inject_unicode = True
    if format == 'code':
        format = 'dd/MM/yyyy HH:mm'
        inject_unicode = False
    if not locale:
        locale = get_current_locale()
    if not timezone and dt.tzinfo:
        timezone = session.tzinfo

    rv = _format_datetime(dt, format=format, locale=locale, tzinfo=timezone)
    return inject_unicode_debug(rv, 2).encode('utf-8') if inject_unicode else rv.encode('utf-8')
Пример #6
0
def format_datetime(dt, format='medium', locale=None, timezone=None, server_tz=False, keep_tz=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    if not locale:
        locale = get_current_locale()
    if keep_tz:
        assert timezone is None
        timezone = dt.tzinfo
    elif not timezone and dt.tzinfo:
        timezone = DisplayTZ().getDisplayTZ()
    elif server_tz:
        timezone = Config.getInstance().getDefaultTimezone()

    return _format_datetime(dt, format=format, locale=locale, tzinfo=timezone).encode('utf-8')
Пример #7
0
def _format_pretty_datetime(dt, locale, tzinfo, formats):
    locale = get_current_locale() if not locale else parse_locale(locale)

    if tzinfo:
        if dt.tzinfo:
            dt = dt.astimezone(tzinfo)
        else:
            dt = tzinfo.localize(dt).astimezone(tzinfo)

    today = (now_utc(False).astimezone(tzinfo) if tzinfo else now_utc(False)).replace(hour=0, minute=0)
    diff = (dt - today).total_seconds() / 86400.0
    mapping = [(-6, 'other'), (-1, 'last_week'), (0, 'last_day'),
               (1, 'same_day'), (2, 'next_day'), (7, 'next_week'),
               (None, 'other')]

    fmt = next(formats[key] for delta, key in mapping if delta is None or diff < delta)
    fmt = fmt.format(date_fmt=locale.date_formats['medium'], time_fmt=locale.time_formats['short'])
    return _format_datetime(dt, fmt, tzinfo, locale)
Пример #8
0
def format_datetime(dt, format=None, tz=None, locale=LC_TIME):
    """Return string formatted datetime, `dt`, using format directives or
    pattern in `format`. If timezone, `tz`, is supplied, the datetime will be
    shifted to that timezone before being formatted.

    Args:
        dt (datetime): A datetime instance.
        format (str, optional): Datetime format string. Defaults to ``None``
            which uses ISO-8601 format.
        tz (None|str|tzinfo, optional): Timezone to shift `dt` to before
            formatting.
        locale (str|Locale, optional): A ``Locale`` object or locale
            identifer. Defaults to system default.

    Returns:
        str
    """
    if not isinstance(dt, datetime):
        raise TypeError("zulu.parser.format()'s first argument must be a "
                        "datetime, not {0}"
                        .format(type(dt).__name__))  # pragma: no cover

    if format is not None and not isinstance(format, string_types):
        raise TypeError("zulu.parser.format()'s second argument must be a "
                        "string or None, not {0}"
                        .format(type(format).__name__))  # pragma: no cover

    if not is_valid_timezone(tz):  # pragma: no cover
        raise ValueError('Unrecognized timezone: {0}'.format(tz))

    if format is None:
        format = ISO8601

    if tz is not None:
        dt = dt.astimezone(tz)

    if format == ISO8601:
        return dt.isoformat()
    elif '%' in format:
        return dt.strftime(format)
    else:
        return _format_datetime(dt, format, locale=locale)
Пример #9
0
def format_datetime(dt, format='medium', locale=None, timezone=None, server_tz=False, keep_tz=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    inject_unicode = True
    if format == 'code':
        format = 'dd/MM/yyyy HH:mm'
        inject_unicode = False
    if not locale:
        locale = get_current_locale()
    if keep_tz:
        assert timezone is None
        timezone = dt.tzinfo
    elif not timezone and dt.tzinfo:
        timezone = session.tzinfo
    elif server_tz:
        timezone = config.DEFAULT_TIMEZONE

    rv = _format_datetime(dt, format=format, locale=locale, tzinfo=timezone)
    return inject_unicode_debug(rv, 2).encode('utf-8') if inject_unicode else rv.encode('utf-8')
Пример #10
0
def format_datetime(dt, format='medium', locale=None, timezone=None, server_tz=False, keep_tz=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    inject_unicode = True
    if format == 'code':
        format = 'dd/MM/yyyy HH:mm'
        inject_unicode = False
    if not locale:
        locale = get_current_locale()
    if keep_tz:
        assert timezone is None
        timezone = dt.tzinfo
    elif not timezone and dt.tzinfo:
        timezone = DisplayTZ().getDisplayTZ()
    elif server_tz:
        timezone = Config.getInstance().getDefaultTimezone()

    rv = _format_datetime(dt, format=format, locale=locale, tzinfo=timezone)
    return inject_unicode_debug(rv, 2).encode('utf-8') if inject_unicode else rv.encode('utf-8')
Пример #11
0
def _format_pretty_datetime(dt, locale, tzinfo, formats):
    locale = get_current_locale() if not locale else parse_locale(locale)

    if tzinfo:
        if dt.tzinfo:
            dt = dt.astimezone(tzinfo)
        else:
            dt = tzinfo.localize(dt).astimezone(tzinfo)

    today = (now_utc(False).astimezone(tzinfo)
             if tzinfo else now_utc(False)).replace(hour=0, minute=0)
    diff = (dt - today).total_seconds() / 86400.0
    mapping = [(-6, 'other'), (-1, 'last_week'), (0, 'last_day'),
               (1, 'same_day'), (2, 'next_day'), (7, 'next_week'),
               (None, 'other')]

    fmt = next(formats[key] for delta, key in mapping
               if delta is None or diff < delta)
    fmt = fmt.format(date_fmt=locale.date_formats['medium'],
                     time_fmt=locale.time_formats['short'])
    return _format_datetime(dt, fmt, tzinfo, locale)
Пример #12
0
def format_datetime(dt,
                    format='medium',
                    locale=None,
                    timezone=None,
                    server_tz=False,
                    keep_tz=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    if not locale:
        locale = get_current_locale()
    if keep_tz:
        assert timezone is None
        timezone = dt.tzinfo
    elif not timezone and dt.tzinfo:
        timezone = DisplayTZ().getDisplayTZ()
    elif server_tz:
        timezone = HelperMaKaCInfo.getMaKaCInfoInstance().getTimezone()

    return _format_datetime(dt, format=format, locale=locale,
                            tzinfo=timezone).encode('utf-8')
Пример #13
0
def format_datetime(dt,
                    format='medium',
                    locale=None,
                    timezone=None,
                    as_unicode=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    inject_unicode = True
    if format == 'code':
        format = 'dd/MM/yyyy HH:mm'
        inject_unicode = False
    if not locale:
        locale = get_current_locale()
    if not timezone and dt.tzinfo:
        timezone = session.tzinfo

    rv = _format_datetime(dt, format=format, locale=locale, tzinfo=timezone)
    if as_unicode:
        return rv
    return inject_unicode_debug(
        rv, 2).encode('utf-8') if inject_unicode else rv.encode('utf-8')
Пример #14
0
def format_skeleton(dt, skeleton, locale=None, timezone=None):
    """Basically a wrapper around Babel's own format_skeleton.

    It also keeps the specified width from the originally requested
    skeleton string and adjusts the one from the locale data accordingly.

    The argument order is swapped to keep uniformity with other format_* functions.
    """
    if not locale:
        locale = get_current_locale()
    if not timezone and isinstance(dt, datetime) and dt.tzinfo:
        timezone = session.tzinfo

    # See https://github.com/python-babel/babel/issues/803 if you wonder why
    # we aren't using the default format_skeleton from Babel.
    locale = IndicoLocale.parse(locale)
    requested_skeleton = skeleton
    if skeleton not in locale.datetime_skeletons:
        skeleton = match_skeleton(skeleton, locale.datetime_skeletons)
    format = locale.datetime_skeletons[skeleton]
    format = _adjust_skeleton(str(format), requested_skeleton)
    return _format_datetime(dt, format=format, locale=locale, tzinfo=timezone)
Пример #15
0
def format_datetime(dt,
                    format='medium',
                    locale=None,
                    timezone=None,
                    server_tz=False,
                    keep_tz=False):
    """
    Basically a wrapper around Babel's own format_datetime
    """
    if format == 'code':
        format = 'dd/MM/yyyy HH:mm'
    if not locale:
        locale = get_current_locale()
    if keep_tz:
        assert timezone is None
        timezone = dt.tzinfo
    elif not timezone and dt.tzinfo:
        timezone = DisplayTZ().getDisplayTZ()
    elif server_tz:
        timezone = Config.getInstance().getDefaultTimezone()

    return _format_datetime(dt, format=format, locale=locale,
                            tzinfo=timezone).encode('utf-8')