-
Notifications
You must be signed in to change notification settings - Fork 0
/
dateClass.py
70 lines (57 loc) · 2.09 KB
/
dateClass.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
class Date:
days_in_months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
cum_days_in_months = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
month_names = ["", "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"]
def __init__(self, day, month, year):
if not 1 <= month <= 12:
print("Invalid month")
raise ValueError
self.month = month
self.day = day
self.year = year
if not 1 <= day <= Date.days_in_months[month] + self.leap_adjustment():
print("Invalid day")
raise ValueError
def __repr__(self):
return "%02d %s, %d" %(self.day, (Date.month_names[self.month])[:3], self.year)
# logic: a number is divisible by 4 if its last 2 digits are divisible by 4
def is_leap_year(self):
yy = self.year // 100 if self.year % 100 == 0 else self.year % 100
return yy % 4 == 0
def leap_adjustment(self):
return int(self.month == 2 and self.is_leap_year())
def is_last_day(self):
return self.day == Date.days_in_months[self.month] + leap_adjustment
def is_first_day(self):
return self.day == 1
def tomorrow(self):
if self.is_last_day():
if self.month == 12:
self.day, self.month = 1, 1
self.year += 1
else:
self.day = 1
self.month += 1
else:
self.day += 1
def next_day(self, step = 1):
for _ in range(step):
self.tomorrow()
def yesterday(self):
if self.is_first_day():
if self.month == 1:
self.day, self.month = 31, 12
self.year -= 1
else:
self.month -= 1
self.day = Date.days_in_months[self.month] +self.leap_adjustment()
else:
self.day -= 1
def prev_day(self, step = 1):
for _ in range(step):
self.yesterday()
d = Date(29, 2,2000)
print(d)
d.yesterday()
print(d)