Python Forum
finding yesterday and tomorrrow without using date.time module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding yesterday and tomorrrow without using date.time module
#2
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)
Reply


Messages In This Thread
RE: finding yesterday and tomorrrow without using date.time module - by scidam - Feb-22-2019, 12:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using zipfile module - finding folders not files darter1010 2 324 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Compare current date on calendar with date format file name Fioravanti 1 294 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 418 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 693 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,108 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,397 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Problem with module time and leap seconds Pedroski55 3 1,284 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Wait til a date and time KatManDEW 2 1,464 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  Module 'time' has no attribute 'clock' Sophie 4 3,181 Jan-25-2022, 08:05 PM
Last Post: Sophie
  Date format and past date check function Turtle 5 4,361 Oct-22-2021, 09:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020