MCPcopy
hub / github.com/pallets/werkzeug / http_date

Function http_date

src/werkzeug/http.py:1051–1080  ·  view source on GitHub ↗

Format a datetime object or timestamp into an :rfc:`2822` date string. This is a wrapper for :func:`email.utils.format_datetime`. It assumes naive datetime objects are in UTC instead of raising an exception. :param timestamp: The datetime or timestamp to format. Defaults to

(
    timestamp: datetime | date | int | float | struct_time | None = None,
)

Source from the content-addressed store, hash-verified

1049
1050
1051def http_date(
1052 timestamp: datetime | date | int | float | struct_time | None = None,
1053) -> str:
1054 """Format a datetime object or timestamp into an :rfc:`2822` date
1055 string.
1056
1057 This is a wrapper for :func:`email.utils.format_datetime`. It
1058 assumes naive datetime objects are in UTC instead of raising an
1059 exception.
1060
1061 :param timestamp: The datetime or timestamp to format. Defaults to
1062 the current time.
1063
1064 .. versionchanged:: 2.0
1065 Use ``email.utils.format_datetime``. Accept ``date`` objects.
1066 """
1067 if isinstance(timestamp, date):
1068 if not isinstance(timestamp, datetime):
1069 # Assume plain date is midnight UTC.
1070 timestamp = datetime.combine(timestamp, time(), tzinfo=timezone.utc)
1071 else:
1072 # Ensure datetime is timezone-aware.
1073 timestamp = _dt_as_utc(timestamp)
1074
1075 return email.utils.format_datetime(timestamp, usegmt=True)
1076
1077 if isinstance(timestamp, struct_time):
1078 timestamp = mktime(timestamp)
1079
1080 return email.utils.formatdate(timestamp, usegmt=True)
1081
1082
1083def parse_age(value: str | None = None) -> timedelta | None:

Callers 6

get_headersMethod · 0.85
dump_cookieFunction · 0.85
__call__Method · 0.85
make_conditionalMethod · 0.85
retry_afterMethod · 0.85

Calls 1

_dt_as_utcFunction · 0.85

Tested by 1