getting human time math more correct

This commit is contained in:
stvnrlly
2022-11-29 22:07:32 -05:00
parent 8f47af11da
commit a0ade73846
3 changed files with 14 additions and 14 deletions

View File

@@ -10,7 +10,7 @@ import ago
import dateutil import dateutil
import humanize import humanize
import pytz import pytz
from flask import Markup, url_for from flask import Markup, current_app, url_for
from notifications_utils.field import Field from notifications_utils.field import Field
from notifications_utils.formatters import make_quotes_smart from notifications_utils.formatters import make_quotes_smart
from notifications_utils.formatters import nl2br as utils_nl2br from notifications_utils.formatters import nl2br as utils_nl2br
@@ -91,7 +91,7 @@ def get_human_day(time, date_prefix=''):
# Add 1 minute to transform 00:00 into midnight today instead of midnight tomorrow # Add 1 minute to transform 00:00 into midnight today instead of midnight tomorrow
time = parse_naive_dt(time) time = parse_naive_dt(time)
date = (convert_utc_to_local_timezone(time) - timedelta(minutes=1)).date() date = (convert_utc_to_local_timezone(time) - timedelta(minutes=1)).date()
now = datetime.utcnow() now = datetime.now(current_app.config['PY_TIMEZONE'])
if date == (now + timedelta(days=1)).date(): if date == (now + timedelta(days=1)).date():
return 'tomorrow' return 'tomorrow'

View File

@@ -75,7 +75,7 @@ from app.utils.user_permissions import all_ui_permissions, permission_options
def get_time_value_and_label(future_time): def get_time_value_and_label(future_time):
return ( return (
future_time.replace(tzinfo=None).isoformat(), future_time.astimezone(pytz.utc).replace(tzinfo=None).isoformat(),
'{} at {} ET'.format( '{} at {} ET'.format(
get_human_day(future_time.astimezone(current_app.config['PY_TIMEZONE'])), get_human_day(future_time.astimezone(current_app.config['PY_TIMEZONE'])),
get_human_time(future_time.astimezone(current_app.config['PY_TIMEZONE'])) get_human_time(future_time.astimezone(current_app.config['PY_TIMEZONE']))
@@ -96,9 +96,9 @@ def get_human_time(time):
def get_human_day(time, prefix_today_with='T'): def get_human_day(time, prefix_today_with='T'):
# Add 1 hour to get midnight today instead of midnight tomorrow # Add 1 hour to get midnight today instead of midnight tomorrow
time = (time - timedelta(hours=1)).strftime('%A') time = (time - timedelta(hours=1)).strftime('%A')
if time == datetime.utcnow().strftime('%A'): if time == datetime.now(current_app.config['PY_TIMEZONE']).strftime('%A'):
return '{}oday'.format(prefix_today_with) return '{}oday'.format(prefix_today_with)
if time == (datetime.utcnow() + timedelta(days=1)).strftime('%A'): if time == (datetime.now(current_app.config['PY_TIMEZONE']) + timedelta(days=1)).strftime('%A'):
return 'Tomorrow' return 'Tomorrow'
return time return time

View File

@@ -11,21 +11,21 @@ def test_form_contains_next_24h(notify_admin):
# Friday # Friday
assert choices[0] == ('', 'Now') assert choices[0] == ('', 'Now')
assert choices[1] == ('2016-01-01T12:00:00', 'Today at noon ET') assert choices[1] == ('2016-01-01T17:00:00', 'Today at noon ET')
assert choices[13] == ('2016-01-02T00:00:00', 'Today at midnight ET') assert choices[13] == ('2016-01-02T05:00:00', 'Today at midnight ET')
# Saturday # Saturday
assert choices[14] == ('2016-01-02T01:00:00', 'Tomorrow at 1am ET') assert choices[14] == ('2016-01-02T06:00:00', 'Tomorrow at 1am ET')
assert choices[37] == ('2016-01-03T00:00:00', 'Tomorrow at midnight ET') assert choices[37] == ('2016-01-03T05:00:00', 'Tomorrow at midnight ET')
# Sunday # Sunday
assert choices[38] == ('2016-01-03T01:00:00', 'Sunday at 1am ET') assert choices[38] == ('2016-01-03T06:00:00', 'Sunday at 1am ET')
# Monday # Monday
assert choices[62] == ('2016-01-04T01:00:00', 'Monday at 1am ET') assert choices[62] == ('2016-01-04T06:00:00', 'Monday at 1am ET')
assert choices[80] == ('2016-01-04T19:00:00', 'Monday at 7pm ET') assert choices[80] == ('2016-01-05T00:00:00', 'Monday at 7pm ET')
assert choices[84] == ('2016-01-04T23:00:00', 'Monday at 11pm ET') assert choices[84] == ('2016-01-05T04:00:00', 'Monday at 11pm ET')
assert choices[85] == ('2016-01-05T00:00:00', 'Monday at midnight ET') assert choices[85] == ('2016-01-05T05:00:00', 'Monday at midnight ET')
with pytest.raises(IndexError): with pytest.raises(IndexError):
assert choices[ assert choices[