diff options
| author | Adam Turner <9087854+aa-turner@users.noreply.github.com> | 2022-09-09 09:49:37 +0100 |
|---|---|---|
| committer | Adam Turner <9087854+aa-turner@users.noreply.github.com> | 2023-01-03 19:02:38 +0000 |
| commit | 3b3ce9cf7b44eb2662242e1d668026c5d6c96e85 (patch) | |
| tree | 8986a3e80dd6baa1e753bb6f81da3fab26f3ff7f | |
| parent | 8c5e7013ea5f6a50e3cc3130b22205a85ba87fab (diff) | |
| download | sphinx-git-3b3ce9cf7b44eb2662242e1d668026c5d6c96e85.tar.gz | |
Move HTTP-date formatting utils to `utils.http_date`
| -rw-r--r-- | doc/extdev/deprecated.rst | 10 | ||||
| -rw-r--r-- | sphinx/transforms/post_transforms/images.py | 3 | ||||
| -rw-r--r-- | sphinx/util/__init__.py | 20 | ||||
| -rw-r--r-- | sphinx/util/http_date.py | 20 |
4 files changed, 37 insertions, 16 deletions
diff --git a/doc/extdev/deprecated.rst b/doc/extdev/deprecated.rst index f613bc8e8..78eda5111 100644 --- a/doc/extdev/deprecated.rst +++ b/doc/extdev/deprecated.rst @@ -22,6 +22,16 @@ The following is a list of deprecated interfaces. - Removed - Alternatives + * - ``sphinx.util.epoch_to_rfc1123`` + - 6.1 + - 8.0 + - ``sphinx.util.http_date.epoch_to_rfc1123`` + + * - ``sphinx.util.rfc1123_to_epoch`` + - 6.1 + - 8.0 + - ``sphinx.util.http_date.rfc1123_to_epoch`` + * - ``sphinx.util.status_iterator`` - 6.1 - 8.0 diff --git a/sphinx/transforms/post_transforms/images.py b/sphinx/transforms/post_transforms/images.py index acf3ba486..1871a0f9c 100644 --- a/sphinx/transforms/post_transforms/images.py +++ b/sphinx/transforms/post_transforms/images.py @@ -12,7 +12,8 @@ from docutils import nodes from sphinx.application import Sphinx from sphinx.locale import __ from sphinx.transforms import SphinxTransform -from sphinx.util import epoch_to_rfc1123, logging, requests, rfc1123_to_epoch, sha1 +from sphinx.util import logging, requests, sha1 +from sphinx.util.http_date import epoch_to_rfc1123, rfc1123_to_epoch from sphinx.util.images import get_image_extension, guess_mimetype, parse_data_uri from sphinx.util.osutil import ensuredir diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index 6f742cfe5..f17d4e7a5 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -10,10 +10,8 @@ import sys import tempfile import traceback import warnings -from datetime import datetime from importlib import import_module from os import path -from time import mktime, strptime from typing import IO, TYPE_CHECKING, Any, Iterable from urllib.parse import parse_qsl, quote_plus, urlencode, urlsplit, urlunsplit @@ -21,6 +19,7 @@ from sphinx.deprecation import RemovedInSphinx70Warning, deprecated_alias from sphinx.errors import ExtensionError, FiletypeNotFoundError, SphinxParallelError from sphinx.locale import __ from sphinx.util import display as _display +from sphinx.util import http_date as _http_date from sphinx.util import logging from sphinx.util.console import strip_colors from sphinx.util.matching import patfilter # noqa: F401 @@ -444,19 +443,6 @@ def isurl(url: str) -> bool: return bool(url) and '://' in url -def epoch_to_rfc1123(epoch: float) -> str: - """Convert datetime format epoch to RFC1123.""" - from babel.dates import format_datetime - - dt = datetime.fromtimestamp(epoch) - fmt = 'EEE, dd LLL yyyy hh:mm:ss' - return format_datetime(dt, fmt, locale='en') + ' GMT' - - -def rfc1123_to_epoch(rfc1123: str) -> float: - return mktime(strptime(rfc1123, '%a, %d %b %Y %H:%M:%S %Z')) - - def xmlname_checker() -> re.Pattern: # https://www.w3.org/TR/REC-xml/#NT-Name name_start_chars = [ @@ -491,6 +477,8 @@ deprecated_alias('sphinx.util', 'status_iterator': _display.status_iterator, 'SkipProgressMessage': _display.SkipProgressMessage, 'progress_message': _display.progress_message, + 'epoch_to_rfc1123': _http_date.epoch_to_rfc1123, + 'rfc1123_to_epoch': _http_date.rfc1123_to_epoch, }, RemovedInSphinx70Warning, { @@ -498,4 +486,6 @@ deprecated_alias('sphinx.util', 'status_iterator': 'sphinx.util.display.status_iterator', 'SkipProgressMessage': 'sphinx.util.display.SkipProgressMessage', 'progress_message': 'sphinx.util.display.progress_message', + 'epoch_to_rfc1123': 'sphinx.http_date.epoch_to_rfc1123', + 'rfc1123_to_epoch': 'sphinx.http_date.rfc1123_to_epoch', }) diff --git a/sphinx/util/http_date.py b/sphinx/util/http_date.py new file mode 100644 index 000000000..e3c452419 --- /dev/null +++ b/sphinx/util/http_date.py @@ -0,0 +1,20 @@ +"""Convert times to and from HTTP-date serialisations. + +Reference: https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1 +""" + +import time +from email.utils import formatdate, parsedate + + +def epoch_to_rfc1123(epoch: float) -> str: + """Return HTTP-date string from epoch offset.""" + return formatdate(epoch, usegmt=True) + + +def rfc1123_to_epoch(rfc1123: str) -> float: + """Return epoch offset from HTTP-date string.""" + t = parsedate(rfc1123) + if t: + return time.mktime(t) + raise ValueError |
