summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-12-06 03:46:53 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2019-12-09 20:43:37 +0000
commit3a8c63f34d8554573b42b0c3749e44ad4f43fb0e (patch)
tree1cf8d395efbf6c29255a1ca38e80912f4a51c761 /vespalib
parent676dd43a12db59f96536aa6d8a45369d24d17404 (diff)
Use std::chrono.
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/time/time_test.cpp10
-rw-r--r--vespalib/src/vespa/vespalib/util/time.h4
2 files changed, 14 insertions, 0 deletions
diff --git a/vespalib/src/tests/time/time_test.cpp b/vespalib/src/tests/time/time_test.cpp
index 667511f6a94..40542b6ca62 100644
--- a/vespalib/src/tests/time/time_test.cpp
+++ b/vespalib/src/tests/time/time_test.cpp
@@ -29,6 +29,16 @@ TEST(TimeTest, double_conversion_works_as_expected) {
EXPECT_EQ(10ms, from_s(0.010));
}
+TEST(TimeTest, timeval_conversion_works_as_expected) {
+ timeval tv1;
+ tv1.tv_sec = 7;
+ tv1.tv_usec = 342356;
+ EXPECT_EQ(from_timeval(tv1), 7342356us);
+ tv1.tv_sec = 7;
+ tv1.tv_usec = 1342356;
+ EXPECT_EQ(from_timeval(tv1), 8342356us);
+}
+
TEST(TimeTest, unit_counting_works_as_expected) {
auto d = 3ms + 5us + 7ns;
EXPECT_EQ(count_ns(d), 3005007);
diff --git a/vespalib/src/vespa/vespalib/util/time.h b/vespalib/src/vespa/vespalib/util/time.h
index 2f8ae0ae016..a3390114bb1 100644
--- a/vespalib/src/vespa/vespalib/util/time.h
+++ b/vespalib/src/vespa/vespalib/util/time.h
@@ -55,6 +55,10 @@ constexpr int64_t count_ns(duration d) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(d).count();
}
+constexpr duration from_timeval(const timeval & tv) {
+ return duration(tv.tv_sec*1000000000L + tv.tv_usec*1000L);
+}
+
/**
* Simple utility class used to measure how much time has elapsed
* since it was constructed.