Python-将天数添加到现有日期 [英] Python - Add days to an existing date

查看:95
本文介绍了Python-将天数添加到现有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很显然,这是家庭作业,因此我不能导入,但是我也不希望被汤匙喂.只是需要一些可能很简单的帮助,但是现在让我陷入困境的时间已经太多了.我必须在python中将天数添加到现有日期.这是我的代码:

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

但是由于某种原因,我的__add__方法无法在正确的时间内运行nextday().有什么建议吗?

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

推荐答案

它正在运行正确的次数,但是由于nextday不会使date对象发生突变,因此您只是在询问当前日期之后的日期.一遍又一遍.试试:

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

使用g = self.nextday(),您将创建一个临时对象,然后通过将其自身重复分配给第二天来增加该临时对象.该范围必须更改为range(1,n)以补偿初始日期,尽管我个人将其写为range(0,n-1).

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).

这篇关于Python-将天数添加到现有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