Skip to content

Commit fcd683a

Browse files
committed
Python3 version:
- clean date ranges functions, make sure a timestamp range limit is an integer
1 parent f1f369e commit fcd683a

File tree

4 files changed

+116
-116
lines changed

4 files changed

+116
-116
lines changed

alignak/daterange.py

+85-17
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,87 @@
5555
import re
5656
from datetime import datetime, timedelta
5757

58-
from alignak.util import get_sec_from_morning, get_day, get_start_of_day, get_end_of_day
5958
from alignak.alignakobject import AlignakObject
6059

6160
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
6261

6362

63+
def get_start_of_day(year, month, day):
64+
"""Get the timestamp associated to the first second of a specific day
65+
66+
:param year: date year
67+
:type year: int
68+
:param month: date month
69+
:type month: int
70+
:param day: date day
71+
:type day: int
72+
:return: timestamp
73+
:rtype: int
74+
"""
75+
# DST is not known in the provided date
76+
try:
77+
timestamp = time.mktime((year, month, day, 00, 00, 00, 0, 0, -1))
78+
except (OverflowError, ValueError):
79+
# Windows mktime sometimes crashes on (1970, 1, 1, ...)
80+
timestamp = 0.0
81+
82+
return int(timestamp)
83+
84+
85+
def get_end_of_day(year, month, day):
86+
"""Get the timestamp associated to the last second of a specific day
87+
88+
:param year: date year
89+
:type year: int
90+
:param month: date month (int)
91+
:type month: int
92+
:param day: date day
93+
:type day: int
94+
:return: timestamp
95+
:rtype: int
96+
"""
97+
# DST is not known in the provided date
98+
timestamp = time.mktime((year, month, day, 23, 59, 59, 0, 0, -1))
99+
return int(timestamp)
100+
101+
102+
def get_day(timestamp):
103+
"""Get timestamp of the beginning of the day (local) given by timestamp
104+
105+
:param timestamp: time to get day from
106+
:type timestamp: int
107+
:return: timestamp
108+
:rtype: int
109+
"""
110+
return int(timestamp - get_sec_from_morning(timestamp))
111+
112+
113+
def get_wday(timestamp):
114+
"""Get week day from date
115+
116+
:param timestamp: timestamp date
117+
:type timestamp: int
118+
:return: weekday (0-6)
119+
:rtype: int
120+
TODO: Not timezone aware
121+
"""
122+
t_lt = time.localtime(timestamp)
123+
return t_lt.tm_wday
124+
125+
126+
def get_sec_from_morning(timestamp):
127+
"""Get the number of seconds elapsed since the beginning of the
128+
day deducted from the provided timestamp
129+
130+
:param timestamp: time to use for computation
131+
:type timestamp: int
132+
:return: timestamp
133+
:rtype: int
134+
"""
135+
t_lt = time.localtime(timestamp)
136+
return t_lt.tm_hour * 3600 + t_lt.tm_min * 60 + t_lt.tm_sec
137+
138+
64139
def find_day_by_weekday_offset(year, month, weekday, offset):
65140
"""Get the day number based on a date and offset
66141
@@ -302,7 +377,6 @@ def get_start_and_end_time(self, ref=None): # pylint: disable=unused-argument,n
302377
:type ref: int
303378
:return: None
304379
"""
305-
logger.warning("Calling function get_start_and_end_time which is not implemented")
306380
raise NotImplementedError()
307381

