GoogleCloudPlatform / cortex-data-foundation

Data Foundation - Google Cloud Cortex Framework
https://cloud.google.com/solutions/cortex
Apache License 2.0
162 stars 88 forks source link

Use of Deprecated `utcnow()` or `utcfromtimestamp()` APIs #78

Closed ShreyTiwari closed 1 month ago

ShreyTiwari commented 2 months 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?

CodeQL Alerts

Here are the specific instances CodeQL flagged:

  1. Location: https://github.com/GoogleCloudPlatform/cortex-data-foundation/blob/33d4c978101ecd90d83f45aab3f6f9a19a916288/src/k9/src/catgap/catgap_pipeline/pipeline_job/matches_writer.py#L97

Explanation

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

Action Required:

References:

For more details, see:

Thank you so much for your time and effort in maintaining this project! 🌟

Best, Shrey

TXZebra commented 1 month ago

Thank you for your input, we very much appreciate it. We will be looking at this for a future roadmap item.