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
#1
self learning python and been stuck trying to figure this out.
Trying to output both the day before and the day after a given date without using the date.time module.
script so far:

#!/usr/bin/env python3


####enter date in yyyymmdd format

def dbda(today):
    if len (today) != 8:
       return '00000000'
    else:
       year = int(today[0:4])
       month = int(today[4:6])
       day = int(today[6:])

####calculate leap year

       lyear = year % 4
       if lyear == 0:
          feb_max = 29
       else:
          feb_max = 28

       lyear = year % 100
       if lyear == 0:
          feb_max = 28

       lyear = year % 400
       if lyear == 0:
          feb_max = 29

       next_day = day + 1 #tomorrow
       pass_day = day - 1 #yesterday

       mon_max = { 1:31, 2:feb_max, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
Reply
#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
#3
Much thanks.
Reply
#4
Just amended the above script by adding the 'day before'. Cheers
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 ' {}.{}.{}'.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):
        """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)
         
 
d = Date(28, 2, 2020)
print("Current date:  ", d)
print("Next day:      ", d.day_after)
print("Two days after:", d.day_after.day_after)
print("Day before:    ", d.day_before)
# ----------- OUTPUT ---------------
#Current date:    28.2.2020
#Next day:        29.2.2020
#Two days after:  1.3.2020
#Day before:      27.2.2020
#-----------------------------------
Reply
#5
is there a way to make so that it takes any date instead of just one given date
Reply
#6
(Feb-22-2019, 10:40 PM)apexman Wrote: is there a way to make so that it takes any date instead of just one given date
Of course, just modifying this line inside the code source:
d = Date(28, 2, 2020)
Cheers
Reply
#7
Better to have the yearly calendar, as shown underneath. Cheers
# ------- mycal.py ----------
import calendar
calendar.prcal(2020)
# ------- OUTPUT ------------
#                                   2020
#
#       January                   February                   March
# Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
#        1  2  3  4  5                      1  2                         1
#  6  7  8  9 10 11 12       3  4  5  6  7  8  9       2  3  4  5  6  7  8
# 13 14 15 16 17 18 19      10 11 12 13 14 15 16       9 10 11 12 13 14 15
# 20 21 22 23 24 25 26      17 18 19 20 21 22 23      16 17 18 19 20 21 22
# 27 28 29 30 31            24 25 26 27 28 29         23 24 25 26 27 28 29
#                                                     30 31
#
#        April                      May                       June
# Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
#        1  2  3  4  5                   1  2  3       1  2  3  4  5  6  7
#  6  7  8  9 10 11 12       4  5  6  7  8  9 10       8  9 10 11 12 13 14
# 13 14 15 16 17 18 19      11 12 13 14 15 16 17      15 16 17 18 19 20 21
# 20 21 22 23 24 25 26      18 19 20 21 22 23 24      22 23 24 25 26 27 28
# 27 28 29 30               25 26 27 28 29 30 31      29 30
#
#         July                     August                  September
# Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
#        1  2  3  4  5                      1  2          1  2  3  4  5  6
#  6  7  8  9 10 11 12       3  4  5  6  7  8  9       7  8  9 10 11 12 13
# 13 14 15 16 17 18 19      10 11 12 13 14 15 16      14 15 16 17 18 19 20
# 20 21 22 23 24 25 26      17 18 19 20 21 22 23      21 22 23 24 25 26 27
# 27 28 29 30 31            24 25 26 27 28 29 30      28 29 30
#                           31
#
#       October                   November                  December
# Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
#           1  2  3  4                         1          1  2  3  4  5  6
#  5  6  7  8  9 10 11       2  3  4  5  6  7  8       7  8  9 10 11 12 13
# 12 13 14 15 16 17 18       9 10 11 12 13 14 15      14 15 16 17 18 19 20
# 19 20 21 22 23 24 25      16 17 18 19 20 21 22      21 22 23 24 25 26 27
# 26 27 28 29 30 31         23 24 25 26 27 28 29      28 29 30 31
#                           30
#
Reply
#8
(Feb-22-2019, 08:49 PM)samsonite Wrote: def day_before(self):
"""Returns date = -1 day + current date
"""

Does not work correctly: if you pass, e.g. d = Date(1,1,2020), than d.day_before causes an error.
Reply
#9
Yes, scidam, you are right. As a matter of generality, such a code is too weak, it's preferable refer to module calendar, as per my previous post. Thank you
Reply
#10
much thanks for all the replies.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 121 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 312 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 516 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Problem with module time and leap seconds Pedroski55 3 1,189 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  Module 'time' has no attribute 'clock' Sophie 4 3,032 Jan-25-2022, 08:05 PM
Last Post: Sophie
  Date format and past date check function Turtle 5 4,069 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Have to use Python 2.x to find "yesterday' files tester_V 6 2,770 Sep-19-2021, 12:26 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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