Commit f48411ef authored by jan.koester's avatar jan.koester
Browse files

use chrono now

parent c31404c6
Loading
Loading
Loading
Loading
+15 −30
Changes for src/session.cpp: 15 added lines, 30 removed lines.
Original line number Diff line number Diff line
@@ -30,11 +30,7 @@

#include <httppp/exception.h>
#include <uuidp.h>

#ifndef Windows
#include <unistd.h>
#include <sys/time.h>
#endif // Windows
#include <chrono>


#include "session.h"
@@ -82,33 +78,22 @@ const std::string &blogi::Session::createSession(std::string &buf,const std::str

    dbpp::SQL sql;
    dbpp::DBResult res;
#ifdef  Windows
    time_t t = time(NULL);
    struct tm time_info = { 0 }; // Renamed 'time' to 'time_info' to avoid conflict with the function name
    char ttmp[26];

    // Use localtime_s, the secure and thread-safe Windows equivalent of localtime_r
    // Note the different argument order: localtime_s(tmDest, sourceTime)
    if (localtime_s(&time_info, &t) == 0) {
        // Use asctime_s, the secure and thread-safe Windows equivalent of asctime_r
        // Note the different argument order and the required buffer size
        if (asctime_s(ttmp, sizeof(ttmp), &time_info) == 0) {
            // The rest of the code is portable
            sql << "INSERT INTO session (id,useragent,created) VALUES ('" << sessionid.c_str() << "','" << useragent.c_str() << "','" << ttmp << "');";
            _sessiondb->exec(sql, res);
        }
    }
#else
    time_t t = time(NULL);
    struct tm time = { 0 };
    char ttmp[26];
    if (localtime_r(&t, &time) == 0) {
        if (asctime_r(&time, ttmp) == 0) {
            sql << "INSERT INTO session (id,useragent,created) VALUES ('" << sessionid.c_str() << "','" << useragent.c_str() << "','" << ttmp << "');";

    auto now = std::chrono::system_clock::now();
    // Use std::format to directly format the time point
    // The format string can be customized
    std::string ttmp = std::format("{:%a %b %d %T %Y}", now);

    sql << "INSERT INTO session (id,useragent,created) VALUES ('"
          << sessionid.c_str() << "','" << useragent.c_str() << "','"
          << ttmp.c_str() << "');";
    try{
        _sessiondb->exec(sql, res);
    }catch(char *err){
        libhttppp::HTTPException excep;
        excep[libhttppp::HTTPException::Error] << "createSession DbError: " << err;
        throw excep;
    }
    }
#endif //  Windows
    return buf;
};