Exemplo n.º 1
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'
Exemplo 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'
Exemplo n.º 3
0
def datetime_filter(value, fmt='full'):
    """Convert a datetime to a different format."""
    if fmt == 'extend':
        fmt = "EEEE, dd MMMM yyyy @ HH:mm:ss"
    elif fmt == 'medium':
        fmt = "EE dd.MM.yyyy HH:mm"
    elif fmt == 'short':
        fmt = "dd.MM.yyyy @ HH:mm"
    return format_datetime(value, fmt)
Exemplo n.º 4
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'
Exemplo n.º 5
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'
Exemplo n.º 6
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'
Exemplo n.º 7
0
    def get_readable(self, row):
        row_timestamp = row[self.name]

        yday_now = time.localtime().tm_yday
        tm_year_now = time.localtime().tm_year
        yday_row = time.localtime(row_timestamp).tm_yday
        tm_year_row = time.localtime(row_timestamp).tm_year
        
        if yday_now == yday_row and tm_year_now == tm_year_row: 
            return format_time(datetime.fromtimestamp(row_timestamp)) 
        else:    
            return format_datetime(datetime.fromtimestamp(row_timestamp), 'short') 
Exemplo n.º 8
0
    def get_readable(self, row):
        row_timestamp = row[self.name]

        yday_now = time.localtime().tm_yday
        tm_year_now = time.localtime().tm_year
        yday_row = time.localtime(row_timestamp).tm_yday
        tm_year_row = time.localtime(row_timestamp).tm_year

        if yday_now == yday_row and tm_year_now == tm_year_row:
            return format_time(datetime.fromtimestamp(row_timestamp))
        else:
            return format_datetime(datetime.fromtimestamp(row_timestamp),
                                   'short')
Exemplo 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'
Exemplo n.º 10
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'
Exemplo n.º 11
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'
Exemplo n.º 12
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'
Exemplo n.º 13
0
def datetime_format(value):
    return format_datetime(value)
Exemplo n.º 14
0
 def get_readable(self, row):
     age = time.time() - row[self.name]
     return format_datetime(datetime.fromtimestamp(row[self.name]))
Exemplo n.º 15
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'
Exemplo n.º 16
0
 def get_readable(self, row):
     age = time.time() - row[self.name]
     return format_datetime(datetime.fromtimestamp(row[self.name]))