2018-07-23 05:54:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-25 22:14:10 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <string>
|
|
|
|
|
2018-07-23 05:54:53 +00:00
|
|
|
namespace date_time
|
|
|
|
{
|
|
|
|
static inline tm get_time(time_t* _time)
|
|
|
|
{
|
|
|
|
tm buf;
|
|
|
|
time_t t = time(_time);
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
localtime_s(&buf, &t);
|
|
|
|
#else
|
|
|
|
buf = *localtime(&t);
|
|
|
|
#endif
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:46:16 +00:00
|
|
|
static inline std::string fmt_time(const char* fmt, const s64 time)
|
|
|
|
{
|
|
|
|
tm buf;
|
|
|
|
time_t t = time;
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
localtime_s(&buf, &t);
|
|
|
|
#else
|
|
|
|
buf = *localtime(&t);
|
|
|
|
#endif
|
|
|
|
char str[80];
|
|
|
|
strftime(str, sizeof(str), fmt, &buf);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-07-23 05:54:53 +00:00
|
|
|
static inline std::string current_time()
|
|
|
|
{
|
|
|
|
char str[80];
|
2021-04-09 19:12:47 +00:00
|
|
|
tm now = get_time(nullptr);
|
2018-07-23 05:54:53 +00:00
|
|
|
strftime(str, sizeof(str), "%c", &now);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2019-08-10 16:49:57 +00:00
|
|
|
template<char separator = 0>
|
2018-07-23 05:54:53 +00:00
|
|
|
static inline std::string current_time_narrow()
|
|
|
|
{
|
|
|
|
char str[80];
|
2021-04-09 19:12:47 +00:00
|
|
|
tm now = get_time(nullptr);
|
2019-08-10 16:49:57 +00:00
|
|
|
|
|
|
|
std::string parse_buf;
|
|
|
|
|
|
|
|
if constexpr(separator != 0)
|
|
|
|
parse_buf = std::string("%Y") + separator + "%m" + separator + "%d" + separator + "%H" + separator + "%M" + separator + "%S";
|
|
|
|
else
|
|
|
|
parse_buf = "%Y%m%d%H%M%S";
|
|
|
|
|
|
|
|
strftime(str, sizeof(str), parse_buf.c_str(), &now);
|
2018-07-23 05:54:53 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|