From f2c8e1dfa810b087f2eaf55f87c5a3268a29e187 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Fri, 9 Jun 2023 21:28:18 +0100 Subject: [PATCH] Fixing timezone issues --- elia_chat/database/converters.py | 2 +- elia_chat/models.py | 2 +- elia_chat/time_display.py | 18 ++++++++++++++++-- elia_chat/widgets/chat_list.py | 5 ++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/elia_chat/database/converters.py b/elia_chat/database/converters.py index 57258da..2cd66b8 100644 --- a/elia_chat/database/converters.py +++ b/elia_chat/database/converters.py @@ -16,7 +16,7 @@ def chat_message_to_message_dao(chat_message: ChatMessage) -> MessageDao: return MessageDao( role=chat_message["role"], content=chat_message["content"], - timestamp=datetime.utcfromtimestamp(chat_message["timestamp"]), + timestamp=datetime.fromtimestamp(chat_message["timestamp"]), status=chat_message["status"], end_turn=chat_message["end_turn"], weight=chat_message["weight"], diff --git a/elia_chat/models.py b/elia_chat/models.py index f2812d4..f3d2c5b 100644 --- a/elia_chat/models.py +++ b/elia_chat/models.py @@ -42,7 +42,7 @@ def non_system_messages(self) -> list[ChatMessage]: @property def create_time(self) -> datetime: - return datetime.fromtimestamp(self.create_timestamp or 0) + return datetime.fromtimestamp(self.create_timestamp or 0).astimezone() @property def update_time(self) -> datetime: diff --git a/elia_chat/time_display.py b/elia_chat/time_display.py index f4efed2..bc2e529 100644 --- a/elia_chat/time_display.py +++ b/elia_chat/time_display.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timezone def format_timestamp(timestamp: float) -> str: @@ -10,4 +10,18 @@ def format_timestamp(timestamp: float) -> str: Returns: The string timestamp in the format "%Y-%m-%d %H:%M:%S". """ - return datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S") + utc_dt = datetime.fromtimestamp(timestamp, timezone.utc) + local_dt = utc_dt.astimezone() + return local_dt.strftime("%Y-%m-%d %H:%M:%S") + + +def convert_to_local(utc_dt: datetime) -> datetime: + """Given a UTC datetime, return a datetime in the local timezone.""" + local_dt_now = datetime.now() + local_tz = local_dt_now.astimezone().tzinfo + local_dt = utc_dt.astimezone(local_tz) + return local_dt + + +def get_local_timezone(): + return datetime.now(timezone.utc).astimezone().tzinfo diff --git a/elia_chat/widgets/chat_list.py b/elia_chat/widgets/chat_list.py index 697269d..ecf7c01 100644 --- a/elia_chat/widgets/chat_list.py +++ b/elia_chat/widgets/chat_list.py @@ -1,5 +1,6 @@ from __future__ import annotations +import datetime from dataclasses import dataclass import humanize @@ -27,7 +28,9 @@ class ChatListItemRenderable: def __rich_console__( self, console: Console, options: ConsoleOptions ) -> RenderResult: - create_time_string = humanize.naturaltime(self.chat.create_time) + utc_dt = datetime.datetime.utcnow() + local_dt = utc_dt.astimezone() + create_time_string = humanize.naturaltime(self.chat.create_time, when=local_dt) subtitle = f"{create_time_string}" yield Padding( Text.assemble(