Use aware objects when storing and reading UTC timestamps #4017
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!4017
Loading…
Reference in a new issue
No description provided.
Delete branch "patch-1"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
Description of the changes
.utcfromtimestamp()takes local timezone into account when converting a timestamp back into adatetimeobject: https://cdn.discordapp.com/attachments/160386989819035648/725707305320185876/unknown.pngConsidering that the timestamp that ends up being stored is in UTC, this is probably not what was intended.
Ref of where the value is stored:
github.com/Cog-Creators/Red-DiscordBot@632840384b/redbot/cogs/mod/kickban.py (L445)There appears to be one other place where
.utcfromtimestamp()is used - I modified that one accordingly too:github.com/Cog-Creators/Red-DiscordBot@ad979180e5/redbot/core/bank.py (L136)Hmm, the actual issue here is with how we create the timestamp -
datetime.timestamp()assumes that naivedatetimeinstances (such as the one generated bydatetime.utcnow()) represents local time and therefore generates incorrect POSIX timestamp that is relative to local timezone.I don't think we can really solve that though, when we already have existing data, so I guess this might be needed middle ground 😐
This is not really the case though?
datetime.utcnow()always generates a UTC timestamp object, no matter the timezone. The.timestamp()method converts that to a POSIX timestamp, anddatetime.fromtimestamp()converts it back - also no matter the timezone. You should always operate on UTC timestamps internally, only optionally using timezone conversions when displaying the information to the user - otherwise you're exposing yourself to lots of potential bugs, such as the stored time intervals changing when you change the system timezone, among many others.My recommendation would be to stick to UTC stamps, and as explained, only deal with timezones when it's time (duh) to display it to the user.
Well, not really.
.timestamp()assumes the time is local when the datetime object is naive. Anddatetime.utcnow()returns a naive datetime object that is not in local timezone.This is technically ready to go. Aware datetime objects are now used in places where they are (or can end up) being stored in the config, along with UTC conversion back.
One thing I'd like to point out tho - the audio cog has lots of
datetime.utcnow()usage, and probably lots of timestamp conversions too. Because the internals of that cog appear to be too complicated for me, I haven't touched anything there. I'm not sure if its in the scope of this PR, but if someone knowledgeable about that would / could point me to the right places, I'll add the changes as needed.A very late review, sorry for that.
Aside from review comments:
message.created_atinredbot/cogs/filter/filter.py:L376was missedutcnow()and it shouldn't need any changes, so this PR doesn't need to touch it.@ -55,7 +54,7 @@ def init_events(bot, cli_flags):if bot._uptime is not None:Bot's uptime from this var is exposed in
bot.uptimeproperty, so this change could break someone (+ it does break[p]uptimecommand). I don't think the change here is really needed.@ -351,9 +353,9 @@ class Case:until = NoneWould it be possible to include " UTC" suffix or something like that so that it's clear to users in what timezone this is in?
@ -374,7 +376,7 @@ class Case:last_modified = NoneWould it be possible to include " UTC" suffix or something like that so that it's clear to users in what timezone this is in?
Looks good to me, thanks for contributing these fixes for the timezone madness 😄