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'
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 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'
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'
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'
def timesince(dt, default="just now"): now = datetime.utcnow() diff = now - dt if diff.days > 10: return format_datetime(dt, 'Y-M-d H:m') elif diff.days <= 10 and diff.days > 0: periods = ((diff.days, "day", "days"), ) elif diff.days <= 0 and diff.seconds > 3600: periods = ((diff.seconds / 3600, "hour", "hours"), ) elif diff.seconds <= 3600 and diff.seconds > 90: periods = ((diff.seconds / 60, "minute", "minutes"), ) else: return default for period, singular, plural in periods: if period: return "%d %s ago" % (period, singular if period == 1 else plural) return default
def test_timezone_selector(app): """Test format_datetime.""" app.config["I18N_LANGUAGES"] = [("da", "Danish")] InvenioI18N(app) with app.test_request_context(): assert format_datetime(datetime(1987, 3, 5, 17, 12)) == "Mar 5, 1987, 5:12:00 PM" assert ( format_datetime(datetime(1987, 3, 5, 17, 12), "full") == "Thursday, March 5, 1987 at 5:12:00 PM GMT+00:00" ) assert format_datetime(datetime(1987, 3, 5, 17, 12), "short") == "3/5/87, 5:12 PM" assert format_datetime(datetime(1987, 3, 5, 17, 12), "dd mm yyy") == "05 12 1987" assert format_datetime(datetime(1987, 3, 5, 17, 12), "dd mm yyyy") == "05 12 1987" with app.test_request_context(headers=[("Accept-Language", "da")]): assert str(get_locale()) == "da" assert format_datetime(datetime(1987, 3, 5, 17, 12)) == "05/03/1987 17.12.00"
def test_timezone_selector(app): """Test format_datetime.""" app.config['I18N_LANGUAGES'] = [('da', 'Danish')] InvenioI18N(app) with app.test_request_context(): assert format_datetime(datetime(1987, 3, 5, 17, 12)) == \ 'Mar 5, 1987, 5:12:00 PM' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'full') == \ 'Thursday, March 5, 1987 at 5:12:00 PM GMT+00:00' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'short') == \ '3/5/87, 5:12 PM' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyy') == \ '05 12 1987' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyyy') \ == '05 12 1987' with app.test_request_context(headers=[('Accept-Language', 'da')]): assert str(get_locale()) == 'da' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'short') == \ '05/03/1987 17.12'
def test_timezone_selector(app): """Test format_datetime.""" app.config['I18N_LANGUAGES'] = ['da'] InvenioI18N(app) with app.test_request_context(): assert format_datetime(datetime(1987, 3, 5, 17, 12)) == \ 'Mar 5, 1987, 5:12:00 PM' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'full') == \ 'Thursday, March 5, 1987 at 5:12:00 PM GMT+00:00' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'short') == \ '3/5/87, 5:12 PM' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyy') == \ '05 12 1987' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyyy') \ == '05 12 1987' with app.test_request_context(headers=[('Accept-Language', 'da')]): assert str(get_locale()) == 'da' assert format_datetime(datetime(1987, 3, 5, 17, 12)) == \ '05/03/1987 17.12.00'
def test_timezone_selector(app): """Test format_datetime.""" app.config['I18N_LANGUAGES'] = [('da', 'Danish')] InvenioI18N(app) with app.test_request_context(): assert format_datetime(datetime(1987, 3, 5, 17, 12)) == \ 'Mar 5, 1987, 5:12:00 PM' # Adds the new date format due to a library update 2 assert format_datetime(datetime(1987, 3, 5, 17, 12), 'full') in [ 'Thursday, March 5, 1987 at 5:12:00 PM GMT+00:00', 'Thursday, March 5, 1987 at 5:12:00 PM Coordinated Universal Time'] assert format_datetime(datetime(1987, 3, 5, 17, 12), 'short') == \ '3/5/87, 5:12 PM' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyy') == \ '05 12 1987' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyyy') \ == '05 12 1987' with app.test_request_context(headers=[('Accept-Language', 'da')]): assert str(get_locale()) == 'da' assert format_datetime(datetime(1987, 3, 5, 17, 12), 'short') == \ '05.03.1987 17.12'
def test_timezone_selector(app): """Test format_datetime.""" app.config["I18N_LANGUAGES"] = [("da", "Danish")] InvenioI18N(app) with app.test_request_context(): assert (format_datetime(datetime(1987, 3, 5, 17, 12)) == "Mar 5, 1987, 5:12:00 PM") # Adds the new date format due to a library update 2 assert format_datetime(datetime(1987, 3, 5, 17, 12), "full") in [ "Thursday, March 5, 1987 at 5:12:00 PM GMT+00:00", "Thursday, March 5, 1987 at 5:12:00 PM Coordinated Universal Time", ] assert (format_datetime(datetime(1987, 3, 5, 17, 12), "short") == "3/5/87, 5:12 PM") assert (format_datetime(datetime(1987, 3, 5, 17, 12), "dd mm yyy") == "05 12 1987") assert (format_datetime(datetime(1987, 3, 5, 17, 12), "dd mm yyyy") == "05 12 1987") with app.test_request_context(headers=[("Accept-Language", "da")]): assert str(get_locale()) == "da" assert (format_datetime(datetime(1987, 3, 5, 17, 12), "short") == "05.03.1987 17.12")
def viewstats_helper(jobpost_id, interval, limit, daybatch=False): post = JobPost.query.get(jobpost_id) if not post.datetime: return {} viewed = UserJobView.query.filter_by(jobpost_id=jobpost_id).all() opened = [v for v in viewed if v.applied == True] # NOQA applied = db.session.query( JobApplication.created_at).filter_by(jobpost_id=jobpost_id).all() # Now batch them by size now = datetime.utcnow() delta = now - post.datetime if daybatch: batches, remainder = divmod(delta.days, interval) if delta.seconds: remainder = True else: batches, remainder = divmod(int(delta.total_seconds()), interval) if remainder or batches == 0: batches += 1 cviewed = batches * [0] copened = batches * [0] capplied = batches * [0] cbuckets = batches * [''] interval_hour = interval / 3600 # these are used as initial values for hourly stats # buckets are like "HH:00 - HH:00" to_datetime = (now + timedelta(hours=1) ) # if now is 09:45, bucket ending hour will be 10:00 from_datetime = to_datetime - timedelta( hours=interval_hour ) # starting hour will be, 06:00, if interval is 4 hours for delta in range(batches): if daybatch: # here delta=0 at first, and last item is the latest date/hour cbuckets[batches - delta - 1] = format_datetime( (now - timedelta(days=delta)), 'd MMM') else: from_hour = format_datetime(from_datetime, 'd MMM HH:00') to_hour = format_datetime(to_datetime, 'HH:00') cbuckets[batches - delta - 1] = u"{from_hour} — {to_hour}".format( from_hour=from_hour, to_hour=to_hour) # if current bucket was 18:00-22:00, then # previous bucket becomes 14:00-18:00 to_datetime = from_datetime from_datetime = to_datetime - timedelta(hours=interval_hour) for clist, source, attr in [(cviewed, viewed, 'created_at'), (copened, opened, 'updated_at'), (capplied, applied, 'created_at')]: for item in source: sourcedate = getattr(item, attr) if sourcedate < post.datetime: # This happens when the user creates a post when logged in. Their 'viewed' date will be # for the draft, whereas the confirmed post's datetime will be later. There should # be just one instance of this. This can also happen if the server's clock is reset, such # as by an NTP error after reboot (has happened to us). sourcedate = post.datetime itemdelta = sourcedate - post.datetime try: if daybatch: clist[int(itemdelta.days // interval)] += 1 else: clist[int(int(itemdelta.total_seconds()) // interval)] += 1 except IndexError: # Server time got messed up. Ouch! Ignore for now. pass if limit and batches > limit: cviewed = cviewed[:limit] copened = copened[:limit] capplied = capplied[:limit] cbuckets = cbuckets[:limit] return { 'max': max([ max(cviewed) if cviewed else 0, max(copened) if copened else 0, max(capplied) if capplied else 0, ]), 'length': max([len(cviewed), len(copened), len(capplied)]), 'viewed': cviewed, 'opened': copened, 'applied': capplied, 'buckets': cbuckets }
def show_time(): from flask_babelex import format_datetime if g.user.is_authenticated: return 'LOCALE:' + format_datetime(datetime.utcnow()) else: return 'UTC:' + format_datetime(datetime.utcnow())
def viewstats_helper(jobpost_id, interval, limit, daybatch=False): post = JobPost.query.get(jobpost_id) if not post.datetime: return {} viewed = UserJobView.query.filter_by(jobpost_id=jobpost_id).all() opened = [v for v in viewed if v.applied == True] # NOQA applied = db.session.query(JobApplication.created_at).filter_by(jobpost_id=jobpost_id).all() # Now batch them by size now = datetime.utcnow() delta = now - post.datetime if daybatch: batches, remainder = divmod(delta.days, interval) if delta.seconds: remainder = True else: batches, remainder = divmod(int(delta.total_seconds()), interval) if remainder or batches == 0: batches += 1 cviewed = batches * [0] copened = batches * [0] capplied = batches * [0] cbuckets = batches * [''] interval_hour = interval / 3600 # these are used as initial values for hourly stats # buckets are like "HH:00 - HH:00" to_datetime = (now + timedelta(hours=1)) # if now is 09:45, bucket ending hour will be 10:00 from_datetime = to_datetime - timedelta(hours=interval_hour) # starting hour will be, 06:00, if interval is 4 hours for delta in range(batches): if daybatch: # here delta=0 at first, and last item is the latest date/hour cbuckets[batches - delta - 1] = format_datetime((now - timedelta(days=delta)), 'd MMM') else: from_hour = format_datetime(from_datetime, 'd MMM HH:00') to_hour = format_datetime(to_datetime, 'HH:00') cbuckets[batches - delta - 1] = u"{from_hour} — {to_hour}".format(from_hour=from_hour, to_hour=to_hour) # if current bucket was 18:00-22:00, then # previous bucket becomes 14:00-18:00 to_datetime = from_datetime from_datetime = to_datetime - timedelta(hours=interval_hour) for clist, source, attr in [ (cviewed, viewed, 'created_at'), (copened, opened, 'updated_at'), (capplied, applied, 'created_at')]: for item in source: sourcedate = getattr(item, attr) if sourcedate < post.datetime: # This happens when the user creates a post when logged in. Their 'viewed' date will be # for the draft, whereas the confirmed post's datetime will be later. There should # be just one instance of this. This can also happen if the server's clock is reset, such # as by an NTP error after reboot (has happened to us). sourcedate = post.datetime itemdelta = sourcedate - post.datetime try: if daybatch: clist[int(itemdelta.days // interval)] += 1 else: clist[int(int(itemdelta.total_seconds()) // interval)] += 1 except IndexError: # Server time got messed up. Ouch! Ignore for now. pass if limit and batches > limit: cviewed = cviewed[:limit] copened = copened[:limit] capplied = capplied[:limit] cbuckets = cbuckets[:limit] return { 'max': max([ max(cviewed) if cviewed else 0, max(copened) if copened else 0, max(capplied) if capplied else 0, ]), 'length': max([len(cviewed), len(copened), len(capplied)]), 'viewed': cviewed, 'opened': copened, 'applied': capplied, 'buckets': cbuckets }
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'