Skip to content

Commit

Permalink
Cálculo de próximo directo con horario local
Browse files Browse the repository at this point in the history
  • Loading branch information
mouredev committed Feb 24, 2024
1 parent cc87da9 commit 2eda57d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions link_bio/link_bio/state/PageState.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
class PageState(rx.State):

live_status = Live(live=False, title="")
next_live: str = ""
next_live = ""
featured_info: list[Featured]

async def check_live(self):
async def check_live(self, timezone: str):
self.live_status = await live(USER)
if not self.live_status.live:
self.next_live = utils.next_date(await schedule())
self.next_live = utils.next_date(await schedule(), timezone)

async def featured_links(self):
self.featured_info = await featured()
25 changes: 18 additions & 7 deletions link_bio/link_bio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,27 @@ def lang() -> rx.Component:
# Date


def next_date(dates: dict) -> str:
def local_timezone() -> str:
return datetime.now().astimezone().tzname()

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

def next_date(dates: dict, timezone: str) -> str:

# Se intenta forzar el locale para traducir el formateo de fecha a español
try:
locale.setlocale(locale.LC_TIME, "es_ES")
except:
try:
locale.setlocale(locale.LC_TIME, "es_ES.utf8")
except:
pass

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

now = datetime.now()
current_time = now.astimezone().timetz()
tz = pytz.timezone(timezone)
now = datetime.now(tz)
current_time = now.timetz()

for weekday in range(7):

Expand All @@ -66,15 +77,15 @@ def next_date(dates: dict) -> str:
tzinfo=pytz.UTC).timetz()

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

if current_time < next_time or weekday > 0:

next_date = now + timedelta(days=weekday)

local_date = datetime(
next_date.year, next_date.month, next_date.day,
time_utc.hour, time_utc.minute, tzinfo=pytz.UTC).astimezone()
time_utc.hour, time_utc.minute, tzinfo=pytz.UTC).astimezone(tz)

return local_date.strftime("%A, %d de %B a las %H:%M").capitalize()

Expand Down
5 changes: 2 additions & 3 deletions link_bio/link_bio/views/header.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from turtle import position
import reflex as rx
import datetime
import link_bio.constants as const
from link_bio.model.Live import Live
import link_bio.utils as utils
from link_bio.styles.styles import Size, Spacing
from link_bio.styles.colors import Color, TextColor
from link_bio.components.link_icon import link_icon
Expand Down Expand Up @@ -151,7 +150,7 @@ def header(details=True) -> rx.Component:
width="100%",
spacing=Spacing.BIG.value,
align_items="start",
on_mount=PageState.check_live
on_mount=PageState.check_live(utils.local_timezone())
)


Expand Down

0 comments on commit 2eda57d

Please sign in to comment.