Skip to content

fix: replace deprecated datetime.utcnow() with datetime.now(UTC) throughout#659

Open
neuron-tech-ai wants to merge 2 commits into
jamiepine:mainfrom
neuron-tech-ai:fix/datetime-utc
Open

fix: replace deprecated datetime.utcnow() with datetime.now(UTC) throughout#659
neuron-tech-ai wants to merge 2 commits into
jamiepine:mainfrom
neuron-tech-ai:fix/datetime-utc

Conversation

@neuron-tech-ai
Copy link
Copy Markdown

@neuron-tech-ai neuron-tech-ai commented May 14, 2026

datetime.utcnow() is deprecated in Python 3.12 and raises DeprecationWarning. It also returns a naive datetime (no timezone info), which is a footgun when comparing with timezone-aware datetimes.

Replaces all usages across the codebase with datetime.now(UTC), which returns a timezone-aware UTC datetime and is the correct approach going forward. Pure mechanical replacement — no behavior change.

Summary by CodeRabbit

Release Notes

Bug Fixes

  • Updated all system timestamps to use timezone-aware UTC format, improving timestamp consistency and accuracy across profile management, story editing, task tracking, and data import operations.

Review Change Stack

Python 3.12 deprecates datetime.utcnow() with a DeprecationWarning and
it will be removed in a future release. Replace all call-site usages in
services, routes, and utils with datetime.now(UTC), and replace the
SQLAlchemy ORM column defaults (which used the bare function reference
datetime.utcnow) with lambda: datetime.now(UTC) so that the returned
objects are timezone-aware and consistent with Python best practice.

Affected: database/models.py, services/{stories,history,profiles,
channels,export_import}.py, routes/{profiles,tasks}.py, utils/tasks.py
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 14, 2026

Warning

Rate limit exceeded

@neuron-tech-ai has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 45 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eb8e5958-1375-46e4-b20f-d5bf0129b4e4

📥 Commits

Reviewing files that changed from the base of the PR and between 3101641 and 2133131.

📒 Files selected for processing (1)
  • backend/services/export_import.py
📝 Walkthrough

Walkthrough

This PR migrates all timestamp handling from naive UTC (datetime.utcnow()) to timezone-aware UTC (datetime.now(UTC)) across the backend. The changes span database schema definitions, service business logic, route endpoints, and task utilities to ensure consistent timezone-aware datetime usage throughout the application.

Changes

Timezone-aware UTC timestamp migration

Layer / File(s) Summary
Database models schema migration to timezone-aware timestamps
backend/database/models.py
All ORM model column defaults and onupdate handlers for created_at and updated_at fields are updated from datetime.utcnow to datetime.now(UTC) across 12 database entity models including VoiceProfile, Generation, Story, StoryItem, Project, GenerationVersion, EffectPreset, AudioChannel, CaptureSettings, GenerationSettings, MCPClientBinding, and Capture.
Service layer timestamp operations
backend/services/history.py, backend/services/stories.py, backend/services/profiles.py, backend/services/channels.py, backend/services/export_import.py
Service modules update timestamp assignments when creating and modifying records: generation creation in history, story/story-item creation and updates (create, move, remove, trim, volume, split, duplicate, reorder, version operations), profile creation and modifications (sample add, update, avatar upload/delete), channel creation, and generation import operations. All use timezone-aware datetime.now(UTC).
Route/API endpoint timestamp updates
backend/routes/profiles.py, backend/routes/tasks.py
Route handlers update timestamp assignments at the API level: profile updated_at on profile update endpoint and task started_at fallback in active tasks retrieval. Both switch to timezone-aware datetime.now(UTC).
Task utility class timestamp defaults
backend/utils/tasks.py
Task dataclass default_factory methods for DownloadTask.started_at and GenerationTask.started_at are updated to create timezone-aware timestamps using datetime.now(UTC) instead of datetime.utcnow().

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Hop into timezone awareness,
From naive UTC we ascend,
datetime.now(UTC) brings fairness,
Time zones aware from start to end!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: replace deprecated datetime.utcnow() with datetime.now(UTC) throughout' accurately summarizes the main change: systematically replacing deprecated datetime.utcnow() calls with timezone-aware datetime.now(UTC) across multiple modules.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/services/export_import.py`:
- Line 435: The call datetime.now(UTC) will raise NameError because UTC isn't
defined; import timezone from the datetime module and use it explicitly (i.e.,
replace or update the usage of datetime.now(UTC) to datetime.now(timezone.utc)
and add a matching import such as "from datetime import timezone") so the
timezone is defined; locate the occurrence of datetime.now(UTC) in
export_import.py and add the import near the existing datetime import.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 88385247-23e6-4a7e-aa56-acc6b3d708cc

📥 Commits

Reviewing files that changed from the base of the PR and between b35b909 and 3101641.

📒 Files selected for processing (9)
  • backend/database/models.py
  • backend/routes/profiles.py
  • backend/routes/tasks.py
  • backend/services/channels.py
  • backend/services/export_import.py
  • backend/services/history.py
  • backend/services/profiles.py
  • backend/services/stories.py
  • backend/utils/tasks.py

Comment thread backend/services/export_import.py
The function-local import was missing UTC, causing NameError at runtime
when datetime.now(UTC) was called.
@neuron-tech-ai
Copy link
Copy Markdown
Author

Addressed the CodeRabbit review comment:

Missing UTC import causing NameError: The function-scope import at the top of import_generation_from_zip was from datetime import datetime. Changed to from datetime import UTC, datetime so datetime.now(UTC) on line 435 resolves correctly at runtime.

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.

1 participant