Closed ShreyTiwari closed 1 month ago
Hi there! 👋
I noticed that the codebase uses datetime.utcnow() or datetime.utcfromtimestamp(). These are deprecated and won't work with Python 3.12. They also handle naïve datetimes, which can lead to bugs. Could we switch to timezone-aware alternatives?
datetime.utcnow()
datetime.utcfromtimestamp()
Here are the specific instances CodeQL flagged:
Issue:
Example Problem:
from datetime import datetime ts = 1571595618.0 x = datetime.utcfromtimestamp(ts) x_ts = x.timestamp() assert ts == x_ts, f"{ts} != {x_ts}" # Can fail in non-UTC locales
Recommended Solution: Switch to time zone-aware methods:
from datetime import datetime, timezone # Replace utcnow() dt_now = datetime.now(tz=timezone.utc) # Replace utcfromtimestamp() ts = 1571595618.0 x = datetime.fromtimestamp(ts, tz=timezone.utc) x_ts = x.timestamp() assert ts == x_ts, f"{ts} != {x_ts}" # This succeeds
datetime.now(tz=timezone.utc)
datetime.fromtimestamp(ts, tz=timezone.utc)
For more details, see:
Thank you so much for your time and effort in maintaining this project! 🌟
Best, Shrey
Thank you for your input, we very much appreciate it. We will be looking at this for a future roadmap item.
Hi there! 👋
I noticed that the codebase uses
datetime.utcnow()
ordatetime.utcfromtimestamp()
. These are deprecated and won't work with Python 3.12. They also handle naïve datetimes, which can lead to bugs. Could we switch to timezone-aware alternatives?CodeQL Alerts
Here are the specific instances CodeQL flagged:
Explanation
Issue:
datetime.utcnow()
anddatetime.utcfromtimestamp()
return naïve datetimes (without timezone info).Example Problem:
Recommended Solution: Switch to time zone-aware methods:
Action Required:
datetime.utcnow()
withdatetime.now(tz=timezone.utc)
.datetime.utcfromtimestamp()
withdatetime.fromtimestamp(ts, tz=timezone.utc)
.References:
For more details, see:
Thank you so much for your time and effort in maintaining this project! 🌟
Best, Shrey