def get_start_and_end_time(self, ref=None): """Specific function to get start time and end time for WeekDayDaterange :param ref: time in seconds :type ref: int :return: tuple with start and end time :rtype: tuple """ now = time.localtime(ref) # If no year, it's our year if self.syear == 0: self.syear = now.tm_year month_start_id = now.tm_mon day_start = find_day_by_weekday_offset(self.syear, month_start_id, self.swday, self.swday_offset) start_time = get_start_of_day(self.syear, month_start_id, day_start) # Same for end year if self.eyear == 0: self.eyear = now.tm_year month_end_id = now.tm_mon day_end = find_day_by_weekday_offset(self.eyear, month_end_id, self.ewday, self.ewday_offset) end_time = get_end_of_day(self.eyear, month_end_id, day_end) # Maybe end_time is before start. So look for the # next month if start_time > end_time: month_end_id += 1 if month_end_id > 12: month_end_id = 1 self.eyear += 1 day_end = find_day_by_weekday_offset(self.eyear, month_end_id, self.ewday, self.ewday_offset) end_time = get_end_of_day(self.eyear, month_end_id, day_end) now_epoch = time.mktime(now) # But maybe we look not enought far. We should add a month if end_time < now_epoch: month_end_id += 1 month_start_id += 1 if month_end_id > 12: month_end_id = 1 self.eyear += 1 if month_start_id > 12: month_start_id = 1 self.syear += 1 # First start day_start = find_day_by_weekday_offset(self.syear, month_start_id, self.swday, self.swday_offset) start_time = get_start_of_day(self.syear, month_start_id, day_start) # Then end day_end = find_day_by_weekday_offset(self.eyear, month_end_id, self.ewday, self.ewday_offset) end_time = get_end_of_day(self.eyear, month_end_id, day_end) return (start_time, end_time)
def get_start_and_end_time(self, ref=None): """Specific function to get start time and end time for MonthDayDaterange :param ref: time in seconds :type ref: int :return: tuple with start and end time :rtype: tuple """ now = time.localtime(ref) if self.syear == 0: self.syear = now.tm_year month_start_id = now.tm_mon day_start = find_day_by_offset(self.syear, month_start_id, self.smday) start_time = get_start_of_day(self.syear, month_start_id, day_start) if self.eyear == 0: self.eyear = now.tm_year month_end_id = now.tm_mon day_end = find_day_by_offset(self.eyear, month_end_id, self.emday) end_time = get_end_of_day(self.eyear, month_end_id, day_end) now_epoch = time.mktime(now) if start_time > end_time: month_start_id -= 1 if month_start_id < 1: month_start_id = 12 self.syear -= 1 day_start = find_day_by_offset(self.syear, month_start_id, self.smday) start_time = get_start_of_day(self.syear, month_start_id, day_start) if end_time < now_epoch: month_end_id += 1 month_start_id += 1 if month_end_id > 12: month_end_id = 1 self.eyear += 1 if month_start_id > 12: month_start_id = 1 self.syear += 1 # For the start day_start = find_day_by_offset(self.syear, month_start_id, self.smday) start_time = get_start_of_day(self.syear, month_start_id, day_start) # For the end day_end = find_day_by_offset(self.eyear, month_end_id, self.emday) end_time = get_end_of_day(self.eyear, month_end_id, day_end) return (start_time, end_time)
def get_start_and_end_time(self, ref=None): """Specific function to get start time and end time for MonthWeekDayDaterange :param ref: time in seconds :type ref: int | None :return: tuple with start and end time :rtype: tuple """ now = time.localtime(ref) if self.syear == 0: self.syear = now.tm_year day_start = find_day_by_weekday_offset(self.syear, self.smon, self.swday, self.swday_offset) start_time = get_start_of_day(self.syear, self.smon, day_start) if self.eyear == 0: self.eyear = now.tm_year day_end = find_day_by_weekday_offset(self.eyear, self.emon, self.ewday, self.ewday_offset) end_time = get_end_of_day(self.eyear, self.emon, day_end) now_epoch = time.mktime(now) if start_time > end_time: # the period is between years if now_epoch > end_time: # check for next year day_end = find_day_by_weekday_offset(self.eyear + 1, self.emon, self.ewday, self.ewday_offset) end_time = get_end_of_day(self.eyear + 1, self.emon, day_end) else: # it s just that the start was the last year day_start = find_day_by_weekday_offset(self.syear - 1, self.smon, self.swday, self.swday_offset) start_time = get_start_of_day(self.syear - 1, self.smon, day_start) else: if now_epoch > end_time: # just have to check for next year if necessary day_start = find_day_by_weekday_offset(self.syear + 1, self.smon, self.swday, self.swday_offset) start_time = get_start_of_day(self.syear + 1, self.smon, day_start) day_end = find_day_by_weekday_offset(self.eyear + 1, self.emon, self.ewday, self.ewday_offset) end_time = get_end_of_day(self.eyear + 1, self.emon, day_end) return (start_time, end_time)
def get_start_and_end_time(self, ref=None): """Specific function to get start time and end time for CalendarDaterange :param ref: time in seconds :type ref: int :return: tuple with start and end time :rtype: tuple """ start_time = get_start_of_day(self.syear, int(self.smon), self.smday) end_time = get_end_of_day(self.eyear, int(self.emon), self.emday) return (start_time, end_time)
def get_start_and_end_time(self, ref=None): """Specific function to get start time and end time for StandardDaterange :param ref: time in seconds :type ref: int :return: tuple with start and end time :rtype: tuple """ now = time.localtime(ref) self.syear = now.tm_year self.month = now.tm_mon # month_start_id = now.tm_mon # month_start = Daterange.get_month_by_id(month_start_id) self.wday = now.tm_wday day_id = Daterange.get_weekday_id(self.day) today_morning = get_start_of_day(now.tm_year, now.tm_mon, now.tm_mday) tonight = get_end_of_day(now.tm_year, now.tm_mon, now.tm_mday) day_diff = (day_id - now.tm_wday) % 7 return (today_morning + day_diff * 86400, tonight + day_diff * 86400)
def get_start_and_end_time(self, ref=None): """Specific function to get start time and end time for StandardDaterange :param ref: time in seconds :type ref: int :return: tuple with start and end time :rtype: tuple """ now = time.localtime(ref) self.syear = now.tm_year self.month = now.tm_mon self.wday = now.tm_wday day_id = Daterange.get_weekday_id(self.day) today_morning = get_start_of_day(now.tm_year, now.tm_mon, now.tm_mday) tonight = get_end_of_day(now.tm_year, now.tm_mon, now.tm_mday) day_diff = (day_id - now.tm_wday) % 7 morning = datetime.fromtimestamp(today_morning) + timedelta( days=day_diff) night = datetime.fromtimestamp(tonight) + timedelta(days=day_diff) return (float(morning.strftime("%s")), float(night.strftime("%s")))