Skip to content

Commit

Permalink
Refactor time to next AoC calculation into a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
n0Oo0Oo0b committed Nov 8, 2023
1 parent 171a0b5 commit 71bbd59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 3 additions & 10 deletions bot/exts/advent_of_code/_cog.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import json
import logging
from datetime import UTC, datetime, timedelta
from datetime import UTC, datetime
from pathlib import Path

import arrow
import discord
from async_rediscache import RedisCache
from discord import app_commands
Expand Down Expand Up @@ -175,14 +174,8 @@ async def aoc_countdown(self, ctx: commands.Context) -> None:
await ctx.send(f"Day {tomorrow.day} starts <t:{next_day_timestamp}:R>.")
return

datetime_now = arrow.now(_helpers.EST)
# Calculate the delta to this & next year's December 1st to see which one is closest and not in the past
this_year = arrow.get(datetime(datetime_now.year, 12, 1, tzinfo=UTC), _helpers.EST)
next_year = arrow.get(datetime(datetime_now.year + 1, 12, 1, tzinfo=UTC), _helpers.EST)
deltas = (dec_first - datetime_now for dec_first in (this_year, next_year))
delta = min(delta for delta in deltas if delta >= timedelta()) # timedelta() gives 0 duration delta

next_aoc_timestamp = int((datetime_now + delta).timestamp())
next_aoc, _ = _helpers.time_left_to_next_aoc()
next_aoc_timestamp = int(next_aoc.timestamp())

await ctx.send(
"The Advent of Code event is not currently running. "
Expand Down
14 changes: 14 additions & 0 deletions bot/exts/advent_of_code/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,20 @@ def is_in_advent() -> bool:
return arrow.now(EST).day in range(1, 25) and arrow.now(EST).month == 12


def time_left_to_next_aoc() -> tuple[datetime.datetime, datetime.timedelta]:
"""
Calculate the amount of time left until the next AoC.
This will be either this year or next year's December 1, whichever one is
closer and not in the past.
"""
datetime_now = arrow.now(EST)
this_year = arrow.get(datetime.datetime(datetime_now.year, 12, 1, tzinfo=datetime.UTC), EST)
next_year = arrow.get(datetime.datetime(datetime_now.year + 1, 12, 1, tzinfo=datetime.UTC), EST)
dec_first = this_year if this_year > datetime_now else next_year
return dec_first, dec_first - datetime_now


def time_left_to_est_midnight() -> tuple[datetime.datetime, datetime.timedelta]:
"""Calculate the amount of time left until midnight EST/UTC-5."""
# Change all time properties back to 00:00
Expand Down

0 comments on commit 71bbd59

Please sign in to comment.