-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdatetime.cpp
64 lines (57 loc) · 2.03 KB
/
datetime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "databento/datetime.hpp"
// NOLINTNEXTLINE(modernize-deprecated-headers): no thread-safe version in STL
#include <time.h> // gmtime_r or gmtime_s
#include <array>
#include <chrono>
#include <ctime> // localtime, strftime, tm
#include <iomanip> // setw
#include <sstream> // ostringstream
#include "databento/constants.hpp" // kUndefTimestamp
namespace databento {
std::string ToIso8601(UnixNanos unix_nanos) {
if (unix_nanos.time_since_epoch().count() == kUndefTimestamp) {
return "UNDEF_TIMESTAMP";
}
std::array<char, 80> buf{};
const std::time_t time = std::chrono::duration_cast<std::chrono::seconds>(
unix_nanos.time_since_epoch())
.count();
std::tm tm = {};
#ifdef _WIN32
if (::gmtime_s(&tm, &time) != 0) {
// Fallback on printing nanos
return ToString(unix_nanos);
}
#else
if (::gmtime_r(&time, &tm) == nullptr) {
// Fallback on printing nanos
return ToString(unix_nanos);
}
#endif
const auto nanos = std::chrono::nanoseconds{
unix_nanos.time_since_epoch() %
std::chrono::nanoseconds{std::chrono::seconds{1}}};
const size_t count =
std::strftime(buf.data(), sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tm);
std::ostringstream time_ss;
time_ss.write(buf.data(), static_cast<std::streamsize>(count));
time_ss << '.' << std::setw(9) << std::setfill('0') << nanos.count() << 'Z';
return time_ss.str();
}
std::string ToString(UnixNanos unix_nanos) {
return std::to_string(unix_nanos.time_since_epoch().count());
}
std::string ToString(TimeDeltaNanos td_nanos) {
return std::to_string(td_nanos.count());
}
std::string DateFromIso8601Int(std::uint32_t date_int) {
const auto year = date_int / 10000;
const auto remaining = date_int % 10000;
const auto month = remaining / 100;
const auto day = remaining % 100;
std::ostringstream out_ss;
out_ss << year << '-' << std::setfill('0') << std::setw(2) << month << '-'
<< std::setfill('0') << std::setw(2) << day;
return out_ss.str();
}
} // namespace databento