1 // Copyright 2022 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #pragma once
6
7 #include "base/time/time.h"
8
9 // Android's external/libchrome directory is out of date.
10 // Add missing templates here as a temporary solution
11 namespace base {
12
13 /**
14 * Workaround for the error in unit tests: ISO C++20 considers use of overloaded
15 * operator '==' (with operand types 'const base::TimeTicks'
16 * and 'const base::TimeTicks') to be ambiguous despite there being a unique
17 * best viable function [-Werror,-Wambiguous-reversed-operator]
18 */
19 bool operator==(const TimeTicks& t1, const TimeTicks& t2);
20
21 namespace time_internal {
22
23 // clang-format off
24 template <typename T>
25 using EnableIfIntegral = typename std::
26 enable_if<std::is_integral<T>::value || std::is_enum<T>::value, int>::type;
27 template <typename T>
28 using EnableIfFloat =
29 typename std::enable_if<std::is_floating_point<T>::value, int>::type;
30
31 } // namespace time_internal
32
33
34 template <typename T, time_internal::EnableIfIntegral<T> = 0>
Seconds(T n)35 constexpr TimeDelta Seconds(T n) {
36 return TimeDelta::FromInternalValue(
37 ClampMul(static_cast<int64_t>(n), Time::kMicrosecondsPerSecond));
38 }
39 template <typename T, time_internal::EnableIfIntegral<T> = 0>
Milliseconds(T n)40 constexpr TimeDelta Milliseconds(T n) {
41 return TimeDelta::FromInternalValue(
42 ClampMul(static_cast<int64_t>(n), Time::kMicrosecondsPerMillisecond));
43 }
44 template <typename T, time_internal::EnableIfFloat<T> = 0>
Seconds(T n)45 constexpr TimeDelta Seconds(T n) {
46 return TimeDelta::FromInternalValue(
47 saturated_cast<int64_t>(n * Time::kMicrosecondsPerSecond));
48 }
49 template <typename T, time_internal::EnableIfFloat<T> = 0>
Milliseconds(T n)50 constexpr TimeDelta Milliseconds(T n) {
51 return TimeDelta::FromInternalValue(
52 saturated_cast<int64_t>(n * Time::kMicrosecondsPerMillisecond));
53 }
54
55 } // namespace base
56