summaryrefslogtreecommitdiff
path: root/sphinx/util/http_date.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/util/http_date.py')
-rw-r--r--sphinx/util/http_date.py20
1 files changed, 20 insertions, 0 deletions
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