Skip to content

Commit

Permalink
Fixing timezone issues
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Jun 9, 2023
1 parent 7b8cbe0 commit f2c8e1d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion elia_chat/database/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion elia_chat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 16 additions & 2 deletions elia_chat/time_display.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone


def format_timestamp(timestamp: float) -> str:
Expand All @@ -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
5 changes: 4 additions & 1 deletion elia_chat/widgets/chat_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import datetime
from dataclasses import dataclass

import humanize
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit f2c8e1d

Please sign in to comment.