Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Created November 23, 2023 14:01
Show Gist options
  • Save rsbohn/36ca09b059fe9f9bfb38f95de8b49d53 to your computer and use it in GitHub Desktop.
Save rsbohn/36ca09b059fe9f9bfb38f95de8b49d53 to your computer and use it in GitHub Desktop.
Calendar
# SPDX-FileCopyrightText: 2023 Randall Bohn
# SPDX-License-Identifier: MIT
# a calendar for eInk display
# Blinka but not CircuitPython because of these imports:
from datetime import datetime
from datetime import timedelta
def pad_day(day:datetime) -> str:
"""
Pads a single day with a leading space
"""
value = day.strftime("%d")
if len(value) == 1:
value = " "+value
return value
def week(delta:int, start:datetime|None=None) -> str:
"""
returns the week calendar relative to the start date
defaults to today
"""
if start is None:
start = datetime.today()
today = start + timedelta(weeks=delta)
day_of_week = today.isocalendar()[2] % 7
sunday = (today - timedelta(days=day_of_week))
current_week = [sunday + timedelta(days=i) for i in range(7)]
current_week = [pad_day(day) for day in current_week]
current_week = " ".join(current_week)
if delta == 0 and day_of_week > 0:
p = day_of_week * 4
current_week = current_week[:p-1]+"!"+current_week[p:]
return current_week
def month(m:datetime):
result = ""
result += week(-1, start=m) + " " + m.strftime('%Y %b') + "\n"
result += week(0, start=m) + " " + m.strftime('%A') + "\n"
result += week(1, start=m) + "\n"
result += week(2, start=m) + "\n"
return result
if __name__=="__main__":
#print(month(datetime(2023,1,1)))
print(month(datetime.today()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment