def setTimeSpan(self, iter_start, iter_end): if isinstance(iter_start, (list, tuple)) and len(iter_start) == 2: iter_start = (iter_start[0], iter_start[1], lastDayOfMonth(*iter_start)) self.iter_start = asDatetime(iter_start) if isinstance(iter_end, (list, tuple)) and len(iter_end) == 2: iter_end = (iter_end[0], iter_end[1], lastDayOfMonth(*iter_end)) self.iter_end = asDatetime(iter_end)
def yearsInterval(date, interval): # always return a calendar year year = date.year + interval # passed date is the last day of a month if date.day == lastDayOfMonth(date.year, date.month): return datetime.datetime(year, date.month, lastDayOfMonth(year, date.month)) # passed date is not last day of month else: return datetime.datetime(year, date.month, date.day)
def yearsDuration(date, duration): # always return a calendar year year = date.year - duration # # date is the last day of a month if date.day == lastDayOfMonth(date.year, date.month): day = lastDayOfMonth(year, date.month) # in case it is Febraury _date = datetime.datetime(year, date.month, day) # date is not last day of month else: _date = datetime.datetime(year, date.month, date.day) return _date + relativedelta(days=1)
def getTimeSpanFromArgs(args): if len(args) < 2: return None, None start_year = int(args[0]) start_month = int(args[1]) if len(args) == 2: # find all hours in a month start_time = (start_year, start_month, 1, 1) end_time = (start_year, start_month, lastDayOfMonth(start_year, start_month), 24) elif len(args) == 3: # find hours in a day day = int(args[2]) start_time = (start_year, start_month, day, 1) end_time = (start_year, start_month, day, 24) elif len(args) == 4: if len(args[2]) == 4: # all hours in a span of months start_time = (start_year, start_month, 1, 1) end_year = int(args[2]) end_month = int(args[3]) end_time = (end_year, end_month, lastDayOfMonth(end_year, end_month), 24) else: # one single hour start_time = (start_year, start_month, int(args[2]), int(args[3])) end_time = start_time elif len(args) == 5: # span of hours in a single day day = int(args[2]) start_time = (start_year, start_month, day, int(args[3])) end_time = (start_year, start_month, day, int(args[4])) elif len(args) == 6: # span of multiple full days start_time = (start_year, start_month, int(args[2]), 1) end_time = (int(args[3]), int(args[4]), int(args[5]), 24) elif len(args) == 8: # span of multiple days start_time = (start_year, start_month, int(args[2]), int(args[3])) end_time = (int(args[4]), int(args[5]), int(args[6]), int(args[7])) else: errmsg = 'Unable to determine date range from command line arguments' raise ValueError, errmsg return start_time, end_time
def monthsInterval(date, interval): # always return a calendar month month = date.month + interval if month > 12: month -= 12 year = date.year + 1 else: year = date.year # passed date is the last day of a month if date.day == lastDayOfMonth(date.year, date.month): return datetime.datetime(year, month, lastDayOfMonth(year, month)) # passed date is not last day of month else: return datetime.datetime(year, month, date.day)
def setTimeSpan(self, iter_start, iter_end): if isinstance(iter_start, (list, tuple)): if len(iter_start) == 2: iter_start = (iter_start[0], iter_start[1], lastDayOfMonth(*iter_start)) iter_start = datetime.date(*iter_start) elif not isinstance(iter_start, datetime.date): errmsg = 'Invalid type for iter_start : %s' raise TypeError, errmsg % (iter_start.__class__.__name__, str(iter_start)) if isinstance(iter_end, (list, tuple)): if len(iter_end) == 2: iter_end = (iter_end[0], iter_end[1], lastDayOfMonth(*iter_end)) iter_end = datetime.date(*iter_end) elif not isinstance(iter_end, datetime.date): errmsg = 'Invalid type for iter_end : %s' raise TypeError, errmsg % (iter_end.__class__.__name__, str(iter_end)) return iter_start, iter_end
def monthsDuration(date, duration): # always return a calendar month # # date is the last day of a month if date.day == lastDayOfMonth(date.year, date.month): month = date.month - duration + 1 if month < 1: month += 12 year = date.year - 1 else: year = date.year return datetime.datetime(year, month, 1) # day is always 1 # date is not last day of month else: month = date.month - duration if month < 1: month += 12 year = date.year - 1 else: year = date.year _date = datetime.datetime(year, month, date.day) return _date + relativedelta(days=1)