Skip to content

Commit 33a3240

Browse files
#14 - python
1 parent 7ed94b7 commit 33a3240

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#14 { Retos para Programadores } FECHAS
2+
3+
# Bibliography reference
4+
# Professional JavaScript for web developers by Matt Frisbie [Frisbie, Matt] (z-lib.org)
5+
#Python Notes for Professionals. 800+ pages of professional hints and tricks (GoalKicker.com) (Z-Library)
6+
# Additionally, I use GPT as a reference and sometimes to correct or generate proper comments.
7+
8+
"""
9+
* EJERCICIO:
10+
* Crea dos variables utilizando los objetos fecha (date, o semejante) de tu lenguaje:
11+
* - Una primera que represente la fecha (día, mes, año, hora, minuto, segundo) actual.
12+
* - Una segunda que represente tu fecha de nacimiento (te puedes inventar la hora).
13+
* Calcula cuántos años han transcurrido entre ambas fechas.
14+
*
15+
* DIFICULTAD EXTRA (opcional):
16+
* Utilizando la fecha de tu cumpleaños, formatéala y muestra su resultado de
17+
* 10 maneras diferentes. Por ejemplo:
18+
* - Día, mes y año.
19+
* - Hora, minuto y segundo.
20+
* - Día de año.
21+
* - Día de la semana.
22+
* - Nombre del mes.
23+
* (lo que se te ocurra...)
24+
25+
"""
26+
27+
from datetime import datetime, timedelta
28+
29+
# Short for print
30+
log = print
31+
32+
# Simulating the window load event
33+
def on_load():
34+
body_style = {
35+
'background': '#000',
36+
'text-align': 'center'
37+
}
38+
39+
title = 'Retosparaprogramadores #14.'
40+
title_style = {
41+
'font-size': '3.5vmax',
42+
'color': '#fff',
43+
'line-height': '100vh'
44+
}
45+
46+
# Simulating setting styles (not applicable in console)
47+
log(f"Body styles: {body_style}")
48+
log(f"Title: {title} with styles: {title_style}")
49+
50+
# Simulating alert after 2 seconds
51+
log("Retosparaprogramadores #14")
52+
53+
# Call the on_load function to simulate the window load event
54+
on_load()
55+
56+
# Current date
57+
today = datetime.now()
58+
log(today) # Current date and time // 2024-12-25 06:22:05.153585
59+
log(today.date()) # Current date // 2024-12-25
60+
my_birthday = datetime(1983, 8, 8, 8, 30) # Birthday
61+
log(my_birthday) # Birthday date and time // 1983-08-08 08:30:00
62+
log(my_birthday.date()) # Birthday date // 1983-08-08
63+
log(my_birthday.strftime("%Y-%m-%d %I:%M:%S %p")) # Birthday in a specific format // 1983-08-08 08:30:00 AM
64+
65+
def calc_years_between(date1, date2):
66+
"""Calculate the years between two dates."""
67+
if date1 < date2:
68+
date1, date2 = date2, date1 # Swap if date1 is earlier
69+
70+
difference_in_days = (date1 - date2).days
71+
years = difference_in_days / 365.25 # Using 365.25 for leap years
72+
full_years = int(years)
73+
74+
# Check if the anniversary has not yet occurred this year
75+
if (date1.month < date2.month) or (date1.month == date2.month and date1.day < date2.day):
76+
full_years -= 1
77+
78+
return years
79+
80+
years_between = calc_years_between(today, my_birthday)
81+
log(f"Years between: {years_between:.2f}") # Display years with two decimal places // Years between: 41.38
82+
83+
def calc_years_between_simple(date1, date2):
84+
"""Calculate the simple year difference between two dates."""
85+
if date1 < date2:
86+
date1, date2 = date2, date1 # Swap if date1 is earlier
87+
88+
years = date1.year - date2.year
89+
if (date1.month < date2.month) or (date1.month == date2.month and date1.day < date2.day):
90+
years -= 1
91+
92+
return years
93+
94+
log(calc_years_between_simple(today, my_birthday)) # Simple year difference // 41
95+
96+
def calc_date_difference(date1, date2):
97+
"""Calculate the difference between two dates."""
98+
if date1 < date2:
99+
date1, date2 = date2, date1 # Swap if date1 is earlier
100+
101+
difference = date1 - date2
102+
103+
# Calculate total seconds
104+
total_seconds = difference.total_seconds()
105+
106+
# Calculate total days, weeks, months, and years
107+
total_days = difference.days
108+
years = date1.year - date2.year
109+
months = (date1.month - date2.month) + (years * 12)
110+
weeks = total_days // 7
111+
days = total_days % 7
112+
113+
# Calculate remaining hours, minutes, and seconds
114+
remaining_hours = (total_seconds // 3600) % 24
115+
remaining_minutes = (total_seconds // 60) % 60
116+
remaining_seconds = total_seconds % 60
117+
118+
return {
119+
'years': years,
120+
'months': months,
121+
'weeks': weeks,
122+
'days': days,
123+
'hours': remaining_hours,
124+
'minutes': remaining_minutes,
125+
'seconds': remaining_seconds
126+
}
127+
128+
difference = calc_date_difference(today, my_birthday)
129+
log(f"Difference: \n{difference['years']} years, \n{difference['months']} months, \n{difference['weeks']} weeks, \n{difference['days']} days, \n{difference['hours']} hours, \n{difference['minutes']} minutes, \n{difference['seconds']} seconds")
130+
"""
131+
Difference:
132+
41 years,
133+
496 months,
134+
2159 weeks,
135+
1 days,
136+
21.0 hours,
137+
52.0 minutes,
138+
5.153584957122803 seconds
139+
140+
"""
141+
142+
def format_birthday(birthday):
143+
"""Format birthday information."""
144+
day_month_year = birthday.strftime("%B %d, %Y")
145+
time = birthday.strftime("%I:%M:%S %p")
146+
day_of_year = (birthday - datetime(birthday.year, 1, 1)).days + 1
147+
day_of_week = birthday.strftime("%A")
148+
month_name = birthday.strftime("%B")
149+
iso_format = birthday.isoformat()
150+
short_format = birthday.strftime("%m/%d/%Y")
151+
long_format = birthday.strftime("%A, %B %d, %Y")
152+
time_12_hour = birthday.strftime("%I:%M:%S %p")
153+
time_with_timezone = birthday.strftime("%m/%d/%Y, %I:%M:%S %p") # Simulating timezone display
154+
155+
log("1. I was born on:", day_month_year) # 1. I was born on: August 08, 1983
156+
log("2. at:", time) # 2. at: 08:30:00 AM
157+
log("3. the day:", day_of_year, "of the year", birthday.year) # 3. the day: 220 of the year 1983
158+
log("4. on:", day_of_week) # 4. on: Monday
159+
log("5. one special day of:", month_name) # 5. one special day of: August
160+
log("6. isoFormat:", iso_format) # 6. isoFormat: 1983-08-08T08:30:00
161+
log("7. shortFormat:", short_format) # 7. shortFormat: 08/08/1983
162+
log("8. longFormat:", long_format) # 8. longFormat: Monday, August 08, 1983
163+
log("9. time12hour:", time_12_hour) # 9. time12hour: 08:30:00 AM
164+
log("10. timeWithTimezone:", time_with_timezone) # 10. timeWithTimezone: 08/08/1983, 08:30:00 AM
165+
166+
# Call the format_birthday function with my_birthday
167+
format_birthday(my_birthday)
168+
169+
# Note: The timezone information is not directly available in the datetime object without additional libraries.
170+
# The time_with_timezone output is simulated for demonstration purposes.

0 commit comments

Comments
 (0)