• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 #ifndef UTIL_CHRONO_HELPERS_H_
6 #define UTIL_CHRONO_HELPERS_H_
7 
8 #include <chrono>
9 
10 // This file is a collection of helpful utilities and using statement for
11 // working with std::chrono. In practice we previously defined these frequently,
12 // this header allows for a single set of convenience statements.
13 namespace openscreen {
14 
15 using hours = std::chrono::hours;
16 using microseconds = std::chrono::microseconds;
17 using milliseconds = std::chrono::milliseconds;
18 using nanoseconds = std::chrono::nanoseconds;
19 using seconds = std::chrono::seconds;
20 
21 // Casting statements. Note that duration_cast is not a type, it's a function,
22 // so its behavior is different than the using statements above.
23 template <typename D>
to_hours(D d)24 static constexpr hours to_hours(D d) {
25   return std::chrono::duration_cast<hours>(d);
26 }
27 
28 template <typename D>
to_microseconds(D d)29 static constexpr microseconds to_microseconds(D d) {
30   return std::chrono::duration_cast<microseconds>(d);
31 }
32 
33 template <typename D>
to_milliseconds(D d)34 static constexpr milliseconds to_milliseconds(D d) {
35   return std::chrono::duration_cast<milliseconds>(d);
36 }
37 
38 template <typename D>
to_nanoseconds(D d)39 static constexpr nanoseconds to_nanoseconds(D d) {
40   return std::chrono::duration_cast<nanoseconds>(d);
41 }
42 
43 template <typename D>
to_seconds(D d)44 static constexpr seconds to_seconds(D d) {
45   return std::chrono::duration_cast<seconds>(d);
46 }
47 
48 }  // namespace openscreen
49 
50 #endif  // UTIL_CHRONO_HELPERS_H_
51