Feb-22-2019, 12:28 PM
I think, it would be better to create class that describes dates.
class Date: feb_max_length = 29 allowed_month_lengths = {1: 31, 2: feb_max_length - 1, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31 } def __init__(self, day, month, year): self.day = day self.month = month self.year = year self._validate() def _validate(self): if not(isinstance(self.year, int) and isinstance(self.month, int) and isinstance(self.day, int)): raise Exception("Error: year, month, day should be integers.") if not(1 <= self.month <= 12): raise Exception("Error: month should be in [1, 12], your value is {}.".format(self.month)) if not (1 <= self.day <= self.month_length): raise Exception("Error: day number ({}) should be less or equal {}.".format(self.day, self.month_length)) def __str__(self): return 'Date: {}.{}.{}'.format(self.day, self.month, self.year) def __repr__(self): return self.__str__() @property def is_leap_year(self): return self.year % 4 == 0 and (self.year % 100 != 0 or self.year % 400 == 0) @property def month_length(self): _ = self.allowed_month_lengths.get(self.month, None) return _ if not self.is_leap_year else (_ + 1 if self.month == 2 else _) @property def day_after(self): """Returns date = 1 day + current date """ if 1 <= self.day < self.month_length: return Date(self.day + 1, self.month, self.year) if self.day == self.month_length and self.month == 12: return Date(1, 1, self.year + 1) elif self.day == self.month_length and self.month < 12: return Date(1, self.month + 1, self.year) @property def day_before(self): raise NotImplementedError d = Date(28, 2, 2019) print("Current date:", d) print("Next day: ", d.day_after) print("Two days after:", d.day_after.day_after)