mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-31 10:41:08 +00:00
time: implement and test RFC1123 timestamp string
HTTP 1.1 responses require a timestamp header with a format specified (currently) by: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.7 This specific format is defined in RFC1123: https://www.rfc-editor.org/rfc/rfc1123#page-55 The libevent implementation can be referenced in evutil_time.c evutil_date_rfc1123()
This commit is contained in:
parent
eea38787b9
commit
ee62405cce
@ -385,6 +385,21 @@ BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
|
||||
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_FormatRFC1123DateTime)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(std::numeric_limits<int64_t>::max()), "");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(253402300800), "");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(253402300799), "Fri, 31 Dec 9999 23:59:59 GMT");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(253402214400), "Fri, 31 Dec 9999 00:00:00 GMT");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(1717429609), "Mon, 03 Jun 2024 15:46:49 GMT");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(0), "Thu, 01 Jan 1970 00:00:00 GMT");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(-1), "Wed, 31 Dec 1969 23:59:59 GMT");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(-1717429609), "Sat, 31 Jul 1915 08:13:11 GMT");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(-62167219200), "Sat, 01 Jan 0000 00:00:00 GMT");
|
||||
BOOST_CHECK_EQUAL(FormatRFC1123DateTime(-62167219201), "");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_FormatMoney)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
#include <util/check.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
@ -17,6 +18,9 @@
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
static constexpr std::array<std::string_view, 7> weekdays{"Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"}; // 1970-01-01 was a Thursday.
|
||||
static constexpr std::array<std::string_view, 12> months{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
|
||||
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
||||
|
||||
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
|
||||
@ -117,6 +121,24 @@ std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
|
||||
return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
|
||||
}
|
||||
|
||||
std::string FormatRFC1123DateTime(int64_t time)
|
||||
{
|
||||
if (time < -62167219200 || 253402300799 < time) {
|
||||
// 4-digit year, so only support years 0 to 9999
|
||||
return "";
|
||||
}
|
||||
const std::chrono::sys_seconds secs{std::chrono::seconds{time}};
|
||||
const auto days{std::chrono::floor<std::chrono::days>(secs)};
|
||||
const auto w{days.time_since_epoch().count() % 7}; // will be in the range [-6, 6]
|
||||
std::string_view weekday{weekdays.at(w >= 0 ? w : w + 7)};
|
||||
const std::chrono::year_month_day ymd{days};
|
||||
std::string_view month{months.at(unsigned{ymd.month()} - 1)};
|
||||
const std::chrono::hh_mm_ss hms{secs - days};
|
||||
// examples: Mon, 27 Jul 2009 12:28:53 GMT
|
||||
// Fri, 31 May 2024 19:18:04 GMT
|
||||
return strprintf("%03s, %02u %03s %04i %02i:%02i:%02i GMT", weekday, unsigned{ymd.day()}, month, signed{ymd.year()}, hms.hours().count(), hms.minutes().count(), hms.seconds().count());
|
||||
}
|
||||
|
||||
struct timeval MillisToTimeval(int64_t nTimeout)
|
||||
{
|
||||
struct timeval timeout;
|
||||
|
||||
@ -136,6 +136,12 @@ std::string FormatISO8601DateTime(int64_t nTime);
|
||||
std::string FormatISO8601Date(int64_t nTime);
|
||||
std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
|
||||
|
||||
/**
|
||||
* RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14
|
||||
* Used in HTTP/1.1 responses
|
||||
*/
|
||||
std::string FormatRFC1123DateTime(int64_t nTime);
|
||||
|
||||
/**
|
||||
* Convert milliseconds to a struct timeval for e.g. select.
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user