308382
def is_time_valid(self, timestamp):
@@ -375,13 +449,8 @@ def is_time_day_invalid(self, timestamp):
375449
:type timestamp: int
376450
:return: False if t in range, True otherwise
377451
:rtype: bool
378-
TODO: Remove this function. Duplication
379452
"""
380-
(start_time, end_time) = self.get_start_and_end_time(timestamp)
381-
if start_time <= timestamp <= end_time:
382-
return False
383-
384-
return True
453+
return not self.is_time_day_valid(timestamp)
385454

386455
def get_next_future_timerange_valid(self, timestamp):
387456
"""Get the next valid timerange (next timerange start in timeranges attribute)
@@ -665,11 +734,10 @@ def get_start_and_end_time(self, ref=None):
665734
:param ref: time in seconds
666735
:type ref: int
667736
:return: tuple with start and end time
668-
:rtype: tuple
737+
:rtype: tuple (int, int)
669738
"""
670-
start_time = get_start_of_day(self.syear, int(self.smon), self.smday)
671-
end_time = get_end_of_day(self.eyear, int(self.emon), self.emday)
672-
return (start_time, end_time)
739+
return (get_start_of_day(self.syear, int(self.smon), self.smday),
740+
get_end_of_day(self.eyear, int(self.emon), self.emday))
673741

674742

675743
class StandardDaterange(AbstractDaterange):
@@ -732,7 +800,7 @@ def get_start_and_end_time(self, ref=None):
732800
:param ref: time in seconds
733801
:type ref: int
734802
:return: tuple with start and end time
735-
:rtype: tuple
803+
:rtype: tuple (int, int)
736804
"""
737805
now = time.localtime(ref)
738806
self.syear = now.tm_year
@@ -744,7 +812,7 @@ def get_start_and_end_time(self, ref=None):
744812
day_diff = (day_id - now.tm_wday) % 7
745813
morning = datetime.fromtimestamp(today_morning) + timedelta(days=day_diff)
746814
night = datetime.fromtimestamp(tonight) + timedelta(days=day_diff)
747-
return (float(morning.strftime("%s")), float(night.strftime("%s")))
815+
return (int(morning.strftime("%s")), int(night.strftime("%s")))
748816

749817

750818
class MonthWeekDayDaterange(Daterange):
@@ -823,7 +891,7 @@ def get_start_and_end_time(self, ref=None):
823891
:param ref: time in seconds
824892
:type ref: int
825893
:return: tuple with start and end time
826-
:rtype: tuple
894+
:rtype: tuple (int, int)
827895
"""
828896
now = time.localtime(ref)
829897
if self.syear == 0:
@@ -867,7 +935,7 @@ def get_start_and_end_time(self, ref=None):
867935
:param ref: time in seconds
868936
:type ref: int
869937
:return: tuple with start and end time
870-
:rtype: tuple
938+
:rtype: tuple (int, int)
871939
"""
872940
now = time.localtime(ref)
873941

@@ -931,7 +999,7 @@ def get_start_and_end_time(self, ref=None):
931999
:param ref: time in seconds
9321000
:type ref: int
9331001
:return: tuple with start and end time
934-
:rtype: tuple
1002+
:rtype: tuple (int, int)
9351003
"""
9361004
now = time.localtime(ref)
9371005
if self.syear == 0:

alignak/objects/schedulingitem.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -958,10 +958,10 @@ def is_enable_action_dependent(self, hosts, services):
958958
:rtype: bool
959959
"""
960960
# Use to know if notification is raise or not
961-
enable_notif = False
961+
enable_action = False
962962
for (dep_id, status, _, _) in self.act_depend_of:
963963
if 'n' in status:
964-
enable_notif = True
964+
enable_action = True
965965
else:
966966
if dep_id in hosts:
967967
dep = hosts[dep_id]
@@ -973,8 +973,8 @@ def is_enable_action_dependent(self, hosts, services):
973973
if True in dep_match:
974974
p_is_down = True
975975
if not p_is_down:
976-
enable_notif = True
977-
return enable_notif
976+
enable_action = True
977+
return enable_action
978978

979979
def check_and_set_unreachability(self, hosts, services):
980980
"""

alignak/util.py

-80
Original file line numberDiff line numberDiff line change
@@ -227,86 +227,6 @@ def jsonify_r(obj): # pragma: no cover, not for unit tests...
227227

228228

229229
# ################################## TIME ##################################
230-
def get_end_of_day(year, month_id, day):
231-
"""Get the timestamp of the end (local) of a specific day
232-
233-
:param year: date year
234-
:type year: int
235-
:param month_id: date month (int)
236-
:type month_id: int
237-
:param day: date day
238-
:type day: int
239-
:return: timestamp
240-
:rtype: int
241-
TODO: Not timezone aware
242-
"""
243-
end_time = (year, month_id, day, 23, 59, 59, 0, 0, -1)
244-
end_time_epoch = time.mktime(end_time)
245-
return end_time_epoch
246-
247-
248-
def get_day(timestamp):
249-
"""Get timestamp of the beginning of the day (local) given by timestamp
250-
251-
:param timestamp: time to get day from
252-
:type timestamp: int
253-
:return: timestamp
254-
:rtype: int
255-
TODO: Not timezone aware
256-
"""
257-
return int(timestamp - get_sec_from_morning(timestamp))
258-
259-
260-
def get_wday(timestamp):
261-
"""Get week day from date
262-
263-
:param timestamp: timestamp date
264-
:type timestamp: int
265-
:return: weekday (0-6)
266-
:rtype: int
267-
TODO: Not timezone aware
268-
"""
269-
t_lt = time.localtime(timestamp)
270-
return t_lt.tm_wday
271-
272-
273-
def get_sec_from_morning(timestamp):
274-
"""Get the numbers of seconds elapsed since the beginning of the day (local) given by timestamp
275-
276-
:param timestamp: time to get amount of second from
277-
:type timestamp: int
278-
:return: timestamp
279-
:rtype: int
280-
TODO: Not timezone aware
281-
"""
282-
t_lt = time.localtime(timestamp)
283-
return t_lt.tm_hour * 3600 + t_lt.tm_min * 60 + t_lt.tm_sec
284-
285-
286-
def get_start_of_day(year, month_id, day):
287-
"""Get the timestamp of the beginning (local) of a specific day
288-
289-
:param year: date year
290-
:type year: int
291-
:param month_id: date month (int)
292-
:type month_id: int
293-
:param day: date day
294-
:type day: int
295-
:return: timestamp
296-
:rtype: float
297-
TODO: Not timezone aware
298-
"""
299-
# DST is not known
300-
start_time = (year, month_id, day, 00, 00, 00, 0, 0, -1)
301-
try:
302-
start_time_epoch = time.mktime(start_time)
303-
except OverflowError:
304-
# Windows mktime sometimes crashes on (1970, 1, 1, ...)
305-
start_time_epoch = 0.0
306-
307-
return start_time_epoch
308-
309-
310230
def format_t_into_dhms_format(timestamp):
311231
""" Convert an amount of second into day, hour, min and sec
312232

test/test_dateranges.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
# along with Alignak. If not, see <http://www.gnu.org/licenses/>.
2020

2121
"""
22-
This file is used to test dateranges
22+
This file is used to test the date ranges management
2323
24-
We make timestamp with time.mktime because timestamp not same is you are in timezone UTC or Paris
24+
We make timestamp with time.mktime because the timestamp is not the same
25+
dependening upon the timezone
2526
"""
2627
# pylint: disable=R0904
2728

