30 lines
900 B
C++
30 lines
900 B
C++
#include "TimeStamp.h"
|
|
#include "lib/utils/datetime.h"
|
|
#include <boost/date_time/posix_time/time_formatters.hpp>
|
|
|
|
namespace bts::core {
|
|
|
|
TimeStamp TimeStamp::from_iso_string(const std::string &iso_string) {
|
|
auto ptime = datetime::parse_iso_date_time_string(iso_string);
|
|
return from_ptime(ptime);
|
|
}
|
|
|
|
std::string TimeStamp::to_iso_string() const {
|
|
return boost::posix_time::to_iso_string(to_ptime());
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &out, const TimeStamp &ts) noexcept {
|
|
out << ts.to_iso_string();
|
|
return out;
|
|
}
|
|
|
|
TimeStamp TimeStamp::from_ptime(boost::posix_time::ptime t) {
|
|
// time_t is Unix time: number of seconds since epoch Jan 1, 1970
|
|
auto epoch_pt =
|
|
t - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1));
|
|
auto epoch_nanos = epoch_pt.total_nanoseconds();
|
|
return TimeStamp{static_cast<std::uint64_t>(epoch_nanos)};
|
|
}
|
|
|
|
} // namespace bts::core
|