c 2b 2b localtime unsafe

Solutions on MaxInterview for c 2b 2b localtime unsafe by the best coders in the world

showing results for - "c 2b 2b localtime unsafe"
Paco
21 Jan 2020
1inline std::tm localtime_xp(std::time_t timer)
2{
3    std::tm bt {};
4#if defined(__unix__)
5    localtime_r(&timer, &bt);
6#elif defined(_MSC_VER)
7    localtime_s(&bt, &timer);
8#else
9    static std::mutex mtx;
10    std::lock_guard<std::mutex> lock(mtx);
11    bt = *std::localtime(&timer);
12#endif
13    return bt;
14}
15
16// default = "YYYY-MM-DD HH:MM:SS"
17inline std::string time_stamp(const std::string& fmt = "%F %T")
18{
19    auto bt = localtime_xp(std::time(0));
20    char buf[64];
21    return {buf, std::strftime(buf, sizeof(buf), fmt.c_str(), &bt)};
22}
23