Skip to content

fix(data): use UTC for timestamps in SQLAlchemy data layer#2923

Open
hobostay wants to merge 1 commit into
Chainlit:mainfrom
hobostay:fix/sql-timestamp-timezone
Open

fix(data): use UTC for timestamps in SQLAlchemy data layer#2923
hobostay wants to merge 1 commit into
Chainlit:mainfrom
hobostay:fix/sql-timestamp-timezone

Conversation

@hobostay
Copy link
Copy Markdown

@hobostay hobostay commented May 10, 2026

Summary

  • Fix get_current_timestamp() in SQLAlchemyDataLayer to use datetime.now(timezone.utc) instead of datetime.now().

Why

The method get_current_timestamp() used datetime.now() which returns local time, but then appended "Z" (the ISO 8601 UTC designator). This resulted in timestamps that claimed to be UTC but were actually in the server's local timezone.

For example, on a server running in UTC+8 (China Standard Time), a timestamp at 2pm local would be stored as 2026-05-10T14:00:00.000000Z, implying 2pm UTC when it should be 6am UTC.

This bug affects:

  • User creation timestamps (createdAt)
  • Thread creation timestamps

Related issue: #2491 (time handling mismatch causes exceptions)

Test plan

  • Verify existing tests pass: uv run pytest tests/ from backend/
  • Verify that timestamps stored in the database are correctly in UTC
  • Test on a server running in a non-UTC timezone to confirm the fix

🤖 Generated with Claude Code


Summary by cubic

Fix timestamp timezone handling in the SQLAlchemy data layer by generating UTC-aware ISO strings in get_current_timestamp(). Ensures createdAt and thread timestamps are true UTC instead of local time labeled as Z, addressing #2491.

Written for commit 07bbb40. Summary will update on new commits.

`get_current_timestamp()` used `datetime.now()` which returns local
time, but appended "Z" (the UTC timezone designator). This produced
incorrect timestamps when the server was not running in UTC.

Use `datetime.now(timezone.utc)` to ensure the timestamp is actually
in UTC before appending "Z".

Related: Chainlit#2491

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working data layer Pertains to data layers. labels May 10, 2026
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/chainlit/data/sql_alchemy.py">

<violation number="1" location="backend/chainlit/data/sql_alchemy.py:106">
P1: UTC-aware isoformat() output incorrectly appends 'Z', producing invalid timestamps with both '+00:00' and 'Z'</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.


async def get_current_timestamp(self) -> str:
return datetime.now().isoformat() + "Z"
return datetime.now(timezone.utc).isoformat() + "Z"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: UTC-aware isoformat() output incorrectly appends 'Z', producing invalid timestamps with both '+00:00' and 'Z'

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/chainlit/data/sql_alchemy.py, line 106:

<comment>UTC-aware isoformat() output incorrectly appends 'Z', producing invalid timestamps with both '+00:00' and 'Z'</comment>

<file context>
@@ -103,7 +103,7 @@ async def execute_sql(
 
     async def get_current_timestamp(self) -> str:
-        return datetime.now().isoformat() + "Z"
+        return datetime.now(timezone.utc).isoformat() + "Z"
 
     def clean_result(self, obj):
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working data layer Pertains to data layers. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant