Skip to content

Commit

Permalink
feat: use built-in timestamp formatting (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria authored Apr 13, 2022
1 parent ac99e4a commit 80f8afa
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions bot/exts/practices/_practice_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from bot.utils.datetimes import (
PACIFIC,
PACIFIC_CURRENT_NAME,
format_multi_time,
format_datetime,
parse_human_readable_datetime,
utcnow,
)
Expand Down Expand Up @@ -113,7 +113,7 @@ async def make_practice_session_embed(
else:
num_sessions = len(sessions)
for session in sessions:
title = format_multi_time(session.dtime)
title = format_datetime(session.dtime, format_type="t")
gcal_event_title = (
f"ASL Practice: {truncate(session.notes, 50)}"
if session.notes
Expand Down
4 changes: 2 additions & 2 deletions bot/exts/practices/daily_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from bot.utils.datetimes import (
EASTERN,
PACIFIC,
format_multi_time,
format_datetime,
parse_human_readable_datetime,
utcnow,
)
Expand Down Expand Up @@ -156,7 +156,7 @@ async def send_daily_message(
)
for event in events_today:
embed.add_field(
name=format_multi_time(event.scheduled_start_time),
name=format_datetime(event.scheduled_start_time, format_type="t"),
value=f"{event.name} · [Details/RSVP]({get_event_url(event)})",
inline=False,
)
Expand Down
5 changes: 2 additions & 3 deletions bot/exts/practices/practice.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
PACIFIC_CURRENT_NAME,
NoTimeZoneError,
display_timezone,
format_multi_time,
format_datetime,
parse_human_readable_datetime,
utcnow,
)
Expand Down Expand Up @@ -204,8 +204,7 @@ async def practice_impl(
logger.info(f"adding new practice session to sheet: {row}")
worksheet = await get_practice_worksheet_for_guild(guild_id)
worksheet.append_row(row)
dtime_pacific = dtime.astimezone(PACIFIC)
short_display_date = f"{dtime_pacific:%a, %b %d} {format_multi_time(dtime)}"
short_display_date = format_datetime(dtime)
sessions = await get_practice_sessions(
guild_id=guild_id, dtime=dtime, worksheet=worksheet
)
Expand Down
4 changes: 3 additions & 1 deletion bot/exts/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
PACIFIC_CURRENT_NAME,
NoTimeZoneError,
display_timezone,
format_datetime,
format_multi_time,
utcnow,
)
Expand Down Expand Up @@ -70,6 +71,7 @@ def format_scheduled_start_time_date(dtime: dt.datetime):


def format_scheduled_start_time(dtime: dt.datetime):
# NOTE: <t:...> format doesn't work in dropdown labels
return format_scheduled_start_time_date(dtime) + " · " + format_multi_time(dtime)


Expand Down Expand Up @@ -462,7 +464,7 @@ async def _prompt_for_start_time(
await start_time_message.edit(
content=(
"☑️ **When will your event start?**\n"
f"Entered: {format_scheduled_start_time(scheduled_start_time)}"
f"Entered: {format_datetime(scheduled_start_time, format_type='F')}"
)
)
return scheduled_start_time, used_timezone
Expand Down
4 changes: 4 additions & 0 deletions bot/utils/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def display_timezone(tzinfo: StaticTzInfo, dtime: dt.datetime) -> str:
return ret


def format_datetime(dtime: dt.datetime, format_type="f") -> str:
return f"<t:{round(dtime.timestamp())}:{format_type}>"


def display_time(dtime: dt.datetime, time_format: str, tzinfo: StaticTzInfo) -> str:
return dtime.astimezone(tzinfo).strftime(time_format) + display_timezone(
tzinfo, dtime
Expand Down
1 change: 1 addition & 0 deletions script/server
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
set -eo pipefail

docker-compose up -d
DEBUG=true python -m bot
6 changes: 3 additions & 3 deletions tests/bot/exts/__snapshots__/test_practice.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
colour=<Colour value=8701136>,
description='Today - Friday, September 25',
fields=<class 'list'> [
EmbedProxy(inline=False, name='2 PM PDT / 3 PM MDT / 4 PM CDT / 5 PM EDT', value='[+Google Calendar](http://www.google.com/calendar/event?action=TEMPLATE&text=ASL+Practice%3A+recurring&dates=20200925T210000Z%2F20200925T220000Z&details=See+the+full+schedule+here%3A+https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2Fabc%2Fedit)\n> Host: <@!12345>\n> Details: recurring'),
EmbedProxy(inline=False, name='<t:1601067600:t>', value='[+Google Calendar](http://www.google.com/calendar/event?action=TEMPLATE&text=ASL+Practice%3A+recurring&dates=20200925T210000Z%2F20200925T220000Z&details=See+the+full+schedule+here%3A+https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2Fabc%2Fedit)\n> Host: <@!12345>\n> Details: recurring'),
EmbedProxy(inline=True, name='🗓 View or edit the schedule using the link below.', value='[Full schedule](https://docs.google.com/spreadsheets/d/abc/edit)'),
],
footer=EmbedProxy(),
Expand All @@ -127,7 +127,7 @@
colour=<Colour value=8701136>,
description='Today - Friday, September 25',
fields=<class 'list'> [
EmbedProxy(inline=False, name='2 PM PDT / 3 PM MDT / 4 PM CDT / 5 PM EDT', value='[+Google Calendar](http://www.google.com/calendar/event?action=TEMPLATE&text=ASL+Practice%3A+recurring&dates=20200925T210000Z%2F20200925T220000Z&details=See+the+full+schedule+here%3A+https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2Fabc%2Fedit)\n> Host: <@!12345>\n> Details: recurring'),
EmbedProxy(inline=False, name='<t:1601067600:t>', value='[+Google Calendar](http://www.google.com/calendar/event?action=TEMPLATE&text=ASL+Practice%3A+recurring&dates=20200925T210000Z%2F20200925T220000Z&details=See+the+full+schedule+here%3A+https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2Fabc%2Fedit)\n> Host: <@!12345>\n> Details: recurring'),
EmbedProxy(inline=True, name='🗓 View or edit the schedule using the link below.', value='[Full schedule](https://docs.google.com/spreadsheets/d/abc/edit)'),
],
footer=EmbedProxy(),
Expand All @@ -151,7 +151,7 @@
colour=<Colour value=8701136>,
description='Today - Thursday, November 26 🦃',
fields=<class 'list'> [
EmbedProxy(inline=False, name='11 AM PST / 12 PM MST / 1 PM CST / 2 PM EST', value='[+Google Calendar](http://www.google.com/calendar/event?action=TEMPLATE&text=ASL+Practice%3A+Turkey+day+PRACTICE&dates=20201126T190000Z%2F20201126T200000Z&details=See+the+full+schedule+here%3A+https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2Fabc%2Fedit)\n> Details: Turkey day PRACTICE'),
EmbedProxy(inline=False, name='<t:1606417200:t>', value='[+Google Calendar](http://www.google.com/calendar/event?action=TEMPLATE&text=ASL+Practice%3A+Turkey+day+PRACTICE&dates=20201126T190000Z%2F20201126T200000Z&details=See+the+full+schedule+here%3A+https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2Fabc%2Fedit)\n> Details: Turkey day PRACTICE'),
EmbedProxy(inline=True, name='🗓 View or edit the schedule using the link below.', value='[Full schedule](https://docs.google.com/spreadsheets/d/abc/edit)'),
],
footer=EmbedProxy(),
Expand Down

0 comments on commit 80f8afa

Please sign in to comment.