Skip to content

Commit b2a09ed

Browse files
authored
Human-readable timestamps in explorer (ton-blockchain#776)
1 parent ddd3d44 commit b2a09ed

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

blockchain-explorer/blockchain-explorer-http.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,38 @@
3535
#include "vm/cells/MerkleProof.h"
3636
#include "block/mc-config.h"
3737
#include "ton/ton-shard.h"
38+
#include "td/utils/date.h"
3839

3940
bool local_scripts{false};
4041

42+
static std::string time_to_human(unsigned ts) {
43+
td::StringBuilder sb;
44+
sb << date::format("%F %T",
45+
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>{std::chrono::seconds(ts)})
46+
<< ", ";
47+
auto now = (unsigned)td::Clocks::system();
48+
bool past = now >= ts;
49+
unsigned x = past ? now - ts : ts - now;
50+
if (!past) {
51+
sb << "in ";
52+
}
53+
if (x < 60) {
54+
sb << x << "s";
55+
} else if (x < 3600) {
56+
sb << x / 60 << "m " << x % 60 << "s";
57+
} else if (x < 3600 * 24) {
58+
x /= 60;
59+
sb << x / 60 << "h " << x % 60 << "m";
60+
} else {
61+
x /= 3600;
62+
sb << x / 24 << "d " << x % 24 << "h";
63+
}
64+
if (past) {
65+
sb << " ago";
66+
}
67+
return sb.as_cslice().str();
68+
}
69+
4170
HttpAnswer& HttpAnswer::operator<<(AddressCell addr_c) {
4271
ton::WorkchainId wc;
4372
ton::StdSmcAddress addr;
@@ -84,7 +113,7 @@ HttpAnswer& HttpAnswer::operator<<(MessageCell msg) {
84113
<< "<tr><th>source</th><td>" << AddressCell{info.src} << "</td></tr>\n"
85114
<< "<tr><th>destination</th><td>NONE</td></tr>\n"
86115
<< "<tr><th>lt</th><td>" << info.created_lt << "</td></tr>\n"
87-
<< "<tr><th>time</th><td>" << info.created_at << "</td></tr>\n";
116+
<< "<tr><th>time</th><td>" << info.created_at << " (" << time_to_human(info.created_at) << ")</td></tr>\n";
88117
break;
89118
}
90119
case block::gen::CommonMsgInfo::int_msg_info: {
@@ -103,7 +132,7 @@ HttpAnswer& HttpAnswer::operator<<(MessageCell msg) {
103132
<< "<tr><th>source</th><td>" << AddressCell{info.src} << "</td></tr>\n"
104133
<< "<tr><th>destination</th><td>" << AddressCell{info.dest} << "</td></tr>\n"
105134
<< "<tr><th>lt</th><td>" << info.created_lt << "</td></tr>\n"
106-
<< "<tr><th>time</th><td>" << info.created_at << "</td></tr>\n"
135+
<< "<tr><th>time</th><td>" << info.created_at << " (" << time_to_human(info.created_at) << ")</td></tr>\n"
107136
<< "<tr><th>value</th><td>" << value << "</td></tr>\n";
108137
break;
109138
}
@@ -277,7 +306,7 @@ HttpAnswer& HttpAnswer::operator<<(TransactionCell trans_c) {
277306
<< "<tr><th>account</th><td>" << trans_c.addr.rserialize(true) << "</td></tr>"
278307
<< "<tr><th>hash</th><td>" << trans_c.root->get_hash().to_hex() << "</td></tr>\n"
279308
<< "<tr><th>lt</th><td>" << trans.lt << "</td></tr>\n"
280-
<< "<tr><th>time</th><td>" << trans.now << "</td></tr>\n"
309+
<< "<tr><th>time</th><td>" << trans.now << " (" << time_to_human(trans.now) << ")</td></tr>\n"
281310
<< "<tr><th>out messages</th><td>";
282311
vm::Dictionary dict{trans.r1.out_msgs, 15};
283312
for (td::int32 i = 0; i < trans.outmsg_cnt; i++) {
@@ -456,7 +485,7 @@ HttpAnswer& HttpAnswer::operator<<(BlockHeaderCell head_c) {
456485
<< "<tr><th>block</th><td>" << block_id.id.to_str() << "</td></tr>\n"
457486
<< "<tr><th>roothash</th><td>" << block_id.root_hash.to_hex() << "</td></tr>\n"
458487
<< "<tr><th>filehash</th><td>" << block_id.file_hash.to_hex() << "</td></tr>\n"
459-
<< "<tr><th>time</th><td>" << info.gen_utime << "</td></tr>\n"
488+
<< "<tr><th>time</th><td>" << info.gen_utime << " (" << time_to_human(info.gen_utime) << ")</td></tr>\n"
460489
<< "<tr><th>lt</th><td>" << info.start_lt << " .. " << info.end_lt << "</td></tr>\n"
461490
<< "<tr><th>global_id</th><td>" << blk.global_id << "</td></tr>\n"
462491
<< "<tr><th>version</th><td>" << info.version << "</td></tr>\n"
@@ -543,7 +572,8 @@ HttpAnswer& HttpAnswer::operator<<(BlockShardsCell shards_c) {
543572
ton::ShardIdFull shard{id.workchain, id.shard};
544573
if (ref.not_null()) {
545574
*this << "<td>" << shard.to_str() << "</td><td><a href=\"" << HttpAnswer::BlockLink{ref->top_block_id()}
546-
<< "\">" << ref->top_block_id().id.seqno << "</a></td><td>" << ref->created_at() << "</td>"
575+
<< "\">" << ref->top_block_id().id.seqno << "</a></td><td><span title=\""
576+
<< time_to_human(ref->created_at()) << "\">" << ref->created_at() << "</span></td>"
547577
<< "<td>" << ref->want_split_ << "</td>"
548578
<< "<td>" << ref->want_merge_ << "</td>"
549579
<< "<td>" << ref->before_split_ << "</td>"

0 commit comments

Comments
 (0)