Python - Add days to an existing date

前提是你 提交于 2020-01-15 06:35:06

问题


Obviously this is homework so I can't import but I also don't expect to be spoon fed the answer. Just need some help on something that's probably pretty simple, but has had me stumped for too many hours now. I have to add days to an existing date in python. Here is my code:

class Date:
    """
    A class for establishing a date.
    """
    min_year = 1800

    def __init__(self, month = 1, day = 1, year = min_year):
        """
        Checks to see if the date is real.
        """
        self.themonth = month
        self.theday = day
        self.theyear = year
    def __repr__(self):
        """
        Returns the date.
        """
        return '%s/%s/%s' % (self.themonth, self.theday, self.theyear)

    def nextday(self):
        """
        Returns the date of the day after given date.
        """
        m = Date(self.themonth, self.theday, self.theyear)
        monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        maxdays = monthdays[self.themonth]

        if self.theday != maxdays:
            return Date(self.themonth, self.theday+1, self.theyear)
        elif self.theday == maxdays and self.themonth == 12:
            return Date(1,1,self.theyear+1)
        elif self.theday == maxdays and self.themonth != 12:
            return Date(self.themonth+1, 1, self.theyear)

    def prevday(self):
        """
        Returns the date of the day before given date.
        """
        m = Date(self.themonth, self.theday, self.theyear)
        monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if self.theday == 1 and self.themonth == 1 and self.theyear == 1800:
            raise Exception("No previous date available.")
        if self.theday == 1 and self.themonth == 1 and self.theyear != 1800:
            return Date(12, monthdays[11], self.theyear-1)
        elif self.theday == 1 and self.themonth != 1:
            return Date(self.themonth -1, monthdays[self.themonth-1], self.theyear)
        elif self.theday != 1:
            return Date(self.themonth, self.theday - 1, self.theyear)

    def __add__(self, n):
        """
        Returns a new date after n days are added to given date.
        """
        for x in range(1, n+1):
            g = self.nextday()
        return g

But for some reason my __add__ method won't run nextday() the right amount of times. Any suggestions?


回答1:


It's running the correct number of times, but since nextday doesn't mutate a date object, you are just asking for the date after the current one over and over. Try:

def __add__(1, n):
    """
    Returns a new date after n days are added to given date.
    """
    g = self.nextday()
    for x in range(1, n):
        g = g.nextday()
    return g

With g = self.nextday(), you create a temporary object that is then incremented by repeatedly assigning itself to the following day. The range has to be changes to range(1,n) to compensate for the initial day, though I'd personally write it as range(0,n-1).



来源:https://stackoverflow.com/questions/29662605/python-add-days-to-an-existing-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!