Proposal to Replace datetime.datetime.utcnow() Due to Potential Deprecation and Improve Timezone Handling #2139

Closed
opened 2026-02-21 17:42:08 -05:00 by yindo · 0 comments
Owner

Originally created by @pamaforce on GitHub (Apr 12, 2024).

Self Checks

  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • Pleas do not modify this template :) and fill in all the required fields.

1. Is this request related to a challenge you're experiencing?

I am currently using datetime.datetime.utcnow() in multiple parts of our project to obtain the current UTC time. However, this method returns a naive datetime object without any timezone information, which can lead to potential errors when dealing with timezone conversions or comparing times. The Python community is also moving towards deprecating naive datetime objects in favor of timezone-aware datetime objects.
image

2. Describe the feature you'd like to see

I would like to replace datetime.datetime.utcnow() with datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) throughout our codebase. This approach uses a timezone-aware datetime object initially and then removes the timezone info, making it a safe and clear alternative that behaves consistently with the original method.
image

3. How will this feature improve your workflow or experience?

Adopting this change would enhance the reliability and clarity of our time handling in the application, reducing the risk of bugs related to naive datetime objects. It will make our codebase more modern and compliant with upcoming Python standards, thus future-proofing our work.

4. Additional context or comments

The transition to a more explicit handling of time zones is supported by the broader Python community and aligns with best practices in software development. Here's a link to the Python documentation that discusses the benefits of aware datetime objects: Python Docs on datetime

5. Can you help us with this feature?

  • I am interested in contributing to this feature.
Originally created by @pamaforce on GitHub (Apr 12, 2024). ### Self Checks - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] Pleas do not modify this template :) and fill in all the required fields. ### 1. Is this request related to a challenge you're experiencing? I am currently using `datetime.datetime.utcnow()` in multiple parts of our project to obtain the current UTC time. However, this method returns a naive datetime object without any timezone information, which can lead to potential errors when dealing with timezone conversions or comparing times. The Python community is also moving towards deprecating naive datetime objects in favor of timezone-aware datetime objects. <img width="550" alt="image" src="https://github.com/langgenius/dify/assets/72378846/4a220a71-ffa1-401e-95dc-1eeefb12110f"> ### 2. Describe the feature you'd like to see I would like to replace `datetime.datetime.utcnow()` with `datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)` throughout our codebase. This approach uses a timezone-aware datetime object initially and then removes the timezone info, making it a safe and clear alternative that behaves consistently with the original method. ![image](https://github.com/langgenius/dify/assets/72378846/851ade9e-bfdd-4e20-8520-b76fce8da281) ### 3. How will this feature improve your workflow or experience? Adopting this change would enhance the reliability and clarity of our time handling in the application, reducing the risk of bugs related to naive datetime objects. It will make our codebase more modern and compliant with upcoming Python standards, thus future-proofing our work. ### 4. Additional context or comments The transition to a more explicit handling of time zones is supported by the broader Python community and aligns with best practices in software development. Here's a link to the Python documentation that discusses the benefits of aware datetime objects: [Python Docs on datetime](https://docs.python.org/3/library/datetime.html) ### 5. Can you help us with this feature? - [X] I am interested in contributing to this feature.
yindo added the 💪 enhancement label 2026-02-21 17:42:08 -05:00
yindo closed this issue 2026-02-21 17:42:08 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#2139