Skip to content

Instantly share code, notes, and snippets.

@pikhovkin
Created January 14, 2025 23:49
Show Gist options
  • Save pikhovkin/cd6f637666d11cb55d70c1f8ae44f731 to your computer and use it in GitHub Desktop.
Save pikhovkin/cd6f637666d11cb55d70c1f8ae44f731 to your computer and use it in GitHub Desktop.
from datetime import datetime, timedelta
from dataclasses import dataclass
from zoneinfo import ZoneInfo
@dataclass(slots=True)
class WorkShift:
id: int
begin: int
end: int
shifts12 = [
WorkShift(1, -4 * 3600, 8 * 3600),
WorkShift(2, 8 * 3600, 20 * 3600),
]
shifts13 = [
WorkShift(1, -4 * 3600, 4 * 3600),
WorkShift(2, 4 * 3600, 12 * 3600),
WorkShift(3, 12 * 3600, 20 * 3600),
]
shifts14 = [
WorkShift(1, -3 * 3600, 3 * 3600),
WorkShift(2, 3 * 3600, 9 * 3600),
WorkShift(3, 9 * 3600, 15 * 3600),
WorkShift(4, 15 * 3600, 21 * 3600),
]
shifts22 = [
WorkShift(1, 8 * 3600, 20 * 3600),
WorkShift(2, 20 * 3600, 32 * 3600),
]
shifts23 = [
WorkShift(1, 4 * 3600, 12 * 3600),
WorkShift(2, 12 * 3600, 20 * 3600),
WorkShift(3, 20 * 3600, 28 * 3600),
]
shifts24 = [
WorkShift(1, 3 * 3600, 9 * 3600),
WorkShift(2, 9 * 3600, 15 * 3600),
WorkShift(3, 15 * 3600, 21 * 3600),
WorkShift(4, 21 * 3600, 27 * 3600),
]
shifts32 = [
WorkShift(1, 0, 12 * 3600),
WorkShift(2, 12 * 3600, 24 * 3600),
]
def get_shift(shifts: list[WorkShift], dt: datetime):
total_seconds = dt.hour * 3600 + dt.minute * 60 + dt.second
if shifts[0].begin < 0:
ts, add_shift = shifts[0], 86400
else:
ts, add_shift = shifts[-1], -86400
for shift in shifts:
if shift.begin <= total_seconds < shift.end:
ts, add_shift = shift, 0
break
d = dt.replace(hour=0, minute=0, second=0, microsecond=0)
print(dt, ts.id, d + timedelta(seconds=ts.begin + add_shift), d + timedelta(seconds=ts.end + add_shift))
def main(shifts: list[WorkShift]):
tz = ZoneInfo('UTC')
now = datetime.now(tz).replace(hour=0, minute=0, second=0, microsecond=0)
for h in range(-5, 33):
get_shift(shifts, now + timedelta(hours=h))
if __name__ == '__main__':
main(sorted(shifts12, key=lambda shift: shift.begin))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment