Skip to content

Commit 0650423

Browse files
committed
reto #14 - python
1 parent e6cc18f commit 0650423

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from datetime import datetime
2+
import random
3+
import functools
4+
from typing import Iterable, Final
5+
6+
7+
def format_date(date: datetime) -> str:
8+
return f"{date:%A, %d de %B de %Y - %I:%M %p}"
9+
10+
11+
def calculate_year_difference(date1: datetime, date2: datetime) -> int:
12+
return abs((date1 - date2).days) // 365
13+
14+
15+
@functools.lru_cache(maxsize=25)
16+
def date_formats(date: datetime) -> list[str]:
17+
return [
18+
f"{date:%Y-%m-%d}", # Año-Mes-Día
19+
f"{date:%d/%m/%Y}", # Día/Mes/Año
20+
f"{date:%B %d, %Y}", # Mes Nombre Día, Año
21+
f"{date:%A, %d %B %Y}", # Día de la semana, Día Mes Nombre Año
22+
f"{date:%Y-%m-%d %H:%M:%S}", # Año-Mes-Día Hora:Minuto:Segundo
23+
f"{date:%d-%m-%Y %H:%M}", # Día-Mes-Año Hora:Minuto
24+
f"{date:%Y/%m/%d %H:%M:%S}", # Año/Mes/Día Hora:Minuto:Segundo
25+
f"{date:%A, %d de %B de %Y}", # Día de la semana, Día de Mes Nombre Año
26+
f"{date:%d/%m/%Y %H:%M:%S}", # Día/Mes/Año Hora:Minuto:Segundo
27+
f"{date:%Y-%m-%d %H:%M:%S.%f}", # Año-Mes-Día Hora:Minuto:Segundo.Microsegundos
28+
]
29+
30+
31+
def choice_random_elements[T](elements: Iterable[T], quantity: int) -> list[T]:
32+
return random.sample(population=elements, k=quantity)
33+
34+
35+
def display_formatted_date_in_various_ways(date: datetime, quantity: int = 10) -> None:
36+
MAXIMUM_ALLOWED_FORMATS: Final = 10
37+
38+
if quantity > MAXIMUM_ALLOWED_FORMATS:
39+
print("la cantidad maxima de fechas a imprimir es 10")
40+
41+
quantity = min(quantity, MAXIMUM_ALLOWED_FORMATS)
42+
available_formats = date_formats(date=date)
43+
display_random_formats = choice_random_elements(
44+
elements=available_formats, quantity=quantity
45+
)
46+
47+
print(*display_random_formats, sep="\n")
48+
49+
50+
def main() -> None:
51+
current_date = datetime.now()
52+
BIRTH_DATE = datetime(
53+
year=2001, month=2, day=11, hour=5, minute=10, second=30, microsecond=0
54+
)
55+
56+
print(format_date(date=current_date))
57+
print(format_date(date=BIRTH_DATE))
58+
print(calculate_year_difference(date1=current_date, date2=BIRTH_DATE))
59+
display_formatted_date_in_various_ways(date=BIRTH_DATE, quantity=20)
60+
61+
62+
if __name__ == "__main__":
63+
main()

0 commit comments

Comments
 (0)