-
Notifications
You must be signed in to change notification settings - Fork 1
/
date.py
97 lines (80 loc) · 2.21 KB
/
date.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
def __repr__(self):
return f"Date({self.year}, {self.month:02}, {self.day:02})"
def __str__(self):
return f"{self.month:02}/{self.day:02}/{self.year}"
def is_leap_year(self):
"""
Returns True if the year is a leap year, False otherwise.
>>> d = Date(2024, 1, 1)
>>> d.is_leap_year()
True
>>> d = Date(2023, 1, 1)
>>> d.is_leap_year()
False
"""
if self.year % 4 == 0 and (self.year % 100 != 0 or self.year % 400 == 0):
return True
else:
return False
def is_valid(self):
"""
Returns True if the date is valid, False otherwise.
>>> d = Date(2023, 2, 28)
>>> d.is_valid()
True
>>> d = Date(2023, 2, 29)
>>> d.is_valid()
False
"""
if self.month < 1 or self.month > 12:
return False
if self.day < 1 or self.day > self.days_in_month():
return False
if self.year == 0:
return False
return True
def tomorrow(self):
"""
Update the Date object to refer to the next day.
>>> d = Date(2023, 2, 28)
>>> d.tomorrow()
>>> d
Date(2023, 03, 01)
>>> d = Date(2022, 12, 31)
>>> d.tomorrow()
>>> d
Date(2023, 01, 01)
"""
if self.day < self.days_in_month():
self.day += 1
else:
self.day = 1
if self.month < 12:
self.month += 1
else:
self.month = 1
self.year += 1
def days_in_month(self):
"""
Returns the number of days in the month.
>>> d = Date(2023, 2, 1)
>>> d.days_in_month()
28
>>> d = Date(2024, 2, 1)
>>> d.days_in_month()
29
"""
if self.month == 2:
if self.is_leap_year():
return 29
else:
return 28
elif self.month in {4, 6, 9, 11}:
return 30
else:
return 31