[PR #8222/524d5883 backport][stable-8] Avoid deprecated utcnow() (#8243)

Avoid deprecated utcnow() (#8222)

Avoid deprecated utcnow().

(cherry picked from commit 524d5883b8)

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
patchback[bot] 2024-04-20 10:21:02 +02:00 committed by GitHub
parent 83738f21a3
commit 4832100d76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 142 additions and 64 deletions

View file

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023 Felix Fontein <felix@fontein.de>
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
# SPDX-License-Identifier: BSD-2-Clause
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import datetime as _datetime
import sys
_USE_TIMEZONE = sys.version_info >= (3, 6)
def ensure_timezone_info(value):
if not _USE_TIMEZONE or value.tzinfo is not None:
return value
return value.astimezone(_datetime.timezone.utc)
def fromtimestamp(value):
if _USE_TIMEZONE:
return _datetime.fromtimestamp(value, tz=_datetime.timezone.utc)
return _datetime.utcfromtimestamp(value)
def now():
if _USE_TIMEZONE:
return _datetime.datetime.now(tz=_datetime.timezone.utc)
return _datetime.datetime.utcnow()