Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use aware objects when storing and reading UTC timestamps #4017

Merged
merged 8 commits into from Aug 12, 2020

Conversation

DevilXD
Copy link
Contributor

@DevilXD DevilXD commented Jun 25, 2020

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

.utcfromtimestamp() takes local timezone into account when converting a timestamp back into a datetime object: https://cdn.discordapp.com/attachments/160386989819035648/725707305320185876/unknown.png
Considering 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:

await self.config.member(user).banned_until.set(unban_time.timestamp())

There appears to be one other place where .utcfromtimestamp() is used - I modified that one accordingly too:

return datetime.datetime.utcfromtimestamp(time)

@DevilXD DevilXD requested a review from palmtree5 as a code owner June 25, 2020 13:53
Dav-Git
Dav-Git previously approved these changes Jun 25, 2020
@DevilXD DevilXD changed the title Fixed Mod cog tempban taking local timezone into account Fix utcfromtimestamp taking local timezone into account Jun 25, 2020
@Drapersniper Drapersniper added QA: Needed Category: Cogs - Mod This is related to the Mod cog. labels Jun 26, 2020
@Jackenmen Jackenmen self-assigned this Jun 30, 2020
@Jackenmen Jackenmen added this to the 3.3.10 milestone Jun 30, 2020
@Jackenmen
Copy link
Member

Hmm, the actual issue here is with how we create the timestamp - datetime.timestamp() assumes that naive datetime instances (such as the one generated by datetime.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 😐

@DevilXD
Copy link
Contributor Author

DevilXD commented Jun 30, 2020

datetime.timestamp() assumes that naive datetime instances (such as the one generated by datetime.utcnow()) represents local time (...)

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, and datetime.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.

@Jackenmen
Copy link
Member

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, and datetime.fromtimestamp() converts it back - also no matter the timezone.

Well, not really. .timestamp() assumes the time is local when the datetime object is naive. And datetime.utcnow() returns a naive datetime object that is not in local timezone.

@DevilXD
Copy link
Contributor Author

DevilXD commented Jul 4, 2020

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.

@DevilXD DevilXD marked this pull request as ready for review July 4, 2020 07:39
@DevilXD DevilXD requested a review from tekulvw as a code owner July 4, 2020 07:39
@Kowlin Kowlin modified the milestones: 3.3.10, 3.3.11 Jul 9, 2020
@Kowlin Kowlin self-assigned this Jul 29, 2020
@Jackenmen Jackenmen modified the milestones: 3.4.1, 3.4.0 Aug 6, 2020
Copy link
Member

@Jackenmen Jackenmen left a comment

Choose a reason for hiding this comment

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

A very late review, sorry for that.

Aside from review comments:

  • The message.created_at in redbot/cogs/filter/filter.py:L376 was missed
  • Per your comment, I checked Audio's usage of utcnow() and it shouldn't need any changes, so this PR doesn't need to touch it.

redbot/core/modlog.py Outdated Show resolved Hide resolved
redbot/core/modlog.py Outdated Show resolved Hide resolved
redbot/core/events.py Outdated Show resolved Hide resolved
@Jackenmen Jackenmen added QA: Changes Requested Used by few QA members. Awaiting changes requested by maintainers or QA. and removed QA: Needed labels Aug 10, 2020
Copy link
Member

@Jackenmen Jackenmen left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for contributing these fixes for the timezone madness 😄

@Jackenmen Jackenmen added QA: Passed Used by few QA members. Has been approved by the assigned QA member(s). and removed QA: Changes Requested Used by few QA members. Awaiting changes requested by maintainers or QA. labels Aug 12, 2020
@Jackenmen Jackenmen changed the title Fix utcfromtimestamp taking local timezone into account Use aware objects when storing and reading UTC timestamps Aug 12, 2020
@Jackenmen Jackenmen merged commit 6e63ed4 into Cog-Creators:V3/develop Aug 12, 2020
@Cog-CreatorsBot Cog-CreatorsBot added the Changelog Entry: Pending Changelog entry for this PR hasn't been added by repo maintainers yet. label Aug 12, 2020
@Jackenmen Jackenmen added Changelog Entry: Added Changelog entry for this PR has already been added to changelog PR. and removed Changelog Entry: Pending Changelog entry for this PR hasn't been added by repo maintainers yet. labels Aug 15, 2020
@Jackenmen Jackenmen added the Type: Bug Unexpected behavior, result, or exception. In case of PRs, it is a fix for the foregoing. label Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Cogs - Mod This is related to the Mod cog. Changelog Entry: Added Changelog entry for this PR has already been added to changelog PR. QA: Passed Used by few QA members. Has been approved by the assigned QA member(s). Type: Bug Unexpected behavior, result, or exception. In case of PRs, it is a fix for the foregoing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants