-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassDate.py
166 lines (143 loc) · 4.22 KB
/
ClassDate.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
classDate.py
Create a class named Date and call one of its class methods (monthsInYear).
Then create an object named d of class Date and call its instance methods.
"""
import sys
class Date(object):
"""
Class Date demonstrates class and instance attributes, class and instance methods.
It is a simple date class, containing year, month, and day integers.
"""
lengths = [
None,
31, #January
28, #February
31, #March
30, #April
31, #May
30, #June
31, #July
31, #August
30, #September
31, #October
30, #November
31 #December
]
january = 1
february = 2
march = 3
april = 4
may = 5
june = 6
july = 7
august = 8
september = 9
october = 10
november = 11
december = 12
def __init__(self, month, day, year):
assert isinstance(year, int)
assert isinstance(month, int) and 1 <= month and month <= Date.monthsInYear()
assert isinstance(day, int) and 1 <= day and day <= Date.lengths[month]
self.year = year
self.month = month
self.day = day
#These three methods are getters.
def getYear(self):
"Return my year."
return self.year
def getMonth(self):
"Return the number of my month (1 to 12 inclusive)."
return self.month
def getDay(self):
"Return the number of my day (1 to the length of my month, inclusive)."
return self.day
def __str__(self):
"Return a string that looks like the contents of myself."
return "{:02}/{:02}/{:04}".format(self.month, self.day, self.year)
def dayOfYear(self):
"Return my day of the year: a number in the range 1 to 365 inclusive."
return sum(Date.lengths[1:self.month]) + self.day
def nextDay(self):
"Move myself one day into the future."
if self.day < Date.lengths[self.month]:
self.day += 1
else:
self.day = 1 #Go to the first day of the next month.
if self.month < Date.monthsinyear:
self.month += 1
else:
self.month = 1 #Go to the first month of the next year.
self.year += 1
def nextDays(self, n):
"Move myself n days into the future."
assert isinstance(n, int) and n >= 0
for i in range(n):
self.nextDay() #Call the instance method in line 62.
def prevDay(self):
"""We're going back in time, Marty"""
if self.day > 0:
self.day -= 1
else:
self.day = Date.lengths[self.month]
if self.month > Date.january:
self.month -= 1
else:
self.month = Date.december
self.year -= 1
def prevDays(self, n):
assert isinstance(n, int) and n >= 0
for i in range(n):
self.prevDay()
def __sub__(self, other):
"""
Return the distance in days between the two Date objects.
The return value is positive is self is later than other,
negative if self is earlier than other, and zero if they're the same Date.
"""
iself = 365 * self.year + self.dayOfYear()
iother = 365 * other.year + other.dayOfYear()
return iself - iother
def __lt__(self, other):
"Return True if self is earlier than the other Date, False otherwise."
return self.dayOfYear() - other.dayOfYear() > 0
@staticmethod
def monthsInYear():
"Return the number of months in a year. This function is selfless."
return len(Date.lengths) - 1;
@staticmethod
def daysInYear():
"""Returns the days in year"""
return sum (Date.lengths[1:])
#The definition of class Date ends here.
d = Date(Date.january, 6, 2017) #Call the instance method in line 32.
d2 = Date(Date.november, 11, 2017)
print("months in year =", d.monthsInYear())
print("Days in year:",d.daysInYear())
print("type(d) =", type(d))
print()
#These three statements do the same thing:
print("d =", d)
print("d =", str(d))
print("d =", d.__str__()) #Call the instance method in line 54.
print("month =", d.getMonth()) #Call the instance method in line 46.
print("day =", d.getDay()) #Call the instance method in line 50.
print("year =", d.getYear()) #Call the instance method in line 42.
print()
print("{} is day number {} of the year {}.".format(d, d.dayOfYear(), d.getYear()))
d.nextDay() #Call the instance method in line 62.
print(d, "is the next day.")
d.nextDays(7) #Call the instance method in line 74.
print(d, "is a week after that.")
d.prevDays(8)
d.prevDay()
print(d, "was yesterday")
d.prevDays(7)
print(d, "was a week ago.")
if d > d2:
print(d,"comes after",d2)
else:
print(d,"comes before",d2)
print(d2 - d)
sys.exit(0)