Skip to content

Backport PR #25918 on branch v3.7.x (migrate from utcfromtimestamp to fromtimestamp) #25972

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

Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion doc/api/prev_api_changes/api_changes_3.7.0/removals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Removals
These methods convert from unix timestamps to matplotlib floats, but are not
used internally to Matplotlib, and should not be needed by end users. To
convert a unix timestamp to datetime, simply use
`datetime.datetime.utcfromtimestamp`, or to use NumPy `~numpy.datetime64`
`datetime.datetime.fromtimestamp`, or to use NumPy `~numpy.datetime64`
``dt = np.datetime64(e*1e6, 'us')``.

Locator and Formatter wrapper methods
Expand Down
5 changes: 3 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import matplotlib

from datetime import timezone
from datetime import datetime
import time

Expand All @@ -36,8 +37,8 @@

# Parse year using SOURCE_DATE_EPOCH, falling back to current time.
# https://reproducible-builds.org/specs/source-date-epoch/
sourceyear = datetime.utcfromtimestamp(
int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))).year
sourceyear = datetime.fromtimestamp(
int(os.environ.get('SOURCE_DATE_EPOCH', time.time())), timezone.utc).year

# If your extensions are in another directory, add it here. If the directory
# is relative to the documentation root, use os.path.abspath to make it
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import codecs
from datetime import timezone
from datetime import datetime
from enum import Enum
from functools import total_ordering
Expand Down Expand Up @@ -153,7 +154,7 @@ def _create_pdf_info_dict(backend, metadata):
# See https://reproducible-builds.org/specs/source-date-epoch/
source_date_epoch = os.getenv("SOURCE_DATE_EPOCH")
if source_date_epoch:
source_date = datetime.utcfromtimestamp(int(source_date_epoch))
source_date = datetime.fromtimestamp(int(source_date_epoch), timezone.utc)
source_date = source_date.replace(tzinfo=UTC)
else:
source_date = datetime.today()
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,9 @@ def _print_ps(
# See https://reproducible-builds.org/specs/source-date-epoch/
source_date_epoch = os.getenv("SOURCE_DATE_EPOCH")
dsc_comments["CreationDate"] = (
datetime.datetime.utcfromtimestamp(
int(source_date_epoch)).strftime("%a %b %d %H:%M:%S %Y")
datetime.datetime.fromtimestamp(
int(source_date_epoch),
datetime.timezone.utc).strftime("%a %b %d %H:%M:%S %Y")
if source_date_epoch
else time.ctime())
dsc_comments = "\n".join(
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def _write_metadata(self, metadata):
# See https://reproducible-builds.org/specs/source-date-epoch/
date = os.getenv("SOURCE_DATE_EPOCH")
if date:
date = datetime.datetime.utcfromtimestamp(int(date))
date = datetime.datetime.fromtimestamp(int(date), datetime.timezone.utc)
metadata['Date'] = date.replace(tzinfo=UTC).isoformat()
else:
metadata['Date'] = datetime.datetime.today().isoformat()
Expand Down