Commit 37f8dbe7 authored by captainwong's avatar captainwong

fix chrono wrapper linux build

parent c9174ca4
#pragma once #pragma once
#include "../base/config.h" #include "../base/config.h"
#include <chrono>
#include <string> #ifdef __GNUG__
#include <ctime> #include <string.h> // memcpy
#include <sstream> #endif
#include <iomanip>
#include <chrono>
namespace jlib { #include <string>
#include <ctime>
#ifdef __GNUG__ #include <sstream>
inline void localtime_s(struct tm* tmtm, const time_t* t) { #include <iomanip>
memcpy(tmtm, localtime(t), sizeof(tm));
} namespace jlib {
#else
#ifdef __GNUG__
#endif #include <string.h> // memcpy
inline void localtime_s(struct tm* tmtm, const time_t* t) {
// section: memcpy(tmtm, localtime(t), sizeof(tm));
// 0 for YYYY-mm-dd HH:MM:SS }
// 1 for YYYY-mm-dd #else
// 2 for HH:MM:SS
inline std::wstring time_tToWstring(time_t t, int section = 0) #endif
{
wchar_t wtime[32] = { 0 }; // section:
struct tm tmtm; // 0 for YYYY-mm-dd HH:MM:SS
localtime_s(&tmtm, &t); // 1 for YYYY-mm-dd
if (t == -1) { // 2 for HH:MM:SS
t = time(nullptr); inline std::wstring time_tToWstring(time_t t, int section = 0)
localtime_s(&tmtm, &t); {
} wchar_t wtime[32] = { 0 };
struct tm tmtm;
if (section == 0) { localtime_s(&tmtm, &t);
wcsftime(wtime, 32, L"%Y-%m-%d %H:%M:%S", &tmtm); if (t == -1) {
} else if (section == 1) { t = time(nullptr);
wcsftime(wtime, 32, L"%Y-%m-%d", &tmtm); localtime_s(&tmtm, &t);
} else { }
wcsftime(wtime, 32, L"%H:%M:%S", &tmtm);
} if (section == 0) {
wcsftime(wtime, 32, L"%Y-%m-%d %H:%M:%S", &tmtm);
return std::wstring(wtime); } else if (section == 1) {
} wcsftime(wtime, 32, L"%Y-%m-%d", &tmtm);
} else {
// section: wcsftime(wtime, 32, L"%H:%M:%S", &tmtm);
// 0 for YYYY-mm-dd HH:MM:SS }
// 1 for YYYY-mm-dd
// 2 for HH:MM:SS return std::wstring(wtime);
inline std::string time_tToString(time_t t, int section = 0) }
{
char stime[32] = { 0 }; // section:
struct tm tmtm; // 0 for YYYY-mm-dd HH:MM:SS
localtime_s(&tmtm, &t); // 1 for YYYY-mm-dd
if (t == -1) { // 2 for HH:MM:SS
t = time(nullptr); inline std::string time_tToString(time_t t, int section = 0)
localtime_s(&tmtm, &t); {
} char stime[32] = { 0 };
struct tm tmtm;
if (section == 0) { localtime_s(&tmtm, &t);
strftime(stime, 32, "%Y-%m-%d %H:%M:%S", &tmtm); if (t == -1) {
} else if (section == 1) { t = time(nullptr);
strftime(stime, 32, "%Y-%m-%d", &tmtm); localtime_s(&tmtm, &t);
} else { }
strftime(stime, 32, "%H:%M:%S", &tmtm);
} if (section == 0) {
strftime(stime, 32, "%Y-%m-%d %H:%M:%S", &tmtm);
return std::string(stime); } else if (section == 1) {
} strftime(stime, 32, "%Y-%m-%d", &tmtm);
} else {
inline std::string timePointToString(const std::chrono::system_clock::time_point& tp, bool with_milliseconds = false) strftime(stime, 32, "%H:%M:%S", &tmtm);
{ }
std::stringstream ss;
auto t = std::chrono::system_clock::to_time_t(tp); return std::string(stime);
std::tm* tm = std::localtime(&t); }
ss << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
if (with_milliseconds) { inline std::string timePointToString(const std::chrono::system_clock::time_point& tp, bool with_milliseconds = false)
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()); {
auto millis = ms.count() % 1000; std::stringstream ss;
ss << '.' << std::setw(3) << std::setfill('0') << millis; auto t = std::chrono::system_clock::to_time_t(tp);
} std::tm* tm = std::localtime(&t);
return ss.str(); ss << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
} if (with_milliseconds) {
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
inline std::wstring timePointToWString(const std::chrono::system_clock::time_point& tp, bool with_milliseconds = false) auto millis = ms.count() % 1000;
{ ss << '.' << std::setw(3) << std::setfill('0') << millis;
std::wstringstream ss; }
auto t = std::chrono::system_clock::to_time_t(tp); return ss.str();
std::tm* tm = std::localtime(&t); }
ss << std::put_time(tm, L"%Y-%m-%d %H:%M:%S");
if (with_milliseconds) { inline std::wstring timePointToWString(const std::chrono::system_clock::time_point& tp, bool with_milliseconds = false)
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()); {
auto millis = ms.count() % 1000; std::wstringstream ss;
ss << L'.' << std::setw(3) << std::setfill(L'0') << millis; auto t = std::chrono::system_clock::to_time_t(tp);
} std::tm* tm = std::localtime(&t);
return ss.str(); ss << std::put_time(tm, L"%Y-%m-%d %H:%M:%S");
} if (with_milliseconds) {
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
inline std::chrono::system_clock::time_point timePointFromString(const std::string& s) auto millis = ms.count() % 1000;
{ ss << L'.' << std::setw(3) << std::setfill(L'0') << millis;
std::tm tm = { 0 }; }
std::istringstream ss(s); return ss.str();
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); }
return std::chrono::system_clock::from_time_t(std::mktime(&tm));
} inline std::chrono::system_clock::time_point timePointFromString(const std::string& s)
{
inline std::chrono::system_clock::time_point timePointFromWString(const std::wstring& s) std::tm tm = { 0 };
{ std::istringstream ss(s);
std::tm tm = { 0 }; ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
std::wistringstream ss(s); return std::chrono::system_clock::from_time_t(std::mktime(&tm));
ss >> std::get_time(&tm, L"%Y-%m-%d %H:%M:%S"); }
return std::chrono::system_clock::from_time_t(std::mktime(&tm));
} inline std::chrono::system_clock::time_point timePointFromWString(const std::wstring& s)
{
inline std::string nowToString(bool with_milliseconds = false) std::tm tm = { 0 };
{ std::wistringstream ss(s);
return timePointToString(std::chrono::system_clock::now(), with_milliseconds); ss >> std::get_time(&tm, L"%Y-%m-%d %H:%M:%S");
} return std::chrono::system_clock::from_time_t(std::mktime(&tm));
}
inline std::wstring nowToWString(bool with_milliseconds = false)
{ inline std::string nowToString(bool with_milliseconds = false)
return timePointToWString(std::chrono::system_clock::now(), with_milliseconds); {
} return timePointToString(std::chrono::system_clock::now(), with_milliseconds);
}
};
inline std::wstring nowToWString(bool with_milliseconds = false)
{
return timePointToWString(std::chrono::system_clock::now(), with_milliseconds);
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment