-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdateClass.py
More file actions
129 lines (117 loc) · 4.09 KB
/
dateClass.py
File metadata and controls
129 lines (117 loc) · 4.09 KB
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
import sys
class MyDate:
'''
MyDate: A simple implementation of date as a class.
'''
def __init__(self, day=1, month=1, year=2000):
'''
Objective: To initialize data members of object MyDate
Input Parameters:
self (implicit parameter) - object of type MyDate
day, month, and year - int
Return Value: None
'''
if not (type(day) == int and type(month) == int and
type(year) == int):
print('Invalid data provided for date')
sys.exit()
if month > 0 and month <= 12:
self.month = month
else:
print('Invalid value for month')
sys.exit()
if year > 1900:
self.year = year
else:
print('Invalid value for year. Year should be greater\
than 1900.')
sys.exit()
self.day = self.checkDay(day) # validate day
def checkDay(self, day):
'''
Objective: To validate day component
Input Parameters:
self (implicit parameter) - object of type MyDate
day - numeric
Return Value: day if it is correct else message 'Invalid
value for the day' is printed and the program is terminated
'''
# currentYear: list of no of days in months of current year
if self.year % 400 == 0 or (self.year % 100 != 0 and self.year % 4 == 0): # if leap year
currentYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
currentYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (day > 0 and day <= currentYear[self.month - 1]):
return day
else:
print('Invalid value for day')
sys.exit()
def __str__(self):
'''
Objective: To return string representation of object
Input Parameter:
self (implicit parameter) - object of type MyDate
Return Value: string
'''
# Approach: use dd-mm-yyyy format
if self.day <= 9:
day = '0' + str(self.day)
else:
day = str(self.day)
if self.month <= 9:
month = '0' + str(self.month)
else:
month = str(self.month)
return day + '-' + month + '-' + str(self.year)
def __eq__(self, other):
'''
Objective: To compare two MyDate objects for equality
Input Parameters:
self (implicit parameter) - object of type MyDate
other - object of type MyDate
Return Value: True if equal else False
'''
# check for equality of year, month, day
if isinstance(other, MyDate):
return (self.day == other.day) and (self.month == other.month) \
and (self.year == other.year)
else:
print('Type Mismatch')
sys.exit()
def __lt__(self, other):
'''
Objective: To compare two MyDate objects
Input Parameters:
self (implicit parameter) - object of type MyDate
other - object of type MyDate
Return Value: True if value of first MyDate object is less
than seconds, else False
'''
# compare by year, month, day successively
if isinstance(other, MyDate):
if self.year < other.year:
answer = True
elif self.year == other.year and \
self.month < other.month:
answer = True
elif self.year == other.year and self.month == other.month \
and self.day < other.day:
answer = True
else:
answer = False
return answer
else:
print('Type Mismatch')
sys.exit()
def main():
'''
Objective: To create objects of class Date and to perform operations on it
Input Parameter: None
Return Value: None
'''
today = MyDate(3, 9, 2014)
print(today)
defaultDate = MyDate()
print(defaultDate)
if __name__ == '__main__':
main()