]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add formatting to InspIRCd::TimeString; switch all code to use it.
authorPeter Powell <petpow@saberuk.com>
Fri, 2 May 2014 15:39:40 +0000 (16:39 +0100)
committerAttila Molnar <attilamolnar@hush.com>
Wed, 25 Jun 2014 12:31:25 +0000 (14:31 +0200)
m_httpd also now uses the correct timestamp format.

Windows-specific fixes by @attilamolnar, original PR #849

include/inspircd.h
src/coremods/core_info/cmd_time.cpp
src/filelogger.cpp
src/helperfuncs.cpp
src/modules/m_alltime.cpp
src/modules/m_httpd.cpp

index 0f6a411a3905f4a831beeba6deb5217a73ade004..1b1543e28dd661dd87053f8f314b6c8c558b5b7a 100644 (file)
@@ -662,8 +662,11 @@ class CoreExport InspIRCd
        void Cleanup();
 
        /** Return a time_t as a human-readable string.
+        * @param format The format to retrieve the date/time in. See `man 3 strftime`
+        * for more information. If NULL, "%a %b %d %T %Y" is assumed.
+        * @return A string representing the given date/time.
         */
-       static std::string TimeString(time_t curtime);
+       static std::string TimeString(time_t curtime, const char* format = NULL);
 
        /** Begin execution of the server.
         * NOTE: this function NEVER returns. Internally,
index 6a10dc3278749bf4050f9389b096c5c6a26ced9c..95cfb12fddc5bcb0c2034359f2fc0b7e7fff457e 100644 (file)
@@ -32,12 +32,8 @@ CmdResult CommandTime::Handle (const std::vector<std::string>& parameters, User
        if (parameters.size() > 0 && parameters[0] != ServerInstance->Config->ServerName)
                return CMD_SUCCESS;
 
-       time_t local = ServerInstance->Time();
-       struct tm* timeinfo = localtime(&local);
-       const std::string& humanTime = asctime(timeinfo);
-
        user->SendText(":%s %03d %s %s :%s", ServerInstance->Config->ServerName.c_str(), RPL_TIME, user->nick.c_str(),
-               ServerInstance->Config->ServerName.c_str(), humanTime.c_str());
+               ServerInstance->Config->ServerName.c_str(), InspIRCd::TimeString(ServerInstance->Time()).c_str());
 
        return CMD_SUCCESS;
 }
index edb753a50c3b112364a977501f639315a5ff03a1..5786758da5f691907187efda8507ed6732a07f39 100644 (file)
@@ -45,10 +45,7 @@ void FileLogStream::OnLog(LogLevel loglevel, const std::string &type, const std:
 
        if (ServerInstance->Time() != LAST)
        {
-               time_t local = ServerInstance->Time();
-               struct tm *timeinfo = localtime(&local);
-
-               TIMESTR.assign(asctime(timeinfo), 24);
+               TIMESTR = InspIRCd::TimeString(ServerInstance->Time());
                LAST = ServerInstance->Time();
        }
 
index 6316d1e3432edaacbf3aa0045b91b4e26f635d3e..9ad481bb8f2ce707a5853f51c4cf2354856e518d 100644 (file)
@@ -403,7 +403,7 @@ const char* InspIRCd::Format(const char* formatString, ...)
        return ret;
 }
 
-std::string InspIRCd::TimeString(time_t curtime)
+std::string InspIRCd::TimeString(time_t curtime, const char* format)
 {
 #ifdef _WIN32
        if (curtime < 0)
@@ -424,7 +424,15 @@ std::string InspIRCd::TimeString(time_t curtime)
        else if (timeinfo->tm_year + 1900 < 1000)
                timeinfo->tm_year = 0;
 
-       return std::string(asctime(timeinfo),24);
+       // This is the default format used by asctime without the terminating new line.
+       if (!format)
+               format = "%a %b %d %H:%M:%S %Y";
+
+       char buffer[512];
+       if (!strftime(buffer, sizeof(buffer), format, timeinfo))
+               buffer[0] = '\0';
+
+       return buffer;
 }
 
 std::string InspIRCd::GenRandomStr(int length, bool printable)
index 58f7c4fb5a484f4076a661f10c8f8371f6d38091..ceee4abbc198919bcd50a3dc92374d3cfcaf1901 100644 (file)
@@ -31,9 +31,7 @@ class CommandAlltime : public Command
 
        CmdResult Handle(const std::vector<std::string> &parameters, User *user)
        {
-               char fmtdate[64];
-               time_t now = ServerInstance->Time();
-               strftime(fmtdate, sizeof(fmtdate), "%Y-%m-%d %H:%M:%S", gmtime(&now));
+               const std::string fmtdate = InspIRCd::TimeString(ServerInstance->Time(), "%Y-%m-%d %H:%M:%S");
 
                std::string msg = ":" + ServerInstance->Config->ServerName + " NOTICE " + user->nick + " :System time is " + fmtdate + " (" + ConvToStr(ServerInstance->Time()) + ") on " + ServerInstance->Config->ServerName;
 
index dda39afec1ce65b0087e20291b18a54aec5afe8e..efd285f8c07f3ce1b26dcbc970d6a9884217d11f 100644 (file)
@@ -185,12 +185,7 @@ class HttpServerSocket : public BufferedSocket
 
                WriteData(http_version + " "+ConvToStr(response)+" "+Response(response)+"\r\n");
 
-               time_t local = ServerInstance->Time();
-               struct tm *timeinfo = gmtime(&local);
-               char *date = asctime(timeinfo);
-               date[strlen(date) - 1] = '\0';
-               rheaders.CreateHeader("Date", date);
-
+               rheaders.CreateHeader("Date", InspIRCd::TimeString(ServerInstance->Time(), "%a, %d %b %Y %H:%M:%S GMT"));
                rheaders.CreateHeader("Server", INSPIRCD_BRANCH);
                rheaders.SetHeader("Content-Length", ConvToStr(size));