Ejemplo n.º 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")
Ejemplo n.º 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'
Ejemplo n.º 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")
Ejemplo n.º 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'
Ejemplo n.º 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))
Ejemplo n.º 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)
Ejemplo n.º 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"
Ejemplo n.º 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"
Ejemplo n.º 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'
Ejemplo n.º 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'
Ejemplo n.º 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'
Ejemplo n.º 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')
Ejemplo n.º 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'
Ejemplo n.º 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'
Ejemplo n.º 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'
Ejemplo n.º 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
Ejemplo n.º 17
0
def date_from_timestamp(timestamp):
    return format_datetime(datetime.fromtimestamp(timestamp))
Ejemplo n.º 18
0
 def macro(self, stamp=None):
     tm = self.parse_time(stamp)
     return format_datetime(tm)
Ejemplo n.º 19
0
def convert_to_localtime(dt):
    return format_datetime(dt)
Ejemplo n.º 20
0
def date_from_timestamp(timestamp):
    return format_datetime(datetime.fromtimestamp(timestamp))
Ejemplo n.º 21
0
def matches_datetime_format_full(value):
    return format_datetime(value, "EEEE',' MMMM d',' yyyy 'at' h':'mm a zzz")
Ejemplo n.º 22
0
def matches_time_format(value):
    return format_datetime(value, "h':'mm a zzz")
Ejemplo n.º 23
0
def matches_date_format(value):
    return format_datetime(value, "MMMM d',' yyyy")
Ejemplo n.º 24
0
def timedelta(mtime):
    return format_datetime(mtime, _('MMMM d, yyyy'))
Ejemplo n.º 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"
Ejemplo n.º 26
0
def convert_to_localtime(dt):
    return format_datetime(dt)
Ejemplo n.º 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'
Ejemplo n.º 28
0
def match_last_updated_format(value):
    return format_datetime(value, "MMM d 'at' h':'mm a")
Ejemplo n.º 29
0
def matches_date_format(value):
    return format_datetime(value, "MMMM d',' yyyy")
Ejemplo n.º 30
0
def match_last_updated_format(value):
    return format_datetime(value, "MMM d 'at' h':'mm a")
Ejemplo n.º 31
0
def matches_time_format(value):
    return format_datetime(value, "h':'mm a zzz")
Ejemplo n.º 32
0
def matches_datetime_format_full(value):
    return format_datetime(value, "EEEE',' MMMM d',' yyyy 'at' h':'mm a zzz")
Ejemplo n.º 33
0
def date(value):
  format="EE, d MMMM y"
  return babel.format_datetime(value, format)
Ejemplo n.º 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)