Last active
March 3, 2025 02:53
-
-
Save bswck/412762f2d70acfb9a88c727fc7764d5c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 collections.abc import Iterator, Sequence | |
from datetime import date, timedelta | |
from functools import partial | |
from itertools import batched | |
work_time = timedelta(hours=7, minutes=15) | |
year = 2025 | |
( | |
january_2025, | |
february_2025, | |
march_2025, | |
april_2025, | |
may_2025, | |
june_2025, | |
july_2025, | |
august_2025, | |
september_2025, | |
october_2025, | |
november_2025, | |
december_2025, | |
) = map(partial(partial, date, year), range(1, 13)) | |
december_2024 = partial(date, 2024, 12) | |
started_at = december_2024(16) | |
free_days = [ | |
december_2024(25), # First day of Christmas | |
december_2024(26), # Second day of Christmas | |
december_2024(30), # Paid leave | |
december_2024(31), # New Year's Eve (paid leave) | |
january_2025(1), # New Year's Day | |
january_2025(6), # Epiphany | |
january_2025(16), # Unpaid leave | |
january_2025(17), # Unpaid leave | |
january_2025(22), # Paid leave | |
january_2025(23), # Paid leave | |
january_2025(31), # Paid leave | |
february_2025(3), # Paid leave | |
] | |
def period(start_date: date, end_date: date | None = None) -> Iterator[date]: | |
return map( | |
date.fromordinal, | |
range( | |
start_date.toordinal(), | |
(end_date or date.today() - timedelta(days=1)).toordinal() + 1, | |
), | |
) | |
def get_work_days( | |
start_date: date, *, end_date: date | None = None, free_days: Sequence[date] | |
) -> list[date]: | |
return [ | |
date | |
for date in period(start_date, end_date) | |
if not date.weekday() in (5, 6) and date not in free_days | |
] | |
print("Work time:", work_time) | |
print("\nWork days:") | |
for rows in batched(enumerate(get_work_days(started_at, free_days=free_days), start=1), 7): | |
for total, day in rows: | |
print(day, end=" ") | |
print() | |
print(f"\nTotal of {total} work days.") | |
total_work_time = work_time * len(get_work_days(started_at, free_days=free_days)) | |
def repr_timedelta(td: timedelta) -> str: | |
return f"{td.days * 24 + td.seconds // 3600}h {td.seconds % 3600 // 60}m" | |
print(f"Total work time: {repr_timedelta(total_work_time)}.") | |
pasted_from_toggl = "301:05:51" | |
work_time_kwargs = dict( | |
zip( | |
("hours", "minutes", "seconds"), | |
map(int, pasted_from_toggl.split(":")), | |
) | |
) | |
actual_work_time = timedelta(**work_time_kwargs) | |
print(f"Actual work time: {repr_timedelta(actual_work_time)}.") | |
type_of_difference = "overtime" if actual_work_time > total_work_time else "undertime" | |
print(f"Difference: {repr_timedelta(total_work_time - actual_work_time)} {type_of_difference}.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment