在课堂上处理过期

时间:2016-05-04 09:10:20

标签: python-2.7 class date

如何在class dateclass delta处理过期 这是我class date的代码。

class date(object):
    def __init__(self, year, month, day):
        self.year=year
        self.month=month
        self.day=day
    def __add__(self, era):
        year=self.year+era.year
        month=self.month+era.month
        day=self.day+era.day
        return date(year, month, day)
    def __str__(self):
        return "("+",".join(map(str,(self.year,self.month,self.day)))+")"

这适用于class delta

class delta(object):
    def __init__(self, year=1978, month=12, day=30):
        self.year=year
        self.month=month
        self.day=day
    def __add__(self, era):
        return self+era

如果我发出这样的命令。

>>> date(2016, 5, 30)+delta(year=5, month=3, day=30)

必须是 date(2021, 9, 1)

不喜欢这个 date(2012, 8, 60)

month的最高值必须为12,而day的最高为30

此代码未使用import

我正在使用python2.7

1 个答案:

答案 0 :(得分:0)

只需使用包含datetime类的内置timedelta模块,该模块执行delta所做的操作。它包含在Python中,无需重新发明它。

相关问题