Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 19, 2025

The timestamp_to_datetime() function in pyrogram/utils.py was incorrectly converting Unix timestamps to datetime objects, causing Message.date to have wrong timezone information. This led to logical errors when users compared message timestamps with current time.

Problem

The issue occurred when users in non-UTC timezones performed time comparisons:

last_message_time = message.date  # Returns wrong timezone
datetime_now = datetime.now(last_message_time.tzinfo)
inactive_threshold = (datetime_now - last_message_time).total_seconds()
# Result: Messages appeared to be from the future!

For users in Europe/Kiev timezone (UTC+3), a message timestamp that should represent "22:00:12 UTC" was incorrectly converted to "01:00:12 UTC", making messages appear 3 hours newer than they actually were.

Root Cause

The timestamp_to_datetime() function used:

return datetime.fromtimestamp(ts).replace(tzinfo=timezone.utc)

This approach:

  1. Creates a naive datetime in the user's local timezone
  2. Then incorrectly labels it as UTC with .replace()
  3. Results in an offset error equal to the timezone difference

Solution

Changed to the correct implementation:

return datetime.fromtimestamp(ts, tz=timezone.utc)

This properly interprets Unix timestamps as UTC time, ensuring consistent datetime objects regardless of the user's system timezone.

Validation

  • ✅ All message dates now consistently show UTC time across different system timezones
  • ✅ Time comparison logic works correctly (messages appear from the past, not future)
  • ✅ Edge cases handled properly (epoch, large timestamps, None input)
  • ✅ No breaking changes to existing API

Fixes #192.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@SpEcHiDe 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Co-authored-by: SpEcHiDe <6317196+SpEcHiDe@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Message.date contains incorrectly formatted datetime with wrong tzinfo, causing logical errors in time comparison Fix Message.date timezone conversion causing logical errors in time comparison Aug 19, 2025
@Copilot Copilot AI requested a review from SpEcHiDe August 19, 2025 11:28
Copilot finished work on behalf of SpEcHiDe August 19, 2025 11:28
@SpEcHiDe SpEcHiDe marked this pull request as ready for review August 19, 2025 11:42
@SpEcHiDe SpEcHiDe merged commit fc838cf into dev Aug 19, 2025
@SpEcHiDe SpEcHiDe deleted the copilot/fix-192 branch August 19, 2025 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Message.date contains incorrectly formatted datetime with wrong tzinfo, causing logical errors in time comparison
2 participants