55
55
import re
56
56
from datetime import datetime , timedelta
57
57
58
- from alignak .util import get_sec_from_morning , get_day , get_start_of_day , get_end_of_day
59
58
from alignak .alignakobject import AlignakObject
60
59
61
60
logger = logging .getLogger (__name__ ) # pylint: disable=invalid-name
62
61
63
62
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
+
64
139
def find_day_by_weekday_offset (year , month , weekday , offset ):
65
140
"""Get the day number based on a date and offset
66
141
@@ -302,7 +377,6 @@ def get_start_and_end_time(self, ref=None): # pylint: disable=unused-argument,n
302
377
:type ref: int
303
378
:return: None
304
379
"""
305
- logger .warning ("Calling function get_start_and_end_time which is not implemented" )
306
380
raise NotImplementedError ()
307
381
308
382
def is_time_valid (self , timestamp ):
@@ -375,13 +449,8 @@ def is_time_day_invalid(self, timestamp):
375
449
:type timestamp: int
376
450
:return: False if t in range, True otherwise
377
451
:rtype: bool
378
- TODO: Remove this function. Duplication
379
452
"""
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 )
385
454
386
455
def get_next_future_timerange_valid (self , timestamp ):
387
456
"""Get the next valid timerange (next timerange start in timeranges attribute)
@@ -665,11 +734,10 @@ def get_start_and_end_time(self, ref=None):
665
734
:param ref: time in seconds
666
735
:type ref: int
667
736
:return: tuple with start and end time
668
- :rtype: tuple
737
+ :rtype: tuple (int, int)
669
738
"""
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 ))
673
741
674
742
675
743
class StandardDaterange (AbstractDaterange ):
@@ -732,7 +800,7 @@ def get_start_and_end_time(self, ref=None):
732
800
:param ref: time in seconds
733
801
:type ref: int
734
802
:return: tuple with start and end time
735
- :rtype: tuple
803
+ :rtype: tuple (int, int)
736
804
"""
737
805
now = time .localtime (ref )
738
806
self .syear = now .tm_year
@@ -744,7 +812,7 @@ def get_start_and_end_time(self, ref=None):
744
812
day_diff = (day_id - now .tm_wday ) % 7
745
813
morning = datetime .fromtimestamp (today_morning ) + timedelta (days = day_diff )
746
814
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" )))
748
816
749
817
750
818
class MonthWeekDayDaterange (Daterange ):
@@ -823,7 +891,7 @@ def get_start_and_end_time(self, ref=None):
823
891
:param ref: time in seconds
824
892
:type ref: int
825
893
:return: tuple with start and end time
826
- :rtype: tuple
894
+ :rtype: tuple (int, int)
827
895
"""
828
896
now = time .localtime (ref )
829
897
if self .syear == 0 :
@@ -867,7 +935,7 @@ def get_start_and_end_time(self, ref=None):
867
935
:param ref: time in seconds
868
936
:type ref: int
869
937
:return: tuple with start and end time
870
- :rtype: tuple
938
+ :rtype: tuple (int, int)
871
939
"""
872
940
now = time .localtime (ref )
873
941
@@ -931,7 +999,7 @@ def get_start_and_end_time(self, ref=None):
931
999
:param ref: time in seconds
932
1000
:type ref: int
933
1001
:return: tuple with start and end time
934
- :rtype: tuple
1002
+ :rtype: tuple (int, int)
935
1003
"""
936
1004
now = time .localtime (ref )
937
1005
if self .syear == 0 :
0 commit comments