def format_datetime(context, value):
    london_date_time = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
    london_date = london_date_time.date()
    formatted_date = flask_babel.format_date(london_date, format="d MMMM yyyy")
    formatted_time = flask_babel.format_time(london_date_time, format="HH:mm")

    result = "<span class='date'>{date}</span>".format(
        date=flask_babel.gettext(
            "%(date)s at %(time)s", date=formatted_date, time=formatted_time))
    return mark_safe(context, result)
Example #2
0
    def test_basics(self):
        app = flask.Flask(__name__)
        babel.Babel(app)
        d = datetime(2010, 4, 12, 13, 46)

        with app.test_request_context():
            assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '1:46:00 PM'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
            assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '3:46:00 PM'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE'
            assert babel.format_datetime(d, 'long') == \
                '12. April 2010 um 15:46:00 MESZ'
Example #3
0
    def test_basics(self):
        app = flask.Flask(__name__)
        b = babel.Babel(app)
        d = datetime(2010, 4, 12, 13, 46)

        with app.test_request_context():
            assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '1:46:00 PM'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
            assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM'
            assert babel.format_date(d) == 'Apr 12, 2010'
            assert babel.format_time(d) == '3:46:00 PM'

        with app.test_request_context():
            app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE'
            assert babel.format_datetime(d, 'long') == \
                '12. April 2010 15:46:00 MESZ'
def format_datetime(context, date_time):
    # flask babel on formatting will automatically convert based on the time zone specified in setup.py
    formatted_date = flask_babel.format_date(date_time, format="d MMMM yyyy")
    formatted_time = flask_babel.format_time(date_time, format="HH:mm")

    date = flask_babel.gettext("%(date)s at %(time)s",
                               date=formatted_date,
                               time=formatted_time)

    result = f"<span class='date'>{date}</span>"

    return mark_safe(context, result)
Example #5
0
def format_datetime(context, value):

    london_date_time = datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
    formatted_date = flask_babel.format_date(london_date_time,
                                             format='d MMMM YYYY')
    formatted_time = flask_babel.format_time(london_date_time, format='HH:mm')

    result = "<span class='date'>{date}</span>".format(
        date=flask_babel.gettext('%(date)s at %(time)s',
                                 date=formatted_date,
                                 time=formatted_time), )
    return mark_safe(context, result)
Example #6
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 #7
0
 def time_filter(time, format=None, locale=None):
     return format_time(time, format, locale)
Example #8
0
def time_short(datetime):
    if datetime:
        return format_time(parse_date(datetime), 'HH:mm')
Example #9
0
def filter_format_time(dt, format=None, rebase=True):
    """Return a date formatted according to the given pattern."""
    return format_time(dt, format=format, rebase=rebase)
Example #10
0
def time(datetime, ref=current_user):
    time = in_timezone_of(datetime, ref).time()
    return format_time(time, 'HH:mm', rebase=False)
Example #11
0
def time(datetime, ref=current_user):
    time = in_timezone_of(datetime, ref).time()
    return format_time(time, 'HH:mm', rebase=False)
Example #12
0
def filter_format_time(dt, format=None, rebase=True):
    """Return a date formatted according to the given pattern."""
    return format_time(dt, format=format, rebase=rebase)
Example #13
0
def to_time(time):
    return format_time(time, format='short')
Example #14
0
def index():

    d = date(1984, 8, 12)
    t = time(22, 16)

    current_locale = str(get_locale())

    if current_locale in ['es', 'de', 'mt', 'se_SE']:
        current_currency = 'EUR'
    elif current_locale == 'en_GB':
        current_currency = 'GBP'
    elif current_locale in ['en_AU']:
        current_currency = 'AUD'
    else:
        current_currency = 'USD'

    translate_current = gettext('Current')

    us_num = numbers.format_decimal(1.2345, locale='en_US')
    us_cur = numbers.format_currency(1.2345, locale='en_US', currency='USD')
    us_date = dates.format_date(d, locale='en_US')
    us_time = dates.format_time(t, locale='en_US')

    se_num = numbers.format_decimal(1.2345, locale='se_SE')
    se_cur = numbers.format_currency(1.2345, locale='se_SE', currency='EUR')
    se_date = dates.format_date(d, locale='se_SE')
    se_time = dates.format_time(t, locale='se_SE')

    mt_num = numbers.format_decimal(1.2345, locale='mt_MT')
    mt_cur = numbers.format_currency(1.2345, locale='mt_MT', currency='EUR')
    mt_date = dates.format_date(d, locale='mt_MT')
    mt_time = dates.format_time(t, locale='mt_MT')

    current_num = format_decimal(1.2345)
    current_cur = format_currency(1.2345, currency=current_currency)
    current_date = format_date(d)
    current_time = format_time(t)

    results = {
        'us_num': us_num,
        'us_cur': us_cur,
        'us_date': us_date,
        'us_time': us_time,
        'se_num': se_num,
        'se_cur': se_cur,
        'se_date': se_date,
        'se_time': se_time,
        'mt_num': mt_num,
        'mt_cur': mt_cur,
        'mt_date': mt_date,
        'mt_time': mt_time,
        'current_num': current_num,
        'current_cur': current_cur,
        'current_date': current_date,
        'current_time': current_time
    }

    return render_template('index.html',
                           results=results,
                           current_locale=current_locale,
                           current=translate_current)