示例#1
0
def matches_datetime_format(value):
    # show the year if the value's year is not the current year, but only do
    # that if it's more than 45 days in the future.  that way, at end of the
    # year, it doesn't show the year for everything.
    utcnow = datetime.datetime.utcnow()
    if value.year != utcnow.year:
        return format_datetime(value, "MMM d',' yyyy 'at' h':'mm a zzz")

    return format_datetime(value, "EEE',' MMM d 'at' h':'mm a zzz")
示例#2
0
 def test_refreshing(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'
         app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
         babel.refresh()
         assert babel.format_datetime(d) == 'Apr 12, 2010 3:46:00 PM'
示例#3
0
def matches_datetime_format(value):
    # show the year if the value's year is not the current year, but only do
    # that if it's more than 45 days in the future.  that way, at end of the
    # year, it doesn't show the year for everything.
    utcnow = datetime.datetime.utcnow()
    if value.year != utcnow.year:
        return format_datetime(value, "MMM d',' yyyy 'at' h':'mm a zzz")

    return format_datetime(value, "EEE',' MMM d 'at' h':'mm a zzz")
示例#4
0
 def test_refreshing(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'
         app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
         babel.refresh()
         assert babel.format_datetime(d) == 'Apr 12, 2010 3:46:00 PM'
示例#5
0
 def macro(self, content, arguments, page_url, alternative):
     if arguments is None:
         tm = time.time() # always UTC
     else:
         stamp = arguments[0]
         tm = self.parse_time(stamp)
     return format_datetime(datetime.utcfromtimestamp(tm))
示例#6
0
def update_time_zone():
    form = UserTimeZoneForm(request.form, obj=g.user)

    if form.validate_on_submit():
        form.populate_obj(g.user)
        db.session.commit()
        babel_refresh()
        flash(u'Your time zone was successfully updated.', 'success')

        now = to_user_timezone(datetime.utcnow())
        user_tz_names = (format_datetime(now, 'zzzz'), format_datetime(now, 'zzz'))

        return jsonify(success=True, time_zone=form.time_zone.data,
                csrf=form.csrf_token.data, user_tz_names=user_tz_names)

    flash(u'There was an error updating your time zone.', 'error')
    return jsonify(success=False)
示例#7
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"
示例#8
0
    def test_custom_formats(self):
        app = flask.Flask(__name__)
        app.config.update(BABEL_DEFAULT_LOCALE="en_US", BABEL_DEFAULT_TIMEZONE="Pacific/Johnston")
        b = babel.Babel(app)
        b.date_formats["datetime"] = "long"
        b.date_formats["datetime.long"] = "MMMM d, yyyy h:mm:ss a"
        d = datetime(2010, 4, 12, 13, 46)

        with app.test_request_context():
            assert babel.format_datetime(d) == "April 12, 2010 3:46:00 AM"
示例#9
0
    def test_custom_formats(self):
        app = flask.Flask(__name__)
        app.config.update(BABEL_DEFAULT_LOCALE='en_US',
                          BABEL_DEFAULT_TIMEZONE='Pacific/Johnston')
        b = babel.Babel(app)
        b.date_formats['datetime'] = 'long'
        b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a'
        d = datetime(2010, 4, 12, 13, 46)

        with app.test_request_context():
            assert babel.format_datetime(d) == 'April 12, 2010 3:46:00 AM'
示例#10
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'
示例#11
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'
示例#12
0
 def test_parse_time(self):
     MacroDateTimeBase_obj = MacroDateTimeBase()
     test_time_args = '2011-08-07T11:11:11+0533'
     result = MacroDateTimeBase_obj.parse_time(test_time_args)
     expected = 1312695491.0
     assert result == expected
     result = format_datetime(datetime.utcfromtimestamp(result))
     expected = u'Aug 7, 2011 5:38:11 AM'
     assert result == expected
     with pytest.raises(ValueError):
         # things after next 10,000 years can't be predicted
         MacroDateTimeBase_obj.parse_time('12011-08-07T11:11:11')
示例#13
0
    def test_custom_formats(self):
        app = flask.Flask(__name__)
        app.config.update(
            BABEL_DEFAULT_LOCALE='en_US',
            BABEL_DEFAULT_TIMEZONE='Pacific/Johnston'
        )
        b = babel.Babel(app)
        b.date_formats['datetime'] = 'long'
        b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a'
        d = datetime(2010, 4, 12, 13, 46)

        with app.test_request_context():
            assert babel.format_datetime(d) == 'April 12, 2010 3:46:00 AM'
示例#14
0
    def test_custom_locale_selector(self):
        app = flask.Flask(__name__)
        b = babel.Babel(app)
        d = datetime(2010, 4, 12, 13, 46)

        the_timezone = 'UTC'
        the_locale = 'en_US'

        @b.localeselector
        def select_locale():
            return the_locale
        @b.timezoneselector
        def select_timezone():
            return the_timezone

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

        the_locale = 'de_DE'
        the_timezone = 'Europe/Vienna'

        with app.test_request_context():
            assert babel.format_datetime(d) == '12.04.2010 15:46:00'
示例#15
0
    def test_custom_locale_selector(self):
        app = flask.Flask(__name__)
        b = babel.Babel(app)
        d = datetime(2010, 4, 12, 13, 46)

        the_timezone = 'UTC'
        the_locale = 'en_US'

        @b.localeselector
        def select_locale():
            return the_locale
        @b.timezoneselector
        def select_timezone():
            return the_timezone

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

        the_locale = 'de_DE'
        the_timezone = 'Europe/Vienna'

        with app.test_request_context():
            assert babel.format_datetime(d) == '12.04.2010 15:46:00'
示例#16
0
def timesince(dt, default=None):
    """
    Returns string representing "time since" e.g.
    3 days ago, 5 hours ago etc.
    """
    
    if default is None:
        default = gettext("just now")

    now = datetime.datetime.utcnow()
    naive = dt.replace(tzinfo=None)
    diff = now - naive

    years = diff.days / 365
    months = diff.days / 30
    weeks = diff.days / 7
    days = diff.days
    hours = diff.seconds / 3600
    minutes = diff.seconds / 60
    seconds = diff.seconds 

    periods = (
        (years, ngettext("%(num)s year", "%(num)s years", num=years)),
        (months, ngettext("%(num)s month", "%(num)s months", num=months)),
        (weeks, ngettext("%(num)s week", "%(num)s weeks", num=weeks)),
        (days, ngettext("%(num)s day", "%(num)s days", num=days)),
        (hours, ngettext("%(num)s hour", "%(num)s hours", num=hours)),
        (minutes, ngettext("%(num)s minute", "%(num)s minutes", num=minutes)),
        (seconds, ngettext("%(num)s second", "%(num)s seconds", num=seconds)),
    )

    if weeks:
        return format_datetime(dt, "short")

    for period, trans in periods:
        if period:
            return gettext("%(period)s ago", period=trans)

    return default
示例#17
0
def date_from_timestamp(timestamp):
    return format_datetime(datetime.fromtimestamp(timestamp))
示例#18
0
 def macro(self, stamp=None):
     tm = self.parse_time(stamp)
     return format_datetime(tm)
示例#19
0
def convert_to_localtime(dt):
    return format_datetime(dt)
示例#20
0
def date_from_timestamp(timestamp):
    return format_datetime(datetime.fromtimestamp(timestamp))
示例#21
0
def matches_datetime_format_full(value):
    return format_datetime(value, "EEEE',' MMMM d',' yyyy 'at' h':'mm a zzz")
示例#22
0
def matches_time_format(value):
    return format_datetime(value, "h':'mm a zzz")
示例#23
0
def matches_date_format(value):
    return format_datetime(value, "MMMM d',' yyyy")
示例#24
0
def timedelta(mtime):
    return format_datetime(mtime, _('MMMM d, yyyy'))
示例#25
0
 def test_non_initialized(self):
     app = flask.Flask(__name__)
     d = datetime(2010, 4, 12, 13, 46)
     with app.test_request_context():
         assert babel.format_datetime(d) == "Apr 12, 2010 1:46:00 PM"
示例#26
0
def convert_to_localtime(dt):
    return format_datetime(dt)
示例#27
0
 def test_non_initialized(self):
     app = flask.Flask(__name__)
     d = datetime(2010, 4, 12, 13, 46)
     with app.test_request_context():
         assert babel.format_datetime(d) == 'Apr 12, 2010 1:46:00 PM'
示例#28
0
def match_last_updated_format(value):
    return format_datetime(value, "MMM d 'at' h':'mm a")
示例#29
0
def matches_date_format(value):
    return format_datetime(value, "MMMM d',' yyyy")
示例#30
0
def match_last_updated_format(value):
    return format_datetime(value, "MMM d 'at' h':'mm a")
示例#31
0
def matches_time_format(value):
    return format_datetime(value, "h':'mm a zzz")
示例#32
0
def matches_datetime_format_full(value):
    return format_datetime(value, "EEEE',' MMMM d',' yyyy 'at' h':'mm a zzz")
示例#33
0
def date(value):
  format="EE, d MMMM y"
  return babel.format_datetime(value, format)
示例#34
0
def jinja_datetime(value, format='medium'):
    if format == 'full':
        format="EEEE, d. MMMM y 'at' HH:mm"
    elif format == 'medium':
        format="dd.MM.y HH:mm"
    return format_datetime(value+datetime.timedelta(days=1), format)