From bde3fba16f8ae989e830980c624164468df0f491 Mon Sep 17 00:00:00 2001
From: Mark Schott <schottm@google.com>
Date: Tue, 23 Apr 2019 19:42:43 +0000
Subject: [PATCH] [libzx] make operator+ fully commutative for zx::duration

There was an edge case where adding a duration to a time was valid, but
not the other way around.

Change-Id: Ib8273142d3ab66b68077a18150c3ccd62a4b0669
---
 zircon/system/ulib/zx/include/lib/zx/time.h | 5 +++++
 zircon/system/utest/libzx/zx-test.cpp       | 1 +
 2 files changed, 6 insertions(+)

diff --git a/zircon/system/ulib/zx/include/lib/zx/time.h b/zircon/system/ulib/zx/include/lib/zx/time.h
index 7b7e25cda1f..fdfcb6f3484 100644
--- a/zircon/system/ulib/zx/include/lib/zx/time.h
+++ b/zircon/system/ulib/zx/include/lib/zx/time.h
@@ -234,6 +234,11 @@ private:
     zx_time_t value_ = 0;
 };
 
+template <zx_clock_t kClockId>
+constexpr basic_time<kClockId> operator+(duration delta, basic_time<kClockId> time) {
+    return time + delta;
+}
+
 using time = basic_time<ZX_CLOCK_MONOTONIC>;
 using time_utc = basic_time<ZX_CLOCK_UTC>;
 using time_thread = basic_time<ZX_CLOCK_THREAD>;
diff --git a/zircon/system/utest/libzx/zx-test.cpp b/zircon/system/utest/libzx/zx-test.cpp
index 54678575748..11cf79415ae 100644
--- a/zircon/system/utest/libzx/zx-test.cpp
+++ b/zircon/system/utest/libzx/zx-test.cpp
@@ -285,6 +285,7 @@ static bool time_test() {
     ASSERT_EQ(zx::hour(10).to_hours(), 10);
 
     ASSERT_EQ((zx::time() + zx::usec(19)).get(), ZX_USEC(19));
+    ASSERT_EQ((zx::usec(19) + zx::time()).get(), ZX_USEC(19));
     ASSERT_EQ((zx::time::infinite() - zx::time()).get(), ZX_TIME_INFINITE);
     ASSERT_EQ((zx::time::infinite() - zx::time::infinite()).get(), 0);
     ASSERT_EQ((zx::time() + zx::duration::infinite()).get(), ZX_TIME_INFINITE);
-- 
GitLab