aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Sadie Powell2025-03-23 11:32:12 +0000
committerGravatar Sadie Powell2025-03-23 14:40:17 +0000
commitf76dcfc20f72c45d14d76b2d82f707d0a513d1bb (patch)
tree8a7c3e4517881d6b7ca77d9e1b04c7f14630f0c4
parentAllow using the long duration format in the xline message. (diff)
Add constants for the common time formats.
-rw-r--r--include/timeutils.h12
-rw-r--r--src/coremods/core_info/cmd_time.cpp2
-rw-r--r--src/helperfuncs.cpp2
-rw-r--r--src/modules/extra/m_log_json.cpp2
-rw-r--r--src/modules/m_alltime.cpp2
-rw-r--r--src/modules/m_httpd.cpp2
6 files changed, 17 insertions, 5 deletions
diff --git a/include/timeutils.h b/include/timeutils.h
index 5ac185e22..dd6585cfb 100644
--- a/include/timeutils.h
+++ b/include/timeutils.h
@@ -74,6 +74,18 @@ namespace Duration
namespace Time
{
+ /** A short time format picked for being readable (e.g. "Sun 23 Mar 2025 10:20:30") */
+ inline constexpr const char* DEFAULT_SHORT = "%a %d %b %Y %H:%M:%S";
+
+ /** A long time format picked for being readable (e.g. "Sunday, 23 March 2025 @ 10:20:30 GMT"). */
+ inline constexpr const char* DEFAULT_LONG = "%A, %d %B %Y @ %H:%M:%S %Z";
+
+ /** The time format specified in ISO 8601 (e.g. "2025-03-23T10:20:30+0000") */
+ inline constexpr const char* ISO_8601 = "%Y-%m-%dT%H:%M:%S%z";
+
+ /** The time format specified in RFC 1123 (e.g. "Sun, 23 Mar 2025 10:20:30 GMT") */
+ inline constexpr const char* RFC_1123 = "%a, %d %b %Y %H:%M:%S %Z";
+
/** Converts a UNIX timestamp to a time string.
*
* @param ts The timestamp to convert to a string.
diff --git a/src/coremods/core_info/cmd_time.cpp b/src/coremods/core_info/cmd_time.cpp
index ee606e4e8..b7dbd8ffa 100644
--- a/src/coremods/core_info/cmd_time.cpp
+++ b/src/coremods/core_info/cmd_time.cpp
@@ -39,7 +39,7 @@ CmdResult CommandTime::Handle(User* user, const Params& parameters)
if (!parameters.empty() && !irc::equals(parameters[0], ServerInstance->Config->ServerName))
return CmdResult::SUCCESS;
- auto timestr = Time::ToString(ServerInstance->Time(), "%A, %d %B %Y @ %H:%M:%S %Z");
+ auto timestr = Time::ToString(ServerInstance->Time(), Time::DEFAULT_LONG);
timestr += INSP_FORMAT(" ({})", ServerInstance->Time());
user->WriteRemoteNumeric(RPL_TIME, ServerInstance->Config->GetServerName(), timestr);
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp
index 3c5f0f399..756aeb11b 100644
--- a/src/helperfuncs.cpp
+++ b/src/helperfuncs.cpp
@@ -617,7 +617,7 @@ std::string Time::ToString(time_t curtime, const char* format, bool utc)
// This is the default format used by asctime without the terminating new line.
if (!format)
- format = "%a %b %d %Y %H:%M:%S";
+ format = Time::DEFAULT_SHORT;
char buffer[512];
if (!strftime(buffer, sizeof(buffer), format, timeinfo))
diff --git a/src/modules/extra/m_log_json.cpp b/src/modules/extra/m_log_json.cpp
index d1886c809..159e661a5 100644
--- a/src/modules/extra/m_log_json.cpp
+++ b/src/modules/extra/m_log_json.cpp
@@ -94,7 +94,7 @@ public:
if (prevtime != time)
{
prevtime = time;
- timestr = Time::ToString(prevtime, "%Y-%m-%dT%H:%M:%S%z");
+ timestr = Time::ToString(prevtime, Time::ISO_8601);
}
const auto* levelstr = Log::LevelToString(level);
diff --git a/src/modules/m_alltime.cpp b/src/modules/m_alltime.cpp
index 64c6a8638..5f3d09f4b 100644
--- a/src/modules/m_alltime.cpp
+++ b/src/modules/m_alltime.cpp
@@ -37,7 +37,7 @@ public:
CmdResult Handle(User* user, const Params& parameters) override
{
- auto timestr = Time::ToString(ServerInstance->Time(), "%A, %d %B %Y @ %H:%M:%S %Z", true);
+ auto timestr = Time::ToString(ServerInstance->Time(), Time::DEFAULT_LONG, true);
timestr += INSP_FORMAT(" ({})", ServerInstance->Time());
user->WriteRemoteNumeric(RPL_TIME, ServerInstance->Config->ServerName, timestr);
diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp
index c253478b7..b38a8cda2 100644
--- a/src/modules/m_httpd.cpp
+++ b/src/modules/m_httpd.cpp
@@ -271,7 +271,7 @@ public:
{
WriteData(INSP_FORMAT("HTTP/{}.{} {} {}\r\n", parser.http_major ? parser.http_major : 1, parser.http_major ? parser.http_minor : 1, response, http_status_str((http_status)response)));
- rheaders.CreateHeader("Date", Time::ToString(ServerInstance->Time(), "%a, %d %b %Y %H:%M:%S GMT", true));
+ rheaders.CreateHeader("Date", Time::ToString(ServerInstance->Time(), Time::RFC_1123, true));
rheaders.CreateHeader("Server", INSPIRCD_BRANCH);
rheaders.SetHeader("Content-Length", ConvToStr(size));