fix: resolve unhandled UTC timezone offset for timestamps in conversation records#5580
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在解决后端处理日期时间对象时存在的时区偏移问题,该问题导致前端界面显示的时间与实际不符。通过在数据序列化和时间戳转换的关键环节强制使用 UTC 时区信息,确保了日期时间数据在整个系统中的一致性和准确性,从而为用户提供了正确的创建和更新时间显示。 Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Hey - 我在这里给了一些总体反馈:
- 在多个路由中重复出现的
obj.replace(tzinfo=timezone.utc).isoformat()逻辑,说明可以考虑引入一个小的共享 helper(例如to_utc_iso(dt)),把“naive→UTC”的转换集中到一个地方,确保行为一致。 - 在
conversation_mgr._convert_conv_from_v2_to_v1和AstrBotJSONProvider中,建议将from datetime import timezone(以及datetime)的导入移到模块级,而不是在函数/类内部导入,以保持导入方式一致并避免重复工作。
给 AI Agent 的提示词
Please address the comments from this code review:
## Overall Comments
- The repeated `obj.replace(tzinfo=timezone.utc).isoformat()` logic across multiple routes suggests introducing a small shared helper (e.g., `to_utc_iso(dt)`) to centralize the naive→UTC conversion and keep behavior consistent in one place.
- In `conversation_mgr._convert_conv_from_v2_to_v1` and `AstrBotJSONProvider`, consider moving `from datetime import timezone` (and `datetime`) imports to the module level instead of doing them inside functions/classes to keep imports consistent and avoid repeated work.帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据这些反馈来改进以后的代码审查。
Original comment in English
Hey - I've left some high level feedback:
- The repeated
obj.replace(tzinfo=timezone.utc).isoformat()logic across multiple routes suggests introducing a small shared helper (e.g.,to_utc_iso(dt)) to centralize the naive→UTC conversion and keep behavior consistent in one place. - In
conversation_mgr._convert_conv_from_v2_to_v1andAstrBotJSONProvider, consider movingfrom datetime import timezone(anddatetime) imports to the module level instead of doing them inside functions/classes to keep imports consistent and avoid repeated work.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The repeated `obj.replace(tzinfo=timezone.utc).isoformat()` logic across multiple routes suggests introducing a small shared helper (e.g., `to_utc_iso(dt)`) to centralize the naive→UTC conversion and keep behavior consistent in one place.
- In `conversation_mgr._convert_conv_from_v2_to_v1` and `AstrBotJSONProvider`, consider moving `from datetime import timezone` (and `datetime`) imports to the module level instead of doing them inside functions/classes to keep imports consistent and avoid repeated work.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This PR effectively addresses and resolves the timestamp offset issue caused by the loss of timezone information when reading naive datetimes from SQLite. The approach of explicitly handling UTC timezone is correct. However, the implementation contains several critical issues, such as missing module imports leading to NameError, and opportunities for improving code style and maintainability. Please refer to the specific review comments for details.
|
@sourcery-ai review |
There was a problem hiding this comment.
Hey - 我已经审查了你的更改,看起来非常不错!
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进之后的审查。
Original comment in English
Hey - I've reviewed your changes and they look great!
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Generated docs update PR (pending manual review): AI change summary:
Experimental bot notice:
|
…tion records
在windows桌面端-更多功能-对话数据板块内,创建时间与更新时间显示与实际时间不符,经排查发现是SQLite 数据库不支持存储带时区的日期,取出的datetime对象是丢失了 UTC 标识的 naive datetime。此时在后端直接对其进行 JSON 序列化(转为字符串)或调用
.timestamp()/.astimezone()时,Python 解释器会将其视作操作系统的“本地时区”强行发生偏移,导致正确原本存入的 UTC 时间被二次破坏。Modifications / 改动点
修复了由于后端输出 Naive Datetime 导致前端页面时间解析发生时差偏移的问题,仅涉及少量序列化拦截与格式替换:
astrbot/dashboard/routes/chatui_project.py
astrbot/dashboard/routes/live_chat.py
astrbot/dashboard/routes/open_api.py
(以上 4 个
文件):移除多余的原生 astimezone() 调用,全部统一为 replace(tzinfo=timezone.utc).isoformat() 输出绝对 UTC 字符串,交由无感的前端浏览器接管并呈现安全的本地时区渲染。
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.由 Sourcery 提供的摘要
确保所有仪表盘和会话相关的时间戳都作为带有 UTC 信息的时间值进行处理和序列化,以避免时区偏移问题。
错误修复:
created_at和updated_at时间戳不正确的问题,该问题由“天真(naive)”的 datetime 被解释为本地时区导致。改进内容:
Original summary in English
Summary by Sourcery
Ensure all dashboard and conversation timestamps are handled and serialized as UTC-aware values to avoid timezone offset issues.
Bug Fixes:
Enhancements:
Bug 修复:
created_at和updated_at时间显示不正确的问题。该问题是由于“naive” datetime 被解释为本地时区时间而非 UTC 所导致。增强内容:
tzinfo缺失时,将 datetime 对象序列化为带有显式 UTC 偏移量的 ISO 8601 字符串。Original summary in English
由 Sourcery 提供的摘要
确保所有仪表盘和会话相关的时间戳都作为带有 UTC 信息的时间值进行处理和序列化,以避免时区偏移问题。
错误修复:
created_at和updated_at时间戳不正确的问题,该问题由“天真(naive)”的 datetime 被解释为本地时区导致。改进内容:
Original summary in English
Summary by Sourcery
Ensure all dashboard and conversation timestamps are handled and serialized as UTC-aware values to avoid timezone offset issues.
Bug Fixes:
Enhancements: