Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Thanksgiving Day app to dynamically calculate days till thanksgiving #2872

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
45 changes: 23 additions & 22 deletions apps/thanksgivingday/thanksgiving_day.star
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Author: J. Alex Cooney
"""

load("encoding/base64.star", "base64")
load("math.star", "math")
load("humanize.star", "humanize")
load("render.star", "render")
load("time.star", "time")

Expand All @@ -16,29 +16,30 @@ R0lGODlhIAAgAPcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
""")

def main(config):
timezone = config.get("timezone") or "America/New_York"
now = time.now().in_location(timezone)

thanksgiving_year = now.year

thanksgiving_day = math.ceil(28 - (5 + now.year + now.year / 4 - now.year / 100 + now.year / 400) % 7)
if thanksgiving_year == 2024:
thanksgiving_day = 28
elif thanksgiving_year == 2025:
thanksgiving_day = 27
def getthanksgivingday(year):
nov30 = time.time(year = year, month = 11, day = 30, location = timezone)
day_of_week = humanize.day_of_week(nov30)
calc = day_of_week - 4
if calc >= 0:
day = 30 - calc
else:
day = 30 - (calc + 7)

if 11 == now.month:
if 0 > thanksgiving_day - now.day:
thanksgiving_year = now.year + 1
return day

if now.month > 11:
thanksgiving_year = now.year + 1

thanksgiving = time.time(year = thanksgiving_year, month = 11, day = thanksgiving_day, hour = 0, minute = 0, location = timezone)

print(thanksgiving)
days_til_thanksgiving = math.ceil(time.parse_duration(thanksgiving - now).seconds / 86400)
print(days_til_thanksgiving)
timezone = config.get("timezone") or "America/New_York"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be timezone = config.get("$tz", "America/New_York")?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m gonna be honest, I have no idea. Looks to be that way ($tx) in some of the other countdown apps (ie Christmas) but it’s this way (timezone) in the Halloween countdown. Not sure which way is the correct way.

now = time.time(year = time.now().year, month = time.now().month, day = time.now().day, location = timezone)

day = getthanksgivingday(now.year)
thanksgiving_day = time.time(year = now.year, month = 11, day = day, location = timezone)
if now >= thanksgiving_day:
next_year = now.year + 1
day = getthanksgivingday(next_year)
thanksgiving_day = time.time(year = next_year, month = 11, day = day, location = timezone)

diff = thanksgiving_day - now
diff_days = abs(int(diff.hours / 24))
days_til_thanksgiving = diff_days

return render.Root(
delay = 1000,
Expand Down
Loading