Skip to content

Commit

Permalink
Cálculo de próximo directo
Browse files Browse the repository at this point in the history
  • Loading branch information
mouredev committed Feb 24, 2024
1 parent bbb1efd commit 59464da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
35 changes: 19 additions & 16 deletions link_bio/link_bio/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import date, datetime, timedelta, timezone
from datetime import datetime, timedelta, timezone
import pytz
import locale
import reflex as rx

# Común
Expand Down Expand Up @@ -44,35 +46,36 @@ def lang() -> rx.Component:

def next_date(dates: dict) -> str:

# Se fuerza el locale para traducir el formateo de fecha a español
locale.setlocale(locale.LC_TIME, "es_ES")

if len(dates) == 0:
return ""

now = datetime.now()
current_weekday = now.weekday()
current_time = now.astimezone().timetz()

for index in range(7):
for weekday in range(7):

day = str((current_weekday + index) % 7)
current_weekday = str((now.weekday() + weekday) % 7)

if day not in dates or dates[day] == "":
if current_weekday not in dates or dates[current_weekday] == "":
continue

time_utc = datetime.strptime(
dates[day],
"%H:%M"
).replace(tzinfo=timezone.utc).timetz()
time_utc = datetime.strptime(dates[current_weekday], "%H:%M").replace(
tzinfo=pytz.UTC).timetz()

time = datetime.combine(now.date(), time_utc).astimezone().timetz()
next_time = datetime.combine(
now.date(), time_utc).astimezone().timetz()

if current_time < time or index > 0:
if current_time < next_time or weekday > 0:

next_date = now + timedelta(days=index)
next_date = now + timedelta(days=weekday)

formatted_next_date = next_date.strftime(
"Hoy, %d/%m") if index == 0 else next_date.strftime("%A, %d/%m")
formatted_next_time = time.strftime("%H:%M")
local_date = datetime(
next_date.year, next_date.month, next_date.day,
time_utc.hour, time_utc.minute, tzinfo=pytz.UTC).astimezone()

return f"{formatted_next_date} a las {formatted_next_time}"
return local_date.strftime("%A, %d de %B a las %H:%M").capitalize()

return ""
3 changes: 2 additions & 1 deletion link_bio/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pip==24.0
reflex==0.4.1
python-dotenv==1.0.1
supabase==2.3.5
configcat-client==9.0.2
configcat-client==9.0.2
pytz==2024.1

0 comments on commit 59464da

Please sign in to comment.