blob: a3711bbebb39d86842d03b909378b02a21c2a3ff [file] [log] [blame]
Jordan Baylesc9201dd2020-06-02 15:51:31 -07001// 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.
13namespace openscreen {
14
15using hours = std::chrono::hours;
16using microseconds = std::chrono::microseconds;
17using milliseconds = std::chrono::milliseconds;
18using nanoseconds = std::chrono::nanoseconds;
19using 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.
23template <typename D>
24static constexpr hours to_hours(D d) {
25 return std::chrono::duration_cast<hours>(d);
26}
27
28template <typename D>
29static constexpr microseconds to_microseconds(D d) {
30 return std::chrono::duration_cast<microseconds>(d);
31}
32
33template <typename D>
34static constexpr milliseconds to_milliseconds(D d) {
35 return std::chrono::duration_cast<milliseconds>(d);
36}
37
38template <typename D>
39static constexpr nanoseconds to_nanoseconds(D d) {
40 return std::chrono::duration_cast<nanoseconds>(d);
41}
42
43template <typename D>
44static 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_