-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerithm.py
More file actions
189 lines (135 loc) · 5.31 KB
/
Copy pathtimerithm.py
File metadata and controls
189 lines (135 loc) · 5.31 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from calendar import monthrange
from datetime import datetime, timedelta
from functools import total_ordering
from re import findall
from types import NotImplementedType
def microseconds(amount: int) -> timedelta:
return timedelta(microseconds=amount)
def milliseconds(amount: int) -> timedelta:
return timedelta(milliseconds=amount)
def seconds(amount: int) -> timedelta:
return timedelta(seconds=amount)
def minutes(amount: int) -> timedelta:
return timedelta(minutes=amount)
def hours(amount: int) -> timedelta:
return timedelta(hours=amount)
def days(amount: int) -> timedelta:
return timedelta(days=amount)
def weeks(amount: int) -> timedelta:
return timedelta(weeks=amount)
def months(amount: int) -> "_Months":
return _Months(amount)
def years(amount: int) -> "_Years":
return _Years(amount)
class _Months:
def __init__(self, amount: int) -> None:
self.amount = amount
class _Years:
def __init__(self, amount: int) -> None:
self.amount = amount
@total_ordering
class Time:
def __init__(self, date: datetime) -> None:
self._date = date
def __repr__(self) -> str:
return f"<not implemented>"
def __str__(self) -> str:
return f"not implemented"
def __hash__(self) -> int:
return hash(self._date)
def __eq__(self, other: object) -> bool | NotImplementedType:
if isinstance(other, Time):
return self._date == other._date
if isinstance(other, datetime):
return self._date == other
return NotImplemented
def __lt__(self, other: object) -> bool | NotImplementedType:
if isinstance(other, Time):
return self._date < other._date
if isinstance(other, datetime):
return self._date < other
return NotImplemented
def __add__(self, other: object) -> "Time | NotImplementedType":
if isinstance(other, timedelta):
return Time(self._date + other)
if isinstance(other, _Months):
return Time(self._shift_months(other.amount))
if isinstance(other, _Years):
return Time(self._shift_months(other.amount * 12))
return NotImplemented
def __sub__(self, other: object) -> "Time | NotImplementedType":
if isinstance(other, timedelta):
return Time(self._date - other)
if isinstance(other, _Months):
return Time(self._shift_months(-other.amount))
if isinstance(other, _Years):
return Time(self._shift_months(-other.amount * 12))
return NotImplemented
def _shift_months(self, amount: int) -> datetime:
total = self._date.month + amount - 1
new_year = (total // 12) + self._date.year
new_month = (total % 12) + 1
max_day = monthrange(new_year, new_month)[1]
new_day = min(self._date.day, max_day)
return self._date.replace(year=new_year, month=new_month, day=new_day)
@property
def microsecond(self) -> int:
return self._date.microsecond
@property
def millisecond(self) -> int:
return self._date.microsecond // 1000
@property
def second(self) -> int:
return self._date.second
@property
def minute(self) -> int:
return self._date.minute
@property
def hour(self) -> int:
return self._date.hour
@property
def day(self) -> int:
return self._date.day
@property
def month(self) -> int:
return self._date.month
@property
def year(self) -> int:
return self._date.year
@classmethod
def now(cls) -> "Time":
return cls(datetime.now())
@classmethod
def at(cls, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> "Time":
return cls(datetime(year, month, day, hour, minute, second, microsecond))
def format(self, layout: str) -> str:
presets = {
"S": "BBBB D, YYYY hh:mm",
"E": "D BBBB, YYYY hh:mm",
"L": "YYYYMMDDhhmmss",
"T": "YYYY-MM-DD hh:mm:ss",
"C": "AAAA, BBBB D YYYY hh:mm:ss",
}
mapping = {
"AAAA": "%A", "AAA": "%a", "BBBB": "%B", "BBB": "%b",
"YYYY": "%Y", "YY": "%y", "MM": "%m", "DD": "%d",
"hh": "%H", "ii": "%I", "mm": "%M", "ss": "%S",
"F": "%f", "J": "%j", "U": "%u", "W": "%w",
"P": "%p", "p": "%p"
}
if "f" in layout:
microsecond = self._date.strftime("%f")
for match in findall(r"f+", layout):
sliced = microsecond[:min(len(match), 6)]
layout = layout.replace(match, sliced)
for preset, expansion in presets.items():
layout = layout.replace(preset, expansion)
if "D" in layout:
layout = layout.replace("D", str(self._date.day))
if "M" in layout:
layout = layout.replace("M", str(self._date.month))
result = self._date.strftime(layout)
if "p" in layout:
result = result.replace("AM", "am")
result = result.replace("PM", "pm")
return result