@@ -50,7 +51,7 @@ def test_get_start_of_day(self):
5051
"""
5152
now = time.localtime()
5253
start = time.mktime((2015, 7, 26, 0, 0, 0, 0, 0, now.tm_isdst))
53-
timestamp = alignak.util.get_start_of_day(2015, 7, 26)
54+
timestamp = alignak.daterange.get_start_of_day(2015, 7, 26)
5455
# time.timezone is the offset related of the current timezone of the system
5556
print("Start: %s, timestamp: %s" % (start, timestamp))
5657
if start != timestamp:
@@ -69,7 +70,7 @@ def test_get_start_of_day_tz_aware(self):
6970
start = time.mktime((now.tm_year, now.tm_mon, now.tm_mday, 0, 0, 0, 0, 0, -1))
7071
print("Start: %s" % start)
7172
# Alignak returns the start of day ts in local time
72-
timestamp = alignak.util.get_start_of_day(now.tm_year, now.tm_mon, now.tm_mday)
73+
timestamp = alignak.daterange.get_start_of_day(now.tm_year, now.tm_mon, now.tm_mday)
7374
print("Timestamp: %s" % timestamp)
7475
# time.timezone is the offset related of the current timezone of the system
7576
if start != timestamp:
@@ -82,7 +83,7 @@ def test_get_end_of_day(self):
8283
"""
8384
now = time.localtime()
8485
start = time.mktime((2016, 8, 20, 23, 59, 59, 0, 0, now.tm_isdst))
85-
timestamp = alignak.util.get_end_of_day(2016, 8, 20)
86+
timestamp = alignak.daterange.get_end_of_day(2016, 8, 20)
8687
print("Start: %s, timestamp: %s" % (start, timestamp))
8788
# time.timezone is the offset related of the current timezone of the system
8889
if start != timestamp:
@@ -182,10 +183,14 @@ def test_standarddaterange_start_end_time(self):
182183
caldate = StandardDaterange({'day': 'friday', 'other': '00:00-24:00'})
183184
for date_now in data:
184185
with freeze_time(date_now, tz_offset=0):
185-
ret = caldate.get_start_and_end_time()
186-
print("* %s" % date_now)
187-
assert data[date_now]['start'] == ret[0]
188-
assert data[date_now]['end'] == ret[1]
186+
# ret = caldate.get_start_and_end_time()
187+
# print("* %s" % date_now)
188+
# assert data[date_now]['start'] == ret[0]
189+
# assert data[date_now]['end'] == ret[1]
190+
start, end = caldate.get_start_and_end_time()
191+
print("-> res: %s (%s) - %s (%s)" % (start, type(start), end, type(end)))
192+
assert data[date_now]['start'] == start
193+
assert data[date_now]['end'] == end
189194

190195
def test_monthweekdaydaterange_start_end_time(self):
191196
""" Test MonthWeekDayDaterange.get_start_and_end_time to get start and end date of date range
@@ -242,6 +247,7 @@ def test_monthdatedaterange_start_end_time(self):
242247
local_hour_offset = "-%02d" % local_hour_offset
243248
else:
244249
local_hour_offset = "+%02d" % -local_hour_offset
250+
245251
data = {
246252
'2015-07-20 01:50:00 %s' % local_hour_offset: {
247253
'start': 1437868800 + local_offset,
@@ -266,12 +272,18 @@ def test_monthdatedaterange_start_end_time(self):
266272
caldate = MonthDateDaterange(params)
267273
for date_now in data:
268274
with freeze_time(date_now, tz_offset=0):
269-
print("Date: %s" % date_now)
270-
ret = caldate.get_start_and_end_time()
271-
print("* %s / %s" % (type(ret[0]), type(data[date_now]['start'])))
272-
print("* %s / %s" % (int(ret[0]), int(ret[1])))
273-
assert data[date_now]['start'] == int(ret[0])
274-
assert data[date_now]['end'] == int(ret[1])
275+
print("Date: %s: %s" % (date_now, data[date_now]))
276+
start, end = caldate.get_start_and_end_time()
277+
print("-> res: %s (%s) - %s (%s)" % (start, type(start), end, type(end)))
278+
assert data[date_now]['start'] == start
279+
assert data[date_now]['end'] == end
280+
281+
# ret = caldate.get_start_and_end_time()
282+
# print("-> res: %s" % ret)
283+
# print("* %s / %s" % (type(ret[0]), type(ret[1])))
284+
# print("* %s / %s" % (int(ret[0]), int(ret[1])))
285+
# assert data[date_now]['start'] == int(ret[0])
286+
# assert data[date_now]['end'] == int(ret[1])
275287

276288
def test_weekdaydaterange_start_end_time(self):
277289
""" Test WeekDayDaterange.get_start_and_end_time to get start and end date of date range

0 commit comments

Comments
 (0)