diff options
Diffstat (limited to 'mojo/common/time_struct_traits.h')
-rw-r--r-- | mojo/common/time_struct_traits.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/mojo/common/time_struct_traits.h b/mojo/common/time_struct_traits.h new file mode 100644 index 0000000..b480edb --- /dev/null +++ b/mojo/common/time_struct_traits.h @@ -0,0 +1,55 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_COMMON_TIME_STRUCT_TRAITS_H_ +#define MOJO_COMMON_TIME_STRUCT_TRAITS_H_ + +#include "base/time/time.h" +#include "mojo/common/time.mojom-shared.h" + +namespace mojo { + +template <> +struct StructTraits<common::mojom::TimeDataView, base::Time> { + static int64_t internal_value(const base::Time& time) { + return time.since_origin().InMicroseconds(); + } + + static bool Read(common::mojom::TimeDataView data, base::Time* time) { + *time = + base::Time() + base::TimeDelta::FromMicroseconds(data.internal_value()); + return true; + } +}; + +template <> +struct StructTraits<common::mojom::TimeDeltaDataView, base::TimeDelta> { + static int64_t microseconds(const base::TimeDelta& delta) { + return delta.InMicroseconds(); + } + + static bool Read(common::mojom::TimeDeltaDataView data, + base::TimeDelta* delta) { + *delta = base::TimeDelta::FromMicroseconds(data.microseconds()); + return true; + } +}; + +template <> +struct StructTraits<common::mojom::TimeTicksDataView, base::TimeTicks> { + static int64_t internal_value(const base::TimeTicks& time) { + return time.since_origin().InMicroseconds(); + } + + static bool Read(common::mojom::TimeTicksDataView data, + base::TimeTicks* time) { + *time = base::TimeTicks() + + base::TimeDelta::FromMicroseconds(data.internal_value()); + return true; + } +}; + +} // namespace mojo + +#endif // MOJO_COMMON_TIME_STRUCT_TRAITS_H_ |