-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.py
97 lines (83 loc) · 2.69 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
from typing import List, Optional, NoReturn, TypeVar
#funcion para ver si a fecha introducida es correcta. tenemos que tener en cuenta los años bisiestos por los dias que varian en febrero.
def correctDate(month:int, day:int, year:int) -> bool:
if (year % 4 == 0) and ((year % 100 != 0) or (year % 100 == 0 and year % 400 == 0)):
if month == 2:
if 0 < day < 30:
return True
else:
return False
elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
if 0 < day < 32:
return True
else:
return False
elif month == 4 or month == 6 or month == 9 or month == 11:
if 0 < day < 31:
return True
else:
return False
else:
if month == 2:
if 0 < day < 29:
return True
else:
return False
elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
if 0 < day < 32:
return True
else:
return False
elif month == 4 or month == 6 or month == 9 or month == 11:
if 0 < day < 31:
return True
else:
return False
class Date():
def __init__(self, day:int, month:int, year:int):
if not correctDate(month, day, year):
raise ValueError('Incorrect Date')
self.__month: int = month
self.__day: int = day
self.__year: int = year
@property
def month(self):
return self.__month
@property
def day(self):
return self.__day
@property
def year(self):
return self.__year
def __str__(self):
if self.__month < 10:
if self.__day < 10:
return f'{self.__year}-0{self.__month}-0{self.__day}'
else:
return f'{self.__year}-0{self.__month}-{self.__day}'
else:
if self.__day < 10:
return f'{self.__year}-{self.__month}-0{self.__day}'
else:
return f'{self.__year}-{self.__month}-{self.__day}'
def __eq__(self, other):
return self.__day == other.day and self.__month == other.month and self.__year == other.year
def __lt__(self, other):
return (self.__year < other.year) or (self.__year == other.year and self.__month < other.month) or (self.__year == other.year and self.__month == other.month and self.__day < other.day)
def test():
#código de prueba
try:
fecha1: Date(29, 2, 2017)
except:
print('ERROR')
#debe dar error
fecha1: Date = Date(29, 2, 2020)
fecha2: Date = Date(29, 3, 2017)
fecha3: Date = Date(29, 3, 2020)
fecha4: Date = Date(2, 2, 2017)
fecha5: Date = Date(2, 10, 2017)
print(fecha1 > fecha2) #debe imprimir True
print(fecha1 < fecha3) #debe imprimir True
print(fecha3) #debe ser 29/03/2020
print(fecha4) #debe ser 02/02/2017
print(fecha5) #debe ser 02/10/2017