Skip to content

datetime.utcnow and datetime.utcfromtimestamp are deprecated in Python 3.12 #32344

Description

@tirkarthi

Apache Airflow version

2.6.2

What happened

The usage of datetime.utcnow is deprecated in Python 3.12 and it can be replaced with airflow.utils.timezone.utcnow and a similar method can be introduced for utcfromtimestamp too in timezone module

datetime.datetime’s utcnow() and utcfromtimestamp() are deprecated and will be removed in a future version. Instead, use timezone-aware objects to represent datetimes in UTC: respectively, call now() and fromtimestamp() with the tz parameter set to datetime.UTC. (Contributed by Paul Ganssle in gh-103857.)

What you think should happen instead

No response

How to reproduce

./python                     
Python 3.13.0a0 (heads/main:58906213cc, Jul  3 2023, 21:54:53) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.utcnow()
<stdin>:1: DeprecationWarning: datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
datetime.datetime(2023, 7, 4, 12, 41, 45, 658447)
>>> datetime.datetime.utcfromtimestamp(10000)
<stdin>:1: DeprecationWarning: datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.fromtimestamp(timestamp, datetime.UTC).
datetime.datetime(1970, 1, 1, 2, 46, 40)

Operating System

Ubuntu

Versions of Apache Airflow Providers

No response

Deployment

Virtualenv installation

Deployment details

No response

Anything else

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

Labels

area:corekind:metaHigh-level information important to the community

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions