Created
April 25, 2023 01:50
-
-
Save Xevion/df02c26985acc71f845affefad531e8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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