Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated datetime.datetime.utcnow #2414

Merged
merged 9 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions win32/Lib/win32timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ def local(class_):
registry.
>>> localTZ = TimeZoneInfo.local()
>>> now_local = datetime.datetime.now(localTZ)
>>> now_UTC = datetime.datetime.utcnow()
>>> now_UTC = datetime.datetime.utcnow() # deprecated
>>> (now_UTC - now_local) < datetime.timedelta(seconds = 5)
Traceback (most recent call last):
...
Expand All @@ -769,6 +769,11 @@ def local(class_):
Now one can compare the results of the two offset aware values
>>> (now_UTC - now_local) < datetime.timedelta(seconds = 5)
True

Or use the newer `datetime.timezone.utc`
>>> now_UTC = datetime.datetime.now(datetime.timezone.utc)
>>> (now_UTC - now_local) < datetime.timedelta(seconds = 5)
True
"""
code, info = TimeZoneDefinition.current()
# code is 0 if daylight savings is disabled or not defined
Expand Down Expand Up @@ -895,22 +900,30 @@ def _enumerate_reg(key, func):
pass


def utcnow():
def utcnow() -> datetime.datetime:
"""
Return the UTC time now with timezone awareness as enabled
by this module
>>> now = utcnow()

>>> (now - datetime.datetime.now(datetime.timezone.utc)) < datetime.timedelta(seconds = 5)
True
>>> type(now.tzinfo) is TimeZoneInfo
True
Comment on lines +908 to +912
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if these are quite necessary

"""
now = datetime.datetime.utcnow()
now = now.replace(tzinfo=TimeZoneInfo.utc())
return now
return datetime.datetime.now(TimeZoneInfo.utc())


def now():
def now() -> datetime.datetime:
"""
Return the local time now with timezone awareness as enabled
by this module
>>> now_local = now()

>>> (now_local - datetime.datetime.now(datetime.timezone.utc)) < datetime.timedelta(seconds = 5)
True
>>> type(now_local.tzinfo) is TimeZoneInfo
True
"""
return datetime.datetime.now(TimeZoneInfo.local())

Expand Down