Skip to content

Instantly share code, notes, and snippets.

@Xevion
Created April 25, 2023 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xevion/df02c26985acc71f845affefad531e8c to your computer and use it in GitHub Desktop.
Save Xevion/df02c26985acc71f845affefad531e8c to your computer and use it in GitHub Desktop.
from datetime import datetime, timedelta
def get_start_of_week(value: datetime) -> datetime:
"""Returns the datetime of the start of the week for the given datetime."""
return (value - timedelta(days=value.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
def get_minutes_since_start_of_week(value: datetime) -> float:
"""Returns the number of minutes since the start of the week."""
return (value - get_start_of_week(value)).total_seconds() / 60
def get_hours_since_start_of_week(value: datetime) -> float:
"""Returns the number of hours since the start of the week."""
return (value - get_start_of_week(value)).total_seconds() / 3600
start_of_week = get_start_of_week(datetime.now())
values = [
start_of_week + timedelta(days=0, hours=13),
start_of_week + timedelta(days=0, hours=20),
start_of_week + timedelta(days=2, hours=8),
start_of_week + timedelta(days=2, hours=17),
start_of_week + timedelta(days=4, hours=15),
start_of_week + timedelta(days=4, hours=23),
]
for value in values:
# format like Wednesday 8:00 AM
print(
f"{value.strftime('%A %I:%M %p')} {get_minutes_since_start_of_week(value):.0f}min {get_hours_since_start_of_week(value):.1f}hrs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment