summaryrefslogtreecommitdiff
path: root/libartbase/base/time_utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'libartbase/base/time_utils.h')
-rw-r--r--libartbase/base/time_utils.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/libartbase/base/time_utils.h b/libartbase/base/time_utils.h
index dd73b1c951..ddabb1289f 100644
--- a/libartbase/base/time_utils.h
+++ b/libartbase/base/time_utils.h
@@ -26,6 +26,8 @@
#include <cstdint>
#include <string>
+#include "android-base/logging.h"
+
namespace art {
enum TimeUnit {
@@ -123,6 +125,13 @@ void NanoSleep(uint64_t ns);
// time corresponding to the indicated clock value plus the supplied offset.
void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts);
+// Converts `timespec` to nanoseconds. The return value can be negative, which should be interpreted
+// as a time before the epoch.
+static constexpr int64_t TimeSpecToNs(timespec ts) {
+ DCHECK_GE(ts.tv_nsec, 0); // According to POSIX.
+ return static_cast<int64_t>(ts.tv_sec) * INT64_C(1000000000) + ts.tv_nsec;
+}
+
} // namespace art
#endif // ART_LIBARTBASE_BASE_TIME_UTILS_H_