summaryrefslogtreecommitdiff
path: root/sphinx/util/http_date.py
blob: e3c452419d23c41fe5849ba8214d1f9feba6b022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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