Joedb 10.4.6
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
get_time_string.cpp
Go to the documentation of this file.
2
3#include <ctime>
4
5namespace joedb
6{
7 /////////////////////////////////////////////////////////////////////////////
8 std::string get_time_string(int64_t timestamp)
9 /////////////////////////////////////////////////////////////////////////////
10 {
11 const time_t stamp = time_t(timestamp);
12 struct tm tm;
13
14#ifdef JOEDB_PORTABLE
15 const auto tm_ptr = std::gmtime(&stamp);
16 if (tm_ptr)
17 tm = *tm_ptr;
18 else
19#elif defined(_WIN32)
20 if (gmtime_s(&tm, &stamp) != 0)
21#else
22 if (gmtime_r(&stamp, &tm) == nullptr)
23#endif
24 return "bad timestamp";
25
26 constexpr size_t buffer_size = 64;
27 std::string result(buffer_size, '\0');
28
29 const size_t size = std::strftime
30 (
31 result.data(),
32 result.size(),
33 "%Y-%m-%d %H:%M:%S GMT",
34 &tm
35 );
36
37 result.resize(size);
38 return result;
39 }
40
41 /////////////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////////////
44 {
45 return get_time_string(std::time(nullptr));
46 }
47}
std::string get_time_string(int64_t timestamp)
std::string get_time_string_of_now()