GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 84.2% 80 / 0 / 95
Functions: 80.0% 28 / 0 / 35
Branches: 85.2% 23 / 0 / 27

src/utilities/time/Time.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "Time.hpp"
7
8 using namespace std;
9 using namespace boost;
10 using namespace posix_time;
11
12 namespace openstudio {
13
14 /// round towards zero, need to move this
15 11768604 int floor0(double value) {
16 11768604 int result(0);
17
2/2
✓ Branch 0 taken 831 times.
✓ Branch 1 taken 11767773 times.
11768604 if (value < 0.0) {
18 831 result = static_cast<int>(std::ceil(value));
19 } else {
20 11767773 result = static_cast<int>(std::floor(value));
21 };
22 11768604 return result;
23 }
24
25 /// get current local time of day
26 618 Time Time::currentTime() {
27
1/1
✓ Branch 1 taken 618 times.
618 posix_time::ptime now = posix_time::second_clock::local_time();
28
2/2
✓ Branch 1 taken 618 times.
✓ Branch 4 taken 618 times.
618 return Time(now.time_of_day());
29 }
30
31 /// get current UTC time of day
32 ✗ Time Time::currentTimeUTC() {
33 ✗ posix_time::ptime now = posix_time::second_clock::universal_time();
34 ✗ return Time(now.time_of_day());
35 }
36
37 /// Default constructor
38 296630 Time::Time() : m_impl(0, 0, 0, 0) {}
39
40 /// Time from number of days, fractional values ok
41 3282624 Time::Time(double fracDays) {
42 3282624 double fracHours = HOURS_PER_DAY * fracDays;
43 3282624 int hours = floor0(fracHours);
44
45 3282624 double fracMinutes = MINUTES_PER_HOUR * (fracHours - hours);
46 3282624 int minutes = floor0(fracMinutes);
47
48 3282624 double fracSeconds = SECONDS_PER_MINUTE * (fracMinutes - minutes);
49 3282624 int seconds = floor0(fracSeconds);
50
51
1/1
✓ Branch 1 taken 3282624 times.
3282624 m_impl = ImplType(hours, minutes, seconds, 0);
52 3282624 }
53
54 /// Time from days, hours, minutes, seconds
55 9539560 Time::Time(int days, int hours, int minutes, int seconds) {
56 9539560 hours += HOURS_PER_DAY * days;
57
5/6
✓ Branch 0 taken 9539558 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 9539557 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 9539557 times.
✗ Branch 5 not taken.
9539560 if ((hours * minutes >= 0) && (hours * seconds >= 0) && (minutes * seconds >= 0)) {
58 // same sign, carry on
59
1/1
✓ Branch 1 taken 9539557 times.
9539557 m_impl = ImplType(hours, minutes, seconds, 0);
60 } else {
61 // mixed sign
62
1/1
✓ Branch 4 taken 3 times.
3 ImplType negativeDuration(std::min(hours, 0), std::min(minutes, 0), std::min(seconds, 0), 0);
63
1/1
✓ Branch 4 taken 3 times.
3 m_impl = ImplType(std::max(hours, 0), std::max(minutes, 0), std::max(seconds, 0), 0);
64
1/1
✓ Branch 1 taken 3 times.
3 m_impl += negativeDuration;
65 }
66 9539560 }
67
68 345 Time::Time(const std::string& string) {
69
1/1
✓ Branch 1 taken 345 times.
345 boost::posix_time::time_duration td = boost::posix_time::duration_from_string(string);
70
71 345 m_impl = boost::posix_time::time_duration(td);
72 345 }
73
74 2 Time::Time(tm t_tm) : m_impl(t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec) {}
75
76 /// copy constructor
77 12233190 Time::Time(const Time& other) : m_impl(other.impl()) {}
78
79 /// Time from impl
80 17743 Time::Time(const ImplType& implType) : m_impl(implType) {}
81
82 /// assignment operator
83 1131663 Time& Time::operator=(const Time& other) {
84 1131663 m_impl = other.impl();
85 1131663 return *this;
86 }
87
88 /// addition operator
89 2688040 Time Time::operator+(const Time& time) const {
90 2688040 Time result(*this);
91 2688040 result += time;
92 2688040 return result;
93 }
94
95 /// assignment by addition operator
96 3046062 Time& Time::operator+=(const Time& time) {
97
2/2
✓ Branch 1 taken 3046062 times.
✓ Branch 4 taken 3046062 times.
3046062 m_impl += time.impl();
98 3046062 return *this;
99 }
100
101 1 std::string Time::toString() const {
102 1 return boost::posix_time::to_simple_string(m_impl);
103 }
104
105 /// difference operator
106 1344304 Time Time::operator-(const Time& time) const {
107 1344304 Time result(*this);
108 1344304 result -= time;
109 1344304 return result;
110 }
111
112 /// assignment by difference operator
113 1658407 Time& Time::operator-=(const Time& time) {
114
2/2
✓ Branch 1 taken 1658407 times.
✓ Branch 4 taken 1658407 times.
1658407 m_impl -= time.impl();
115 1658407 return *this;
116 }
117
118 /// multiplication operator
119 21818 Time Time::operator*(double mult) const {
120 21818 return {this->totalDays() * mult};
121 }
122
123 /// division operator
124 ✗ Time Time::operator/(double div) const {
125 ✗ return {this->totalDays() / div};
126 }
127
128 /// equality operator
129 318 bool Time::operator==(const Time& other) const {
130 318 return totalSeconds() == other.totalSeconds();
131 }
132
133 /// non-equality operator
134 ✗ bool Time::operator!=(const Time& other) const {
135 ✗ return totalSeconds() != other.totalSeconds();
136 }
137
138 /// less than operator
139 919368 bool Time::operator<(const Time& rhs) const {
140 919368 return totalSeconds() < rhs.totalSeconds();
141 }
142
143 /// less than equals operator
144 ✗ bool Time::operator<=(const Time& rhs) const {
145 ✗ return totalSeconds() <= rhs.totalSeconds();
146 }
147
148 /// greater than operator
149 ✗ bool Time::operator>(const Time& rhs) const {
150 ✗ return totalSeconds() > rhs.totalSeconds();
151 }
152
153 /// greater than equals operator
154 ✗ bool Time::operator>=(const Time& rhs) const {
155 ✗ return totalSeconds() >= rhs.totalSeconds();
156 }
157
158 /// whole number of days
159 1920732 int Time::days() const {
160 1920732 return floor0(totalDays());
161 }
162
163 /// whole number of hours remaining after days
164 224903 int Time::hours() const {
165 224903 return (m_impl.hours() % 24);
166 }
167
168 /// whole number of minutes remaining after hours
169 224898 int Time::minutes() const {
170 224898 return (int)m_impl.minutes();
171 }
172
173 /// whole number of seconds remaining after minutes
174 75403 int Time::seconds() const {
175 75403 return (int)m_impl.seconds();
176 }
177
178 /// entire time in days
179 12839835 double Time::totalDays() const {
180 12839835 return DAYS_PER_SECOND * totalSeconds();
181 }
182
183 /// entire time in hours
184 20 double Time::totalHours() const {
185 20 return HOURS_PER_SECOND * totalSeconds();
186 }
187
188 /// entire time in minutes
189 228370 double Time::totalMinutes() const {
190 228370 return MINUTES_PER_SECOND * totalSeconds();
191 }
192
193 /// entire time in seconds
194 16468746 int Time::totalSeconds() const {
195 16468746 return (int)m_impl.total_seconds();
196 }
197
198 // reference to impl
199 24296422 const Time::ImplType Time::impl() const {
200 24296422 return m_impl;
201 }
202
203 // std::ostream operator<<
204 371 std::ostream& operator<<(std::ostream& os, const Time& time) {
205 // DLM@2009_07_09: os << time.impl(); does not work on windows as strftime limits
206 // hours from 0 to 23.
207
208
3/3
✓ Branch 1 taken 371 times.
✓ Branch 4 taken 371 times.
✓ Branch 7 taken 371 times.
371 os << boost::posix_time::to_simple_string(time.impl());
209 371 return os;
210 }
211
212 // compare two Dates
213 ✗ bool TimeCompare::operator()(const Time& lhs, const Time& rhs) const {
214 ✗ return lhs < rhs;
215 }
216
217 } // namespace openstudio
218