def isIntervalActive(self): try: strBeginTime = self.getStartTime() strEndTime = self.getEndTime() dateToday = getDateToday() curTime = DateTime() calculatedStartTime = ParseTimeStringToDate(dateToday, self._begin) calculatedEndTime = ParseTimeStringToDate(dateToday, self._end) #End is before start -> interval is over midnight if calculatedEndTime.isBefore(calculatedStartTime): if (curTime.isAfter(calculatedStartTime)): calculatedEndTime = calculatedEndTime.plusDays(1) elif (curTime.isBefore(calculatedEndTime)): calculatedStartTime = calculatedStartTime.minusDays(1) else: calculatedEndTime = calculatedEndTime.plusDays(1) getLogger().debug(( "Processing interval '{}' - '{}' -> '{}' - '{}' - Current Time '{}', isActive='{}'" ).format(self.getStartTime(), self.getEndTime(), calculatedStartTime, calculatedEndTime, curTime, (curTime.isAfter(calculatedStartTime) and curTime.isBefore(calculatedEndTime)))) return (curTime.isAfter(calculatedStartTime) and curTime.isBefore(calculatedEndTime)) except: LogException() return False
def isActive(self): try: curDateTime = DateTime() if (self._arrSunInfo is not None): sunInfoYesterday = self._arrSunInfo[0] sunInfoToday = self._arrSunInfo[1] sunInfoTomorrow = self._arrSunInfo[2] #After Sunset yesterday, Before sunrise Today -> Night if (curDateTime.isAfter(sunInfoYesterday.getSunsetStart()) and curDateTime.isBefore(sunInfoToday.getSunriseEnd())): getLogger().debug( ("NIGHT -> '{}' is between '{}' - '{}'").format( curDateTime, sunInfoYesterday.getSunsetStart(), sunInfoToday.getSunriseEnd())) return True #After Sunrise today, Before sunset Today -> Day if (curDateTime.isAfter(sunInfoToday.getSunriseEnd()) and curDateTime.isBefore(sunInfoToday.getSunsetStart())): getLogger().debug( ("DAY -> '{}' is between '{}' - '{}'").format( curDateTime, sunInfoToday.getSunriseEnd(), sunInfoToday.getSunsetStart())) return False #After Sunset today, Before Sunrise Tomorrow -> Night elif (curDateTime.isAfter(sunInfoToday.getSunsetStart()) and curDateTime.isBefore(sunInfoTomorrow.getSunriseEnd())): getLogger().debug( ("NIGHT -> '{}' is between '{}' - '{}'").format( curDateTime, sunInfoToday.getSunsetStart(), sunInfoTomorrow.getSunriseEnd())) return True getLogger().warn( ("'{}' didn't evalute night/day '{}', '{}', '{}', '{}'" ).format(curDateTime, sunInfoYesterday.getSunsetStart(), sunInfoToday.getSunriseEnd(), sunInfoToday.getSunsetStart(), sunInfoTomorrow.getSunriseEnd())) else: getLogger().warn("No SunInfo data found - Evaluating 'False'") return False except: LogException() return False
def run(self, func, when): """If it has been long enough since the last time that run was called, execute the passed in func. Otherwise ignore the call. Arguments: - func: The lambda or function to call if allowed. - when: When the rate limit will expire. Can be a DateTime type object, a number which is treated as seconds, or a duration string (e.g. 5m 2.5s), or an ISO 8601 formatted date string. See time_utils for details """ now = DateTime().now() if now.isAfter(self.until): self.until = to_datetime(when) func()