mirror of https://github.com/ansible/ansible.git
remove deprecated datetime.datetime methods (#81323)
* Remove datetime.datetime.utcfromtimestamp and datetime.datetime.uctnow from controller code since they are deprecated in Python 3.12. * Update target side code to use new utcfromtimestamp and utcnow utils in ansible.module_utils.compat.datetime that return aware datetime objects on Python 2.7 and 3. Co-authored-by: Matt Clay <matt@mystile.com>pull/81351/head
parent
923ede4f72
commit
4c41562270
@ -0,0 +1,4 @@
|
||||
bugfixes:
|
||||
- On Python 3 use datetime methods ``fromtimestamp`` and ``now`` with UTC timezone instead of ``utcfromtimestamp`` and ``utcnow``, which are deprecated in Python 3.12.
|
||||
minor_changes:
|
||||
- Add ``utcfromtimestamp`` and ``utcnow`` to ``ansible.module_utils.compat.datetime`` to return fixed offset datetime objects.
|
@ -0,0 +1,40 @@
|
||||
# Copyright (c) 2023 Ansible
|
||||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.module_utils.six import PY3
|
||||
|
||||
import datetime
|
||||
|
||||
|
||||
if PY3:
|
||||
UTC = datetime.timezone.utc
|
||||
else:
|
||||
_ZERO = datetime.timedelta(0)
|
||||
|
||||
class _UTC(datetime.tzinfo):
|
||||
__slots__ = ()
|
||||
|
||||
def utcoffset(self, dt):
|
||||
return _ZERO
|
||||
|
||||
def dst(self, dt):
|
||||
return _ZERO
|
||||
|
||||
def tzname(self, dt):
|
||||
return "UTC"
|
||||
|
||||
UTC = _UTC()
|
||||
|
||||
|
||||
def utcfromtimestamp(timestamp): # type: (float) -> datetime.datetime
|
||||
"""Construct an aware UTC datetime from a POSIX timestamp."""
|
||||
return datetime.datetime.fromtimestamp(timestamp, UTC)
|
||||
|
||||
|
||||
def utcnow(): # type: () -> datetime.datetime
|
||||
"""Construct an aware UTC datetime from time.time()."""
|
||||
return datetime.datetime.now(UTC)
|
Loading…
Reference in New Issue