Example #1
0
def format_date_time(utc_dt=None):
    """Return current or passed (dt) time in user's preferred format or default to moin-style ISO 8601."""
    if utc_dt is None:
        utc_dt = datetime.datetime.utcnow()
    user_tz = i18n.get_timezone()
    fmt = '%Y-%m-%d %H:%M'
    if user_tz:
        tz = pytz.timezone(user_tz)
    else:
        tz = pytz.utc
        fmt = fmt + 'Z'
    loc_dt = tz.localize(utc_dt)
    dt = loc_dt.strftime(fmt)
    return dt
Example #2
0
def format_date_time(utc_dt=None,
                     fmt='yyyy-MM-dd HH:mm:ss',
                     interval='datetime'):
    """
    Add an ISO 8601 alternative to babel's date/time formatting.

    Visitors who are not logged-in see ISO 8601 formatted dates and times with
    a "z" suffix indicating the date/time is a UTC Zulu time.

    Logged in users who have selected the ISO 8601 option in usersettings and
    have set their time zone to UTC in usersettings see date/times with a "z" suffix.
    Users with a time zone other than UTC see local date/times in ISO 8601 format
    without the "z" suffix.

    All other logged-in users will see the usual babel date/time formats based upon
    their time zone and locale.

    See http://babel.pocoo.org/en/latest/dates.html#date-fields for babel format syntax.
    """
    if utc_dt is None:
        utc_dt = datetime.datetime.utcnow()
    elif isinstance(utc_dt, (float, int)):
        utc_dt = datetime.datetime.utcfromtimestamp(utc_dt)

    if not flaskg.user.valid:
        # users who are not logged-in get moin version of ISO 8601: 2019-07-15 07:08:09z
        return flask_babel.format_datetime(utc_dt, fmt) + 'z'

    if flaskg.user.iso_8601:
        suffix = ''
        user_tz = i18n.get_timezone()
        if user_tz:
            if pytz.timezone(user_tz) == pytz.utc:
                suffix = 'z'
        return flask_babel.format_datetime(utc_dt, fmt) + suffix

    if interval == 'date':
        return flask_babel.format_date(utc_dt)
    elif interval == 'time':
        return flask_babel.format_time(utc_dt)
    return flask_babel.format_datetime(utc_dt)
Example #3
0
def test_user_attributes():
    test_locale = get_locale()
    assert test_locale == 'en'

    test_timezone = get_timezone()
    assert test_timezone == 'UTC'