timestamp_to_str rewritten to generate UTC time strings

master
moneroexamples 8 years ago
parent a2c3c575ba
commit 6054c3e691

@ -139,4 +139,5 @@ target_link_libraries(${PROJECT_NAME}
pthread pthread
unbound unbound
unwind unwind
curl
dl) dl)

@ -2208,27 +2208,28 @@ namespace xmreg {
{ {
uint64_t estimated_blk_height {0}; uint64_t estimated_blk_height {0};
// first parse the string date into boost's ptime object // first parse the string date sys_seconds and then to timestamp
dateparser parser {"%Y-%m-%d %H:%M:%S"}; // since epoch
uint64_t blk_timestamp_utc = parse(search_text).time_since_epoch().count();
if (parser(search_text)) if (blk_timestamp_utc)
{ {
// seems we have a correct date! // seems we have a correct date!
// so try to estimate block height from it. // so try to estimate block height from it.
cout << "timestamp: " << xmreg::ptime_to_time_t(parser.pt) << endl; cout << "timestamp: " << blk_timestamp_utc << endl;
// estimate blockchain height from the start date provided // // estimate blockchain height from the start date provided
estimated_blk_height = xmreg::estimate_bc_height(search_text); // estimated_blk_height = xmreg::estimate_bc_height(search_text);
//
result_html = show_block(estimated_blk_height); // result_html = show_block(estimated_blk_height);
//
// nasty check if output is "Cant get" as a sign of // // nasty check if output is "Cant get" as a sign of
// a not found tx. Later need to think of something better. // // a not found tx. Later need to think of something better.
if (result_html.find("Cant get") == string::npos) // if (result_html.find("Cant get") == string::npos)
{ // {
return result_html; // return result_html;
} // }
} }
} }

@ -143,23 +143,14 @@ namespace xmreg
return bf::path(remove_trailing_path_separator(path_str)); return bf::path(remove_trailing_path_separator(path_str));
} }
string string
timestamp_to_str(time_t timestamp, const char* format) timestamp_to_str(time_t timestamp, const char* format)
{ {
auto a_time_point = chrono::system_clock::from_time_t(timestamp);
auto utc = date::to_utc_time(chrono::system_clock::from_time_t(timestamp));
auto sys_time = date::to_sys_time(utc);
const int TIME_LENGTH = 60; return date::format(format, date::floor<chrono::seconds>(sys_time));
char str_buff[TIME_LENGTH];
tm *tm_ptr;
tm_ptr = localtime(&timestamp);
size_t len;
len = std::strftime(str_buff, TIME_LENGTH, format, tm_ptr);
return string(str_buff, len);
} }
@ -890,5 +881,21 @@ namespace xmreg
return null_pkey; return null_pkey;
} }
date::sys_seconds
parse(const std::string& str, string format)
{
std::istringstream in(str);
date::sys_seconds tp;
in >> date::parse(format, tp);
if (in.fail())
{
in.clear();
in.str(str);
in >> date::parse(format, tp);
}
return tp;
}
} }

@ -253,6 +253,9 @@ namespace xmreg
public_key public_key
get_tx_pub_key_from_received_outs(const transaction &tx); get_tx_pub_key_from_received_outs(const transaction &tx);
date::sys_seconds
parse(const std::string& str, string format="%Y-%m-%d %H:%M:%S");
} }
#endif //XMREG01_TOOLS_H #endif //XMREG01_TOOLS_H

Loading…
Cancel
Save