/src/opendnp3/cpp/lib/src/util/TimeDuration.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2013-2022 Step Function I/O, LLC |
3 | | * |
4 | | * Licensed to Green Energy Corp (www.greenenergycorp.com) and Step Function I/O |
5 | | * LLC (https://stepfunc.io) under one or more contributor license agreements. |
6 | | * See the NOTICE file distributed with this work for additional information |
7 | | * regarding copyright ownership. Green Energy Corp and Step Function I/O LLC license |
8 | | * this file to you under the Apache License, Version 2.0 (the "License"); you |
9 | | * may not use this file except in compliance with the License. You may obtain |
10 | | * a copy of the License at: |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | #include "opendnp3/util/TimeDuration.h" |
21 | | |
22 | | #include <exe4cpp/Typedefs.h> |
23 | | |
24 | | namespace opendnp3 |
25 | | { |
26 | | |
27 | | TimeDuration TimeDuration::Min() |
28 | 48.0k | { |
29 | 48.0k | return TimeDuration(exe4cpp::duration_t::min()); |
30 | 48.0k | } |
31 | | |
32 | | TimeDuration TimeDuration::Max() |
33 | 32.0k | { |
34 | 32.0k | return TimeDuration(exe4cpp::duration_t::max()); |
35 | 32.0k | } |
36 | | |
37 | | TimeDuration TimeDuration::Zero() |
38 | 2 | { |
39 | 2 | return TimeDuration(std::chrono::milliseconds(0)); |
40 | 2 | } |
41 | | |
42 | | template<class T> TimeDuration TimeDuration::FromValue(int64_t value) |
43 | 42.3k | { |
44 | | // > this will overflow when converting to nanos |
45 | 42.3k | const auto MAX = std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::max()).count(); |
46 | 42.3k | const auto MIN = std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::min()).count(); |
47 | | |
48 | 42.3k | if (value > MAX) |
49 | 0 | { |
50 | 0 | return TimeDuration(std::chrono::steady_clock::duration::max()); |
51 | 0 | } |
52 | | |
53 | 42.3k | if (value < MIN) |
54 | 0 | { |
55 | 0 | return TimeDuration(std::chrono::steady_clock::duration::min()); |
56 | 0 | } |
57 | | |
58 | 42.3k | return TimeDuration(T(value)); |
59 | 42.3k | } Unexecuted instantiation: opendnp3::TimeDuration opendnp3::TimeDuration::FromValue<std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> > >(long) opendnp3::TimeDuration opendnp3::TimeDuration::FromValue<std::__1::chrono::duration<long long, std::__1::ratio<1l, 1l> > >(long) Line | Count | Source | 43 | 34.3k | { | 44 | | // > this will overflow when converting to nanos | 45 | 34.3k | const auto MAX = std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::max()).count(); | 46 | 34.3k | const auto MIN = std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::min()).count(); | 47 | | | 48 | 34.3k | if (value > MAX) | 49 | 0 | { | 50 | 0 | return TimeDuration(std::chrono::steady_clock::duration::max()); | 51 | 0 | } | 52 | | | 53 | 34.3k | if (value < MIN) | 54 | 0 | { | 55 | 0 | return TimeDuration(std::chrono::steady_clock::duration::min()); | 56 | 0 | } | 57 | | | 58 | 34.3k | return TimeDuration(T(value)); | 59 | 34.3k | } |
opendnp3::TimeDuration opendnp3::TimeDuration::FromValue<std::__1::chrono::duration<long, std::__1::ratio<60l, 1l> > >(long) Line | Count | Source | 43 | 8.00k | { | 44 | | // > this will overflow when converting to nanos | 45 | 8.00k | const auto MAX = std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::max()).count(); | 46 | 8.00k | const auto MIN = std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::min()).count(); | 47 | | | 48 | 8.00k | if (value > MAX) | 49 | 0 | { | 50 | 0 | return TimeDuration(std::chrono::steady_clock::duration::max()); | 51 | 0 | } | 52 | | | 53 | 8.00k | if (value < MIN) | 54 | 0 | { | 55 | 0 | return TimeDuration(std::chrono::steady_clock::duration::min()); | 56 | 0 | } | 57 | | | 58 | 8.00k | return TimeDuration(T(value)); | 59 | 8.00k | } |
|
60 | | |
61 | | TimeDuration TimeDuration::Milliseconds(int64_t milliseconds) |
62 | 0 | { |
63 | 0 | return FromValue<std::chrono::milliseconds>(milliseconds); |
64 | 0 | } |
65 | | |
66 | | |
67 | | TimeDuration TimeDuration::Seconds(int64_t seconds) |
68 | 34.3k | { |
69 | 34.3k | return FromValue<std::chrono::seconds>(seconds); |
70 | 34.3k | } |
71 | | |
72 | | TimeDuration TimeDuration::Minutes(int64_t minutes) |
73 | 8.00k | { |
74 | 8.00k | return FromValue<std::chrono::minutes>(minutes); |
75 | 8.00k | } |
76 | | |
77 | | |
78 | 18.3k | TimeDuration::TimeDuration() : value(std::chrono::milliseconds(0)) {} |
79 | | |
80 | | TimeDuration TimeDuration::Double() const |
81 | 0 | { |
82 | 0 | const bool doubling_would_cause_mult_overflow = this->value >= exe4cpp::duration_t::max() / 2; |
83 | |
|
84 | 0 | return doubling_would_cause_mult_overflow ? TimeDuration::Max() : TimeDuration(this->value + this->value); |
85 | 0 | } |
86 | | |
87 | | bool TimeDuration::IsNegative() const |
88 | 2 | { |
89 | 2 | return *this < TimeDuration::Zero(); |
90 | 2 | } |
91 | | |
92 | | std::string TimeDuration::ToString() const |
93 | 0 | { |
94 | 0 | const auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(value).count(); |
95 | |
|
96 | 0 | return std::to_string(millis); |
97 | 0 | } |
98 | | |
99 | | bool TimeDuration::operator==(const TimeDuration& other) const |
100 | 0 | { |
101 | 0 | return this->value == other.value; |
102 | 0 | } |
103 | | |
104 | | bool TimeDuration::operator<(const TimeDuration& other) const |
105 | 2 | { |
106 | 2 | return this->value < other.value; |
107 | 2 | } |
108 | | |
109 | | bool TimeDuration::operator<=(const TimeDuration& other) const |
110 | 0 | { |
111 | 0 | return this->value <= other.value; |
112 | 0 | } |
113 | | |
114 | | bool TimeDuration::operator>(const TimeDuration& other) const |
115 | 0 | { |
116 | 0 | return this->value > other.value; |
117 | 0 | } |
118 | | |
119 | | bool TimeDuration::operator>=(const TimeDuration& other) const |
120 | 0 | { |
121 | 0 | return this->value >= other.value; |
122 | 0 | } |
123 | | |
124 | 122k | TimeDuration::TimeDuration(exe4cpp::duration_t value) : value(value) {} |
125 | | |
126 | | } // namespace opendnp3 |