New utility functions for formatting time intervals.
BUG=none
TEST=Builds and runs unit tests
Change-Id: I6f14f1671f83e488f65355bb35884fa5aa816f9a
Reviewed-on: https://gerrit.chromium.org/gerrit/22403
Reviewed-by: Don Garrett <[email protected]>
Commit-Ready: Gilad Arnold <[email protected]>
Tested-by: Gilad Arnold <[email protected]>
Reviewed-by: Jay Srinivasan <[email protected]>
diff --git a/update_check_scheduler.cc b/update_check_scheduler.cc
index 4d088ab..cbe90c4 100644
--- a/update_check_scheduler.cc
+++ b/update_check_scheduler.cc
@@ -80,7 +80,7 @@
}
GTimeoutAddSeconds(interval, StaticCheck);
scheduled_ = true;
- LOG(INFO) << "Next update check in " << utils::SecsToHourMinSecStr(interval);
+ LOG(INFO) << "Next update check in " << utils::FormatSecs(interval);
}
gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) {
diff --git a/utils.cc b/utils.cc
index 3232061..1cdcc5c 100644
--- a/utils.cc
+++ b/utils.cc
@@ -589,20 +589,40 @@
return FALSE;
}
-string SecsToHourMinSecStr(unsigned secs) {
- // Canonicalize into hours, minutes, seconds.
- unsigned hours = secs / (60 * 60);
- secs -= hours * (60 * 60);
- unsigned mins = secs / 60;
- secs -= mins * 60;
+string FormatSecs(unsigned secs) {
+ return FormatTimeDelta(base::TimeDelta::FromSeconds(secs));
+}
+
+string FormatTimeDelta(base::TimeDelta delta) {
+ // Canonicalize into days, hours, minutes, seconds and microseconds.
+ unsigned days = delta.InDays();
+ delta -= base::TimeDelta::FromDays(days);
+ unsigned hours = delta.InHours();
+ delta -= base::TimeDelta::FromHours(hours);
+ unsigned mins = delta.InMinutes();
+ delta -= base::TimeDelta::FromMinutes(mins);
+ unsigned secs = delta.InSeconds();
+ delta -= base::TimeDelta::FromSeconds(secs);
+ unsigned usecs = delta.InMicroseconds();
// Construct and return string.
string str;
- if (hours)
+ if (days)
+ base::StringAppendF(&str, "%ud", days);
+ if (days || hours)
base::StringAppendF(&str, "%uh", hours);
- if (hours || mins)
+ if (days || hours || mins)
base::StringAppendF(&str, "%um", mins);
- base::StringAppendF(&str, "%us", secs);
+ base::StringAppendF(&str, "%u", secs);
+ if (usecs) {
+ int width = 6;
+ while ((usecs / 10) * 10 == usecs) {
+ usecs /= 10;
+ width--;
+ }
+ base::StringAppendF(&str, ".%0*u", width, usecs);
+ }
+ base::StringAppendF(&str, "s");
return str;
}
diff --git a/utils.h b/utils.h
index 3c83674..7d64ee2 100644
--- a/utils.h
+++ b/utils.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -13,6 +13,7 @@
#include <vector>
#include <base/eintr_wrapper.h>
+#include <base/time.h>
#include <ext2fs/ext2fs.h>
#include <glib.h>
@@ -251,10 +252,19 @@
// Assumes data points to a Closure. Runs it and returns FALSE;
gboolean GlibRunClosure(gpointer data);
-// Converts seconds into hour-minute-second notation. For example, 185 will
-// yield 3m5s and 4300 will yield 1h11m40s. Zero padding not applied. Seconds
-// are always shown in the result.
-std::string SecsToHourMinSecStr(unsigned secs);
+// Converts seconds into human readable notation including days, hours, minutes
+// and seconds. For example, 185 will yield 3m5s, 4300 will yield 1h11m40s, and
+// 360000 will yield 4d4h0m0s. Zero padding not applied. Seconds are always
+// shown in the result.
+std::string FormatSecs(unsigned secs);
+
+// Converts a TimeDelta into human readable notation including days, hours,
+// minutes, seconds and fractions of a second down to microsecond granularity,
+// as necessary; for example, an output of 5d2h0m15.053s means that the input
+// time was precise to the milliseconds only. Zero padding not applied, except
+// for fractions. Seconds are always shown, but fractions thereof are only shown
+// when applicable.
+std::string FormatTimeDelta(base::TimeDelta delta);
} // namespace utils