Skip to content

Fix deprecated datetime.utcnow() in test fixture#307

Merged
wolph merged 4 commits intodevelopfrom
copilot/fix-deprecation-warning-date-parsing
Apr 18, 2026
Merged

Fix deprecated datetime.utcnow() in test fixture#307
wolph merged 4 commits intodevelopfrom
copilot/fix-deprecation-warning-date-parsing

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 17, 2026

Python 3.12+ deprecated datetime.utcnow(), and Python 3.13 surfaces additional datetime-related DeprecationWarnings around locale-sensitive date parsing. The test fixture in conftest.py was using the deprecated API to compute the local UTC offset for freezegun.

Changes

  • tests/conftest.py: Replace datetime.utcnow() with datetime.now(timezone.utc).astimezone().utcoffset() to compute the local timezone offset
# Before
offset_seconds = (datetime.now() - datetime.utcnow()).seconds + 10
offset_hours = int(offset_seconds / 3600)

# After
local_offset = datetime.now(timezone.utc).astimezone().utcoffset()
offset_hours = local_offset.total_seconds() / 3600

The new approach also fixes two latent bugs in the old calculation:

  • .seconds on a negative timedelta returns the wrong value for UTC− timezones (e.g. UTC−5 would yield 68400 instead of −18000)
  • Passing a float directly to freezegun's tz_offset correctly handles fractional-hour offsets (e.g. UTC+5:30), removing the need for the + 10 buffer hack

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • docs.python.org
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Fix deprecation warning for ambiguous date parsing Fix deprecated datetime.utcnow() in test fixture Apr 17, 2026
Copilot AI requested a review from wolph April 17, 2026 13:55
@wolph wolph marked this pull request as ready for review April 18, 2026 01:48
Copilot AI review requested due to automatic review settings April 18, 2026 01:48
@wolph
Copy link
Copy Markdown
Owner

wolph commented Apr 18, 2026

/gemini review

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the test-time timezone offset calculation used by freezegun to avoid deprecated datetime.utcnow() usage on newer Python versions.

Changes:

  • Replace datetime.utcnow()-based offset computation with datetime.now(timezone.utc).astimezone().utcoffset().
  • Preserve correct handling for negative and fractional-hour timezone offsets by using timedelta.total_seconds().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/conftest.py Outdated
offset_hours = int(offset_seconds / 3600)
# Compute the local UTC offset so freezegun uses the same timezone as
# the local system. Using datetime.now(timezone.utc).astimezone() avoids
# the deprecated datetime.utcnow() which was removed in Python 3.12+.
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

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

The comment says datetime.utcnow() “was removed in Python 3.12+”, but it was deprecated in 3.12 (and still exists). Suggest rewording to avoid misinformation (e.g., “deprecated since Python 3.12; use timezone-aware datetimes instead”).

Suggested change
# the deprecated datetime.utcnow() which was removed in Python 3.12+.
# the deprecated datetime.utcnow() (deprecated since Python 3.12) and
# uses a timezone-aware datetime instead.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the sleep_faster fixture in tests/conftest.py to replace the deprecated datetime.utcnow() with a Python 3.12-compatible approach for calculating local UTC offsets. Feedback suggests simplifying the logic by passing the timedelta object directly to freezegun.freeze_time rather than manually converting it to hours.

Comment thread tests/conftest.py
Comment on lines +43 to 46
local_offset = datetime.now(timezone.utc).astimezone().utcoffset()
offset_hours = local_offset.total_seconds() / 3600

freeze_time = freezegun.freeze_time(tz_offset=offset_hours)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The freezegun.freeze_time function accepts a timedelta object directly for the tz_offset parameter. You can simplify this logic by passing local_offset directly, which avoids the manual conversion to hours and handles fractional offsets more cleanly without precision loss.

Suggested change
local_offset = datetime.now(timezone.utc).astimezone().utcoffset()
offset_hours = local_offset.total_seconds() / 3600
freeze_time = freezegun.freeze_time(tz_offset=offset_hours)
local_offset = datetime.now(timezone.utc).astimezone().utcoffset()
freeze_time = freezegun.freeze_time(tz_offset=local_offset)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot what do you think of this change?

@wolph
Copy link
Copy Markdown
Owner

wolph commented Apr 18, 2026

@copilot Did you benchmark the performance before/after? Are there any other places in the code that use datetime.utcnow()?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 18, 2026

@copilot Did you benchmark the performance before/after? Are there any other places in the code that use datetime.utcnow()?

No benchmarking needed here — the offset is computed once per test fixture setup (not in a hot path), so there's no meaningful performance difference.

I also searched the entire codebase for utcnow, utcfromtimestamp, and utctimetuple — the only occurrence was the one in conftest.py that's already been fixed (commit 0c153cf also corrects the comment wording, changing "removed" to "deprecated" since utcnow() was deprecated in 3.12, not removed).

@wolph wolph merged commit b38f7f5 into develop Apr 18, 2026
11 checks passed
@wolph wolph deleted the copilot/fix-deprecation-warning-date-parsing branch April 18, 2026 02:37
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.

DeprecationWarning: Parsing dates involving a day of month without a year specified is ambiguious

3 participants