Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ localtime unsafe

inline std::tm localtime_xp(std::time_t timer)
{
    std::tm bt {};
#if defined(__unix__)
    localtime_r(&timer, &bt);
#elif defined(_MSC_VER)
    localtime_s(&bt, &timer);
#else
    static std::mutex mtx;
    std::lock_guard<std::mutex> lock(mtx);
    bt = *std::localtime(&timer);
#endif
    return bt;
}

// default = "YYYY-MM-DD HH:MM:SS"
inline std::string time_stamp(const std::string& fmt = "%F %T")
{
    auto bt = localtime_xp(std::time(0));
    char buf[64];
    return {buf, std::strftime(buf, sizeof(buf), fmt.c_str(), &bt)};
}
Comment

c++ localtime unsafe

inline std::tm localtime_xp(std::time_t timer)
{
    std::tm bt {};
#if defined(__unix__)
    localtime_r(&timer, &bt);
#elif defined(_MSC_VER)
    localtime_s(&bt, &timer);
#else
    static std::mutex mtx;
    std::lock_guard<std::mutex> lock(mtx);
    bt = *std::localtime(&timer);
#endif
    return bt;
}

// default = "YYYY-MM-DD HH:MM:SS"
inline std::string time_stamp(const std::string& fmt = "%F %T")
{
    auto bt = localtime_xp(std::time(0));
    char buf[64];
    return {buf, std::strftime(buf, sizeof(buf), fmt.c_str(), &bt)};
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: . Shell sort in c++ 
Cpp :: what does map.count() return in c++ 
Cpp :: convert c++ to mips 
Cpp :: opengl draw cresent moon c++ 
Cpp :: Types of Triangles Based on Angles in c++ 
Cpp :: pointers mcq sanfoundry 
Cpp :: reading matrix from text file in c++ and adding them and then storing them in oother c++ file 
Cpp :: pimpl c++ 
Cpp :: big o notation practice c++ 
Cpp :: accepting string with space on same line C++ 
Cpp :: https://stackoverflow.comInstance of a Character in a String c++ 
Cpp :: how to open program in c++ 
Cpp :: Fill 2-dimensional array with value 
Cpp :: Equalize problem codeforces 
Cpp :: Difference Array | Range update query in O 
Cpp :: C++ meaning :: 
Cpp :: error c4001 
Cpp :: qt_invok 
Cpp :: sort c++ stl 
Cpp :: Maximum Cake Tastiness codeforces solution 
Cpp :: pros millis() 
Cpp :: check if number is positive or negative in cpp 
Cpp :: find largest number in each row in array c++ using function 
Cpp :: c++ FAILED: objekt aufruf : symbol(s) not found for architecture x86_64 
Cpp :: c++ ide online 
Cpp :: auto keyword 
Cpp :: minimum no of jump required to reach end of arry 
Cpp :: sin trigonometric function 
Cpp :: c++ n in regex 
Cpp :: QMetaObject_invokeMethod 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =