Skip to content

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

docs/docs/string_formatting.md

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ The following are methods to display a `DateTime` instance as a common format:
5656
>>> dt.to_cookie_string()
5757
'Thursday, 25-Dec-1975 14:15:16 EST'
5858

59+
>>> dt.to_http_date_string()
60+
'Thu, 25 Dec 1975 19:15:16 GMT'
61+
5962
>>> dt.to_iso8601_string()
6063
'1975-12-25T14:15:16-0500'
6164

pendulum/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
RFC1036 = "ddd, DD MMM YY HH:mm:ss ZZ"
3131
RFC1123 = "ddd, DD MMM YYYY HH:mm:ss ZZ"
3232
RFC2822 = "ddd, DD MMM YYYY HH:mm:ss ZZ"
33+
RFC7231 = "ddd, DD MMM YYYY HH:mm:ss zz"
3334
RFC3339 = ISO8601
3435
RFC3339_EXTENDED = ISO8601_EXTENDED
3536
RSS = "ddd, DD MMM YYYY HH:mm:ss ZZ"

pendulum/datetime.py

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .constants import RFC1036
2424
from .constants import RFC1123
2525
from .constants import RFC2822
26+
from .constants import RFC7231
2627
from .constants import RSS
2728
from .constants import SATURDAY
2829
from .constants import SECONDS_PER_DAY
@@ -56,6 +57,7 @@ class DateTime(datetime.datetime, Date):
5657
"rfc1123": RFC1123,
5758
"rfc2822": RFC2822,
5859
"rfc3339": lambda dt: dt.isoformat(),
60+
"rfc7231": RFC7231,
5961
"rss": RSS,
6062
"w3c": W3C,
6163
}
@@ -280,6 +282,14 @@ def to_cookie_string(self) -> str:
280282
"""
281283
return self._to_string("cookie", locale="en")
282284

285+
def to_http_date_string(self) -> str:
286+
"""
287+
Format the instance as HTTP Date (RFC 7231).
288+
289+
HTTP dates are always expressed in GMT, never in local time.
290+
"""
291+
return self.in_timezone("GMT")._to_string("rfc7231", locale="en")
292+
283293
def to_iso8601_string(self) -> str:
284294
"""
285295
Format the instance as ISO 8601.

tests/datetime/test_strings.py

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def test_to_cookie_string():
3838
assert d.to_cookie_string() == "Thursday, 25-Dec-1975 14:15:16 EST"
3939

4040

41+
def test_to_http_date_string():
42+
d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
43+
assert d.to_http_date_string() == "Thu, 25 Dec 1975 19:15:16 GMT"
44+
45+
4146
def test_to_iso8601_string():
4247
d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
4348
assert d.to_iso8601_string() == "1975-12-25T14:15:16-05:00"

0 commit comments

Comments
 (0)