From 2eda57dd4578d797d63351e7ebbaa82e214c3f81 Mon Sep 17 00:00:00 2001 From: Brais Moure Date: Sat, 24 Feb 2024 10:35:01 +0100 Subject: [PATCH] =?UTF-8?q?C=C3=A1lculo=20de=20pr=C3=B3ximo=20directo=20co?= =?UTF-8?q?n=20horario=20local?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- link_bio/link_bio/state/PageState.py | 6 +++--- link_bio/link_bio/utils.py | 25 ++++++++++++++++++------- link_bio/link_bio/views/header.py | 5 ++--- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/link_bio/link_bio/state/PageState.py b/link_bio/link_bio/state/PageState.py index c70b1c9e..5d4cd4d5 100644 --- a/link_bio/link_bio/state/PageState.py +++ b/link_bio/link_bio/state/PageState.py @@ -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() diff --git a/link_bio/link_bio/utils.py b/link_bio/link_bio/utils.py index bb76ea02..dc28ca9e 100644 --- a/link_bio/link_bio/utils.py +++ b/link_bio/link_bio/utils.py @@ -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): @@ -66,7 +77,7 @@ 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: @@ -74,7 +85,7 @@ def next_date(dates: dict) -> str: 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() diff --git a/link_bio/link_bio/views/header.py b/link_bio/link_bio/views/header.py index a957d348..a5721221 100644 --- a/link_bio/link_bio/views/header.py +++ b/link_bio/link_bio/views/header.py @@ -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 @@ -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()) )