Skip to content

Commit

Permalink
make format_duration more natural
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzziqersoftware committed Nov 3, 2024
1 parent 4aaecf1 commit e33d770
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ string format_time_natural(struct timeval* tv) {
string format_duration(uint64_t usecs, int8_t subsecond_precision) {
if (usecs < 1000000ULL) {
if (subsecond_precision < 0) {
subsecond_precision = 5;
subsecond_precision = 6;
}
return string_printf("%.*lf", subsecond_precision,
static_cast<double>(usecs) / 1000000ULL);

} else if (usecs < 60 * 1000000ULL) {
if (subsecond_precision < 0) {
subsecond_precision = (usecs < 10 * 1000000ULL) ? 5 : 4;
subsecond_precision = 6;
}
return string_printf("%.*lf", subsecond_precision,
static_cast<double>(usecs) / 1000000ULL);

} else if (usecs < 60 * 60 * 1000000ULL) {
if (subsecond_precision < 0) {
subsecond_precision = (usecs < 10 * 60 * 1000000ULL) ? 2 : 1;
subsecond_precision = 3;
}
uint64_t minutes = usecs / (60 * 1000000ULL);
uint64_t usecs_part = usecs - (minutes * 60 * 1000000ULL);
Expand Down
22 changes: 12 additions & 10 deletions src/TimeTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ int main(int, char**) {
expect_ge(now(), start_time + 1000000);

fprintf(stderr, "-- format_time\n");
expect_eq(format_time(static_cast<uint64_t>(0)), "1970-01-01 00:00:00");
expect_eq(format_time(1478915749908529), "2016-11-12 01:55:49");
expect_eq(format_time(static_cast<uint64_t>(0)), "1970-01-01 00:00:00.000000");
expect_eq(format_time(1478915749908529), "2016-11-12 01:55:49.908529");
expect_eq(format_time(1478915749000529), "2016-11-12 01:55:49.000529");
expect_eq(format_time(1478915749908000), "2016-11-12 01:55:49.908000");

{
fprintf(stderr, "-- format_duration\n");
expect_eq("0.00000", format_duration(0));
expect_eq("0.22222", format_duration(222222));
expect_eq("2.22222", format_duration(2222222));
expect_eq("12.2222", format_duration(12222222));
expect_eq("1:02.22", format_duration(62222222));
expect_eq("1:10.00", format_duration(69999999));
expect_eq("1:12.22", format_duration(72222222));
expect_eq("11:12.2", format_duration(672222222));
expect_eq("0.000000", format_duration(0));
expect_eq("0.222222", format_duration(222222));
expect_eq("2.222222", format_duration(2222222));
expect_eq("12.222222", format_duration(12222222));
expect_eq("1:02.222", format_duration(62222222));
expect_eq("1:10.000", format_duration(69999999));
expect_eq("1:12.222", format_duration(72222222));
expect_eq("11:12.222", format_duration(672222222));
expect_eq("1:01:12", format_duration(3672222222));
expect_eq("1:11:12", format_duration(4272222222));
expect_eq("11:11:12", format_duration(40272222222));
Expand Down

0 comments on commit e33d770

Please sign in to comment.