Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Parse and format HTTP dates, such as Last-Modified and If-Modified-Since headers.
httpdate is a Python library for parsing and formatting HTTP date fields that are
used in headers like Last-Modified
and If-Modified-Since
. It does so in strict
accordance with RFC 9110 Section 5.6.7.
It has only one dependency, which is my tiny leapseconds library that just provides a tuple of official leap seconds.
All HTTP dates (eg, in Last-Modified
headers) must be sent in this format:
# IMF-fixdate
Sun, 06 Nov 1994 08:49:37 GMT
However, RFC 9110 states that recipients must also accept two other obsolete formats:
# rfc850-date
Sunday, 06-Nov-94 08:49:37 GMT
# asctime-date
Sun Nov 6 08:49:37 1994
RFC 9110 criteria for the HTTP date field includes the following:
23:59:60
).1900
or above.It isn't stated explicitly in the RFCs, but httpdate
will only consider a leap second
semantically correct if it's an official leap second.
pip install httpdate
from httpdate import is_valid_httpdate, httpdate_to_unixtime, unixtime_to_httpdate
# Check if an HTTP date (eg, `Last-Modified` header) is valid:
is_valid_httpdate("Sun, 06 Nov 1994 08:49:37 GMT")
try:
# Parse an HTTP date:
httpdate_to_unixtime("Sun, 06 Nov 1994 08:49:37 GMT")
# Parse an HTTP date (rfc850-date):
httpdate_to_unixtime("Sunday, 06-Nov-94 08:49:37 GMT")
# Parse an HTTP date (asctime-date):
httpdate_to_unixtime("Sun Nov 6 08:49:37 1994")
except ValueError:
# Not a valid HTTP date string.
pass
# Format a Unix timestamp as an HTTP date:
try:
unixtime_to_httpdate(784111777)
except ValueError:
# Outside the range supported by the operating system.
pass
is_valid_httpdate(httpdate)
:
httpdate (str)
: An HTTP date string.bool
: True if httpdate
is a valid HTTP date string, otherwise False.TypeError
if httpdate
is not of type str
.httpdate_to_unixtime(httpdate)
:
httpdate (str)
: An HTTP date string.int
: A Unix timestamp (int
).TypeError
if httpdate
is not of type str
.ValueError
if httpdate
is not a valid HTTP date string.unixtime_to_httpdate(unixtime)
:
unixtime (int)
: A Unix timestamp.str
: An HTTP date string.TypeError
if unixtime
is not of type int
.ValueError
if unixtime
represents a year before 1900, or if it is outside
the range supported by the operating system.Unix timestamps are unambiguous, efficient, and can easily be converted to other date-time formats using only the standard library.
When a Last-Modified
header is semantically correct, this conversion — from string to
Unix timestamp and back to string — is lossless. (The only exception is leap seconds;
for example Sat, 31 Dec 2016 23:59:60 GMT
would be returned as Sun, 01 Jan 2017 00:00:00 GMT
. Recipients should interpret these as being identical anyway.)
If you want to store the original string instead, just use is_valid_httpdate()
to
validate before storing.
httpdate
is distributed under the terms of the MIT license.
FAQs
Parse and format HTTP dates, such as Last-Modified and If-Modified-Since headers.
We found that httpdate demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.