forked from Ridepad/uwu-logs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h_datetime.py
111 lines (89 loc) · 3.16 KB
/
h_datetime.py
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
import re
from datetime import datetime, timedelta
MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
T_DELTA = {
"2SEC": timedelta(seconds=2),
"15SEC": timedelta(seconds=15),
"30SEC": timedelta(seconds=30),
"1MIN": timedelta(minutes=1),
"2MIN": timedelta(minutes=2),
"3MIN": timedelta(minutes=3),
"5MIN": timedelta(minutes=5),
"10MIN": timedelta(minutes=10),
"15MIN": timedelta(minutes=15),
"20MIN": timedelta(minutes=20),
"30MIN": timedelta(minutes=30),
"14H": timedelta(hours=14),
}
def get_now():
return datetime.now()
CURRENT_YEAR = get_now().year
RE_FIND_ALL = re.compile("(\d+)").findall
RE_FIND_ALL_BYTES = re.compile(b'(\d+)').findall
def to_dt_closure(year=None):
re_find_all = re.compile('(\d+)').findall
CURRENT = get_now()
CURRENT_SHIFT = CURRENT + T_DELTA["14H"]
if year is None:
year = CURRENT.year
def inner(s: str):
q = list(map(int, re_find_all(s[:18])))
q[-1] *= 1000
dt = datetime(year, *q)
if dt > CURRENT_SHIFT:
dt = dt.replace(year=year-1)
return dt
else:
def inner(s: str):
q = list(map(int, re_find_all(s[:18])))
q[-1] *= 1000
return datetime(year, *q)
return inner
class ToDatetime:
def __init__(self, year=None) -> None:
self.now = get_now()
if year is None:
year = self.now.year
self.year = year
self.re_findall = RE_FIND_ALL
@property
def datetime_shifted(self):
try:
self.__datetime_shifted
except AttributeError:
self.__datetime_shifted = self.now + T_DELTA["14H"]
return self.__datetime_shifted
def to_dt_simple(s: str):
return datetime(CURRENT_YEAR, *map(int, RE_FIND_ALL(s[:18])))
def to_dt_simple_precise(s: str):
q = list(map(int, RE_FIND_ALL(s[:18])))
q[-1] *= 1000
return datetime(CURRENT_YEAR, *q)
def get_delta(current, previous):
return to_dt_simple(current) - to_dt_simple(previous)
def get_delta_simple_precise(current, previous):
return to_dt_simple_precise(current) - to_dt_simple_precise(previous)
def to_dt_year(s: str, year: int):
return datetime(year, *map(int, RE_FIND_ALL(s[:18])))
def to_dt_year_precise(s: str, year: int):
q = list(map(int, RE_FIND_ALL(s[:18])))
q[-1] *= 1000
return datetime(year, *q)
def to_dt_simple_bytes(s: bytes):
return datetime(CURRENT_YEAR, *map(int, RE_FIND_ALL_BYTES(s[:18])))
def to_dt_year_bytes(s: bytes, year: int):
return datetime(year, *map(int, RE_FIND_ALL_BYTES(s[:18])))
def to_dt_bytes_closure(year: int=None):
if year is None:
year = get_now().year
def inner(s: str):
return datetime(year, *map(int, RE_FIND_ALL_BYTES(s[:18])))
return inner
def to_dt_bytes_year_fix(s, year: int=None):
if year is None:
year = get_now().year
dt = datetime(year, *map(int, RE_FIND_ALL_BYTES(s[:18])))
CURRENT_SHIFTED = get_now() + T_DELTA["14H"]
if dt > CURRENT_SHIFTED:
dt = dt.replace(year=year-1)
return dt