Skip to content

Commit f5fd8f3

Browse files
authored
Merge pull request #1320 from joto/human-readable-durations
Output durations in more human readable form
2 parents 96b95a0 + 860b13a commit f5fd8f3

10 files changed

Lines changed: 52 additions & 12 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(osm2pgsql_lib_SOURCES
3333
taginfo.cpp
3434
tagtransform-c.cpp
3535
tagtransform.cpp
36+
util.cpp
3637
wildcmp.cpp
3738
)
3839

src/flex-table.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ void table_connection_t::stop(bool updateable, bool append)
276276
fmt::print(stderr, "Analyzing table '{}'...\n", table().name());
277277
m_db_connection->exec("ANALYZE " + table().full_name());
278278

279-
fmt::print(stderr, "All postprocessing on table '{}' done in {}s.\n",
280-
table().name(), timer.stop());
279+
fmt::print(stderr, "All postprocessing on table '{}' done in {}.\n",
280+
table().name(), util::human_readable_duration(timer.stop()));
281281

282282
teardown();
283283
}

src/middle-pgsql.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ void middle_pgsql_t::table_desc::stop(std::string const &conninfo,
103103
sql_conn.exec(m_create_fw_dep_indexes);
104104
}
105105

106-
fmt::print(stderr, "Stopped table: {} in {}s\n", name(), timer.stop());
106+
fmt::print(stderr, "Stopped table: {} in {}\n", name(),
107+
util::human_readable_duration(timer.stop()));
107108
}
108109

109110
namespace {

src/osm2pgsql.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ int main(int argc, char *argv[])
9797
progress.update(osmdata.process_file(file, options.bbox));
9898

9999
progress.print_status(std::time(nullptr));
100-
fmt::print(stderr, " parse time: {}s\n", timer_parse.stop());
100+
fmt::print(stderr, " parse time: {}\n",
101+
util::human_readable_duration(timer_parse.stop()));
101102
}
102103

103104
progress.print_summary();
@@ -106,8 +107,8 @@ int main(int argc, char *argv[])
106107
// create indexes.
107108
osmdata.stop();
108109

109-
fmt::print(stderr, "\nOsm2pgsql took {}s overall\n",
110-
timer_overall.stop());
110+
fmt::print(stderr, "\nOsm2pgsql took {} overall\n",
111+
util::human_readable_duration(timer_overall.stop()));
111112
} catch (std::exception const &e) {
112113
fmt::print(stderr, "Osm2pgsql failed due to ERROR: {}\n", e.what());
113114
return 1;

src/osmdata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ class multithreaded_processor
337337

338338
timer.stop();
339339

340-
fmt::print(stderr, "\rFinished processing {} {}s in {} s\n\n",
341-
ids_queued, type, timer.elapsed());
340+
fmt::print(stderr, "\rFinished processing {} {}s in {}\n\n", ids_queued,
341+
type, util::human_readable_duration(timer.elapsed()));
342342

343343
if (timer.elapsed() > 0) {
344344
fmt::print(stderr,

src/output-flex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,8 +1513,8 @@ void output_flex_t::reprocess_marked()
15131513
}
15141514
}
15151515

1516-
fmt::print(stderr, " Creating id indexes took {} seconds\n"_format(
1517-
timer.stop()));
1516+
fmt::print(stderr, " Creating id indexes took {}\n"_format(
1517+
util::human_readable_duration(timer.stop())));
15181518
}
15191519

15201520
lua_gc(lua_state(), LUA_GCCOLLECT, 0);

src/table.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
272272
}
273273
fmt::print(stderr, "Creating indexes on {} finished\n", m_target->name);
274274
m_sql_conn->exec("ANALYZE {}"_format(qual_name));
275-
fmt::print(stderr, "All indexes on {} created in {}s\n", m_target->name,
276-
timer.stop());
275+
fmt::print(stderr, "All indexes on {} created in {}\n", m_target->name,
276+
util::human_readable_duration(timer.stop()));
277277
}
278278
teardown();
279279

src/util.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include "util.hpp"
3+
4+
namespace util {
5+
6+
std::string human_readable_duration(uint64_t seconds)
7+
{
8+
if (seconds < 60) {
9+
return "{}s"_format(seconds);
10+
} else if (seconds < (60 * 60)) {
11+
return "{}s ({}m {}s)"_format(seconds, seconds / 60, seconds % 60);
12+
}
13+
14+
auto const secs = seconds % 60;
15+
auto const mins = seconds / 60;
16+
return "{}s ({}h {}m {}s)"_format(seconds, mins / 60, mins % 60, secs);
17+
}
18+
19+
} // namespace util

src/util.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cstdint>
99
#include <cstdlib>
1010
#include <ctime>
11+
#include <string>
1112

1213
namespace util {
1314

@@ -90,6 +91,8 @@ class timer_t
9091

9192
}; // class timer_t
9293

94+
std::string human_readable_duration(uint64_t seconds);
95+
9396
} // namespace util
9497

9598
#endif // OSM2PGSQL_UTIL_HPP

tests/test-util.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,18 @@ TEST_CASE("double_to_buffer 3.141")
3434
util::double_to_buffer buffer{3.141};
3535
REQUIRE(std::strcmp(buffer.c_str(), "3.141") == 0);
3636
}
37+
38+
TEST_CASE("human readable time durations")
39+
{
40+
REQUIRE(util::human_readable_duration(0) == "0s");
41+
REQUIRE(util::human_readable_duration(17) == "17s");
42+
REQUIRE(util::human_readable_duration(59) == "59s");
43+
REQUIRE(util::human_readable_duration(60) == "60s (1m 0s)");
44+
REQUIRE(util::human_readable_duration(66) == "66s (1m 6s)");
45+
REQUIRE(util::human_readable_duration(247) == "247s (4m 7s)");
46+
REQUIRE(util::human_readable_duration(3599) == "3599s (59m 59s)");
47+
REQUIRE(util::human_readable_duration(3600) == "3600s (1h 0m 0s)");
48+
REQUIRE(util::human_readable_duration(3723) == "3723s (1h 2m 3s)");
49+
REQUIRE(util::human_readable_duration(152592) == "152592s (42h 23m 12s)");
50+
}
51+

0 commit comments

Comments
 (0)