From fe8d33b9860807247b31250575de61c9e6411ee8 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Mon, 9 Jan 2017 03:44:08 +0000 Subject: [PATCH] get_human_readable_timestamp simplified --- src/tools.cpp | 62 ++++++++++++++++++++++++++++++++++++--------------- src/tools.h | 4 ++++ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/tools.cpp b/src/tools.cpp index 078541f..0e82eca 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -143,29 +143,34 @@ remove_trailing_path_separator(const bf::path& in_path) return bf::path(remove_trailing_path_separator(path_str)); } +//string +//timestamp_to_str(time_t timestamp, const char* format) +//{ +// auto a_time_point = chrono::system_clock::from_time_t(timestamp); +// +// try +// { +// auto utc = date::to_utc_time(chrono::system_clock::from_time_t(timestamp)); +// auto sys_time = date::to_sys_time(utc); +// +// return date::format(format, date::floor(sys_time)); +// } +// catch (std::runtime_error& e) +// { +// cerr << "xmreg::timestamp_to_str: " << e.what() << endl; +// cerr << "Seems cant convert to UTC timezone using date library. " +// "So just use local timezone." <(sys_time)); - } - catch (std::runtime_error& e) - { - cerr << "xmreg::timestamp_to_str: " << e.what() << endl; - cerr << "Seems cant convert to UTC timezone using date libary. " - "So just use local timezone." <"; + + time_t tt = ts; + + struct tm tm; + + gmtime_r(&tt, &tm); + + strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M:%S", &tm); + + return std::string(buffer); +} + + } diff --git a/src/tools.h b/src/tools.h index e165e64..952d5b2 100644 --- a/src/tools.h +++ b/src/tools.h @@ -299,6 +299,10 @@ make_tx_from_json(const string& json_str, transaction& tx); string make_printable(const string& in_s); + +string +get_human_readable_timestamp(uint64_t ts); + } #endif //XMREG01_TOOLS_H