-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
54 lines (45 loc) · 1.82 KB
/
Copy pathmodels.py
File metadata and controls
54 lines (45 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from dataclasses import dataclass, asdict
from datetime import datetime, timedelta
from typing import Optional
@dataclass
class ShiftRecord:
"""Запись о смене"""
user_id: int
username: str
full_name: str
start_time: str # ISO формат
end_time: Optional[str] = None
@property
def start_datetime(self) -> datetime:
"""Начало смены как datetime"""
return datetime.fromisoformat(self.start_time)
@property
def end_datetime(self) -> Optional[datetime]:
"""Конец смены как datetime"""
return datetime.fromisoformat(self.end_time) if self.end_time else None
def duration(self) -> Optional[timedelta]:
"""Продолжительность смены"""
if self.end_time:
return self.end_datetime - self.start_datetime
return None
def duration_str(self) -> str:
"""Продолжительность в читаемом виде"""
duration = self.duration()
if duration:
hours = int(duration.total_seconds() // 3600)
minutes = int((duration.total_seconds() % 3600) // 60)
return f"{hours}ч {minutes}м"
return "В процессе"
def elapsed_time(self) -> str:
"""Сколько времени прошло с начала смены"""
elapsed = datetime.now() - self.start_datetime
hours = int(elapsed.total_seconds() // 3600)
minutes = int((elapsed.total_seconds() % 3600) // 60)
return f"{hours}ч {minutes}м"
def to_dict(self) -> dict:
"""Преобразование в словарь"""
return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> 'ShiftRecord':
"""Создание из словаря"""
return cls(**data)