-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
81 lines (65 loc) · 2.29 KB
/
utils.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
import time
import datetime
import re
import sys
from bblogger import Logger, LogLevel
SECONDS_IN_DAY = 86400
Log = Logger()
def getMilis(seconds):
int_sec = int(seconds)
return int((seconds - int_sec)*100)
def convertTimeDate(seconds, bgetMilis = False):
time_object = time.gmtime(seconds)
if bgetMilis:
str_time = "{}-{}-{} {}:{}:{}.{}".format(time_object.tm_year, time_object.tm_mon, time_object.tm_mday,
time_object.tm_hour, time_object.tm_min, time_object.tm_sec, getMilis(seconds))
else:
str_time = "{}-{}-{} {}:{}:{}".format(time_object.tm_year, time_object.tm_mon, time_object.tm_mday,
time_object.tm_hour, time_object.tm_min, time_object.tm_sec)
return str_time
def convertDate(seconds):
time_object = time.gmtime(seconds)
str_time = "{}-{}-{}".format(time_object.tm_year, time_object.tm_mon, time_object.tm_mday)
return str_time
def checkDate(str_date, bIsMandatory = False):
if str_date == None:
return None
match = re.findall("(\d+)\.(\d+)\.(\d+)", str_date)
if len(match) < 1:
if bIsMandatory:
Log.Log (LogLevel.MANDATORY, "Wrong date format at all!")
return None
if len(match[0]) < 3:
if bIsMandatory:
Log.Log (LogLevel.MANDATORY, "Wrong date format!")
return None
return match
def convertDateToSec(str_time):
match = checkDate(str_time, True)
if match == None:
return 0
Log.Log (LogLevel.DEBUG, match[0][0], match[0][1], match[0][2])
try:
year, month, day = int(match[0][0]), int(match[0][1]), int(match[0][2])
time_zone = datetime.timezone(datetime.timedelta())
total_secs = datetime.datetime(year, month, day, tzinfo = time_zone).timestamp()
Log.Log (LogLevel.DEBUG, convertTimeDate(total_secs))
return int(total_secs)
except ValueError as error:
Log.Log (LogLevel.MANDATORY, "Incorrect date format [{}]".format(error))
except:
Log.Log (LogLevel.MANDATORY, "Unexpected error:", sys.exc_info()[0])
return 0
def findClosestNumber(numbers_list, number):
numbers_list.sort()
index = numbers_list.index(number)
prev_index = index - 1
next_index = index + 1
if prev_index < 0:
return next_index
if next_index >= len(numbers_list):
return prev_index
if abs(numbers_list[index] - numbers_list[prev_index]) < abs(numbers_list[next_index] - numbers_list[prev_index]):
return numbers_list[prev_index]
else:
return numbers_list[next_index]