GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 92.3% 156 / 0 / 169
Functions: 91.9% 34 / 0 / 37
Branches: 87.1% 175 / 0 / 201

src/utilities/time/DateTime.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 "DateTime.hpp"
7 #include <fmt/format.h>
8
9 using namespace std;
10 using namespace boost;
11
12 namespace openstudio {
13
14 /// get the local time now
15 24 DateTime DateTime::now() {
16
1/1
✓ Branch 1 taken 24 times.
24 posix_time::ptime now = posix_time::second_clock::local_time();
17
6/6
✓ Branch 1 taken 24 times.
✓ Branch 4 taken 24 times.
✓ Branch 7 taken 24 times.
✓ Branch 10 taken 24 times.
✓ Branch 13 taken 24 times.
✓ Branch 16 taken 24 times.
48 return DateTime(Date(now.date()), Time(now.time_of_day()), DateTime::localOffsetUTC());
18 }
19
20 /// get the utc time now
21 6907 DateTime DateTime::nowUTC() {
22
1/1
✓ Branch 1 taken 6907 times.
6907 posix_time::ptime now = posix_time::second_clock::universal_time();
23
5/5
✓ Branch 1 taken 6907 times.
✓ Branch 4 taken 6907 times.
✓ Branch 7 taken 6907 times.
✓ Branch 10 taken 6907 times.
✓ Branch 13 taken 6907 times.
6907 return DateTime(Date(now.date()), Time(now.time_of_day()), 0.0);
24 }
25
26 /// get the current UTC offset
27 25 double DateTime::localOffsetUTC() {
28
3/3
✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
✓ Branch 7 taken 25 times.
25 boost::posix_time::time_duration offset = posix_time::second_clock::local_time() - posix_time::second_clock::universal_time();
29
1/1
✓ Branch 1 taken 25 times.
50 return HOURS_PER_SECOND * offset.total_seconds();
30 }
31
32 /// default constructor
33 290072 DateTime::DateTime() : m_utcOffset(0.0) {}
34
35 /// constructor from Date
36 784 DateTime::DateTime(const Date& date) : m_date(date), m_time(Time(0, 0, 0, 0)), m_utcOffset(0.0) {
37 // should not need to normalize
38 784 }
39
40 /// constructor from Date and Time
41 1175781 DateTime::DateTime(const Date& date, const Time& timeFromDate) : m_date(date), m_time(timeFromDate), m_utcOffset(0.0) {
42 1175781 normalize();
43 1175781 }
44
45 17057 DateTime::DateTime(const Date& date, const Time& timeFromDate, double utcOffset) : m_date(date), m_time(timeFromDate), m_utcOffset(utcOffset) {
46 17057 normalize();
47 17057 }
48
49 /// copy constructor
50 2245930 DateTime::DateTime(const DateTime& other) : m_date(other.date()), m_time(other.time()), m_utcOffset(other.utcOffset()) {
51 // should not need to normalize
52 2245930 }
53
54 7 DateTime::DateTime(const std::string& string) : m_date(), m_time() {
55
1/1
✓ Branch 1 taken 7 times.
7 boost::posix_time::ptime pt(boost::posix_time::time_from_string(string));
56
3/3
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
7 m_date = Date(pt.date());
57
3/3
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
7 m_time = Time(pt.time_of_day());
58 7 m_utcOffset = 0.0;
59 7 }
60
61 1 DateTime::DateTime(tm t_tm) : m_date(t_tm), m_time(t_tm), m_utcOffset(0.0) {}
62
63 /// assignment operator
64 1130691 DateTime& DateTime::operator=(const DateTime& other) {
65
2/2
✓ Branch 1 taken 1130691 times.
✓ Branch 4 taken 1130691 times.
1130691 m_date = Date(other.date());
66
2/2
✓ Branch 1 taken 1130691 times.
✓ Branch 4 taken 1130691 times.
1130691 m_time = Time(other.time());
67 1130691 m_utcOffset = other.utcOffset();
68
69 // should not need to normalize
70
71 1130691 return *this;
72 }
73
74 /// addition operator
75 313734 DateTime DateTime::operator+(const Time& time) const {
76 313734 DateTime result(*this);
77 313734 result += time;
78 313734 return result;
79 }
80
81 /// assignment by addition operator
82 357542 DateTime& DateTime::operator+=(const Time& time) {
83 357542 m_time += time;
84 357542 normalize();
85 357542 return *this;
86 }
87
88 /// difference operator
89 93 DateTime DateTime::operator-(const Time& time) const {
90 93 DateTime result(*this);
91 93 result -= time;
92 93 return result;
93 }
94
95 /// assignment by difference operator
96 98 DateTime& DateTime::operator-=(const Time& time) {
97 98 m_time -= time;
98 98 normalize();
99 98 return *this;
100 }
101
102 /// time duration
103 1343994 Time DateTime::operator-(const DateTime& dateTime) const {
104
2/2
✓ Branch 1 taken 1343994 times.
✓ Branch 4 taken 1343994 times.
1343994 Time t1 = (m_date - dateTime.date());
105
2/2
✓ Branch 1 taken 1343994 times.
✓ Branch 4 taken 1343994 times.
1343994 Time t2 = (m_time - dateTime.time());
106
2/2
✓ Branch 1 taken 1343994 times.
✓ Branch 4 taken 1343994 times.
1343994 Time t3((dateTime.utcOffset() - m_utcOffset) / HOURS_PER_DAY);
107
2/2
✓ Branch 1 taken 1343994 times.
✓ Branch 4 taken 1343994 times.
1343994 return t1 + t2 + t3;
108 }
109
110 /// time duration
111 ✗ Time DateTime::operator-(const Date& date) const {
112 ✗ return m_time + (m_date - date);
113 }
114
115 /// equality operator
116 97 bool DateTime::operator==(const DateTime& other) const {
117 97 return (toEpoch() == other.toEpoch());
118 }
119
120 /// non-equality operator
121 ✗ bool DateTime::operator!=(const DateTime& other) const {
122 ✗ return (toEpoch() != other.toEpoch());
123 ;
124 }
125
126 /// less than operator
127 2189116 bool DateTime::operator<(const DateTime& rhs) const {
128 2189116 return (toEpoch() < rhs.toEpoch());
129 }
130
131 /// less than equals operator
132 17579 bool DateTime::operator<=(const DateTime& rhs) const {
133 17579 return (toEpoch() <= rhs.toEpoch());
134 }
135
136 /// greater than operator
137 1 bool DateTime::operator>(const DateTime& rhs) const {
138 1 return (toEpoch() > rhs.toEpoch());
139 }
140
141 /// greater than equals operator
142 ✗ bool DateTime::operator>=(const DateTime& rhs) const {
143 ✗ return (toEpoch() >= rhs.toEpoch());
144 }
145
146 /// getter to date
147 8352088 Date DateTime::date() const {
148 8352088 return m_date;
149 }
150
151 /// getter to time
152 6002388 Time DateTime::time() const {
153 6002388 return m_time;
154 }
155
156 /// UTC offset in hours
157 4720633 double DateTime::utcOffset() const {
158 4720633 return m_utcOffset;
159 }
160
161 /// convert to string
162 1795944 std::string DateTime::toString() const {
163
7/7
✓ Branch 1 taken 1795944 times.
✓ Branch 4 taken 1795944 times.
✓ Branch 7 taken 1795944 times.
✓ Branch 10 taken 1795944 times.
✓ Branch 13 taken 1795944 times.
✓ Branch 16 taken 1795944 times.
✓ Branch 19 taken 1795944 times.
1795944 const boost::posix_time::ptime pt(m_date.impl(), m_time.impl() - boost::posix_time::time_duration(utcOffsetHours(), utcOffsetMinutes(), 0, 0));
164
1/1
✓ Branch 1 taken 1795944 times.
3591888 return boost::posix_time::to_simple_string(pt);
165 }
166
167 15203 std::string DateTime::toISO8601() const {
168 // 20160713T160843-06:00
169 // 20160713T160843Z
170
3/3
✓ Branch 1 taken 15203 times.
✓ Branch 4 taken 15203 times.
✓ Branch 7 taken 15203 times.
15203 const boost::posix_time::ptime pt(m_date.impl(), m_time.impl());
171
1/1
✓ Branch 1 taken 15203 times.
15203 std::string result = boost::posix_time::to_iso_string(pt);
172
2/2
✓ Branch 0 taken 15202 times.
✓ Branch 1 taken 1 time.
15203 if (m_utcOffset == 0.0) {
173
1/1
✓ Branch 1 taken 15202 times.
15202 result += "Z";
174 } else {
175
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
1 const Time temp(0, utcOffsetHours(), utcOffsetMinutes(), 0);
176 1 char offset = '+';
177
2/3
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
1 if (temp.totalHours() < 0) {
178 ✗ offset = '-';
179 }
180
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
2 result += fmt::format("{}{:02d}{:02d}", offset, std::abs(temp.hours()), std::abs(temp.minutes()));
181 }
182
183 30406 return result;
184 ✗ }
185
186 828 std::string DateTime::toXsdDateTime() const {
187 // 2016-07-13T16:08:43-06:00
188 // 2016-07-13T16:08:43Z
189
190
3/3
✓ Branch 1 taken 828 times.
✓ Branch 4 taken 828 times.
✓ Branch 7 taken 828 times.
828 const boost::posix_time::ptime pt(m_date.impl(), m_time.impl());
191
1/1
✓ Branch 1 taken 828 times.
828 std::string result = boost::posix_time::to_iso_extended_string(pt);
192
2/2
✓ Branch 0 taken 827 times.
✓ Branch 1 taken 1 time.
828 if (m_utcOffset == 0.0) {
193
1/1
✓ Branch 1 taken 827 times.
827 result += "Z";
194 } else {
195
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
1 const Time temp(0, utcOffsetHours(), utcOffsetMinutes(), 0);
196 1 char offset = '+';
197
2/3
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
1 if (temp.totalHours() < 0) {
198 ✗ offset = '-';
199 }
200
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
2 result += fmt::format("{}{:02d}:{:02d}", offset, std::abs(temp.hours()), std::abs(temp.minutes()));
201 }
202
203 1656 return result;
204 ✗ }
205
206 4413591 std::time_t DateTime::toEpoch() const {
207 // credit: http://stackoverflow.com/questions/4461586/how-do-i-convert-boostposix-timeptime-to-time-t
208
7/7
✓ Branch 1 taken 4413591 times.
✓ Branch 4 taken 4413591 times.
✓ Branch 7 taken 4413591 times.
✓ Branch 10 taken 4413591 times.
✓ Branch 13 taken 4413591 times.
✓ Branch 16 taken 4413591 times.
✓ Branch 19 taken 4413591 times.
4413591 boost::posix_time::ptime pt(m_date.impl(), m_time.impl() - boost::posix_time::time_duration(utcOffsetHours(), utcOffsetMinutes(), 0, 0));
209 4413591 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
210
2/2
✓ Branch 1 taken 4413591 times.
✓ Branch 4 taken 4413591 times.
4413591 boost::posix_time::time_duration::sec_type x = (pt - epoch).total_seconds();
211 4413591 return std::time_t(x);
212 }
213
214 10122 boost::optional<DateTime> DateTime::fromISO8601(const std::string& str) {
215
216
1/1
✓ Branch 1 taken 10122 times.
10122 std::string finalStr(str);
217
1/1
✓ Branch 1 taken 10122 times.
10122 boost::replace_all(finalStr, ".", "");
218
1/1
✓ Branch 1 taken 10122 times.
10122 boost::replace_all(finalStr, ":", "");
219
1/1
✓ Branch 1 taken 10122 times.
10122 boost::replace_all(finalStr, "Z", "+0000");
220
221 10122 std::string timeZone;
222
223
4/7
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 10086 times.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 36 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
10122 static const boost::regex yyyymmddThhmmssffRegex("^(\\d{4}-?\\d{2}-?\\d{2}T\\d{6})\\d{2}(.*?)$");
224
4/7
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 10086 times.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 36 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
10122 static const boost::regex yyyymmddThhmmssRegex("^(\\d{4}-?\\d{2}-?\\d{2}T\\d{6})(.*?)$");
225
4/7
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 10086 times.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 36 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
10122 static const boost::regex yyyymmddThhmmRegex("^(\\d{4}-?\\d{2}-?\\d{2}T\\d{4})(.*?)$");
226
1/1
✓ Branch 1 taken 10122 times.
10122 boost::smatch m;
227
3/3
✓ Branch 1 taken 10122 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 10118 times.
10122 if (boost::regex_match(finalStr, m, yyyymmddThhmmssffRegex)) {
228
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
8 timeZone = std::string(m[2].first, m[2].second); // pull out timeZone first because reassigning finalStr will invalidate iterators
229
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
8 finalStr = std::string(m[1].first, m[1].second);
230
3/3
✓ Branch 1 taken 10118 times.
✓ Branch 3 taken 10114 times.
✓ Branch 4 taken 4 times.
10118 } else if (boost::regex_match(finalStr, m, yyyymmddThhmmssRegex)) {
231
3/3
✓ Branch 1 taken 10114 times.
✓ Branch 4 taken 10114 times.
✓ Branch 7 taken 10114 times.
20228 timeZone = std::string(m[2].first, m[2].second); // pull out timeZone first because reassigning finalStr will invalidate iterators
232
3/3
✓ Branch 1 taken 10114 times.
✓ Branch 4 taken 10114 times.
✓ Branch 7 taken 10114 times.
20228 finalStr = std::string(m[1].first, m[1].second);
233
2/3
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 } else if (boost::regex_match(finalStr, m, yyyymmddThhmmRegex)) {
234
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
8 timeZone = std::string(m[2].first, m[2].second); // pull out timeZone first because reassigning finalStr will invalidate iterators
235
4/4
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
8 finalStr = std::string(m[1].first, m[1].second) + "00";
236 }
237
1/1
✓ Branch 1 taken 10122 times.
10122 boost::replace_all(finalStr, "-", "");
238
239 10122 double utcOffset(0.0);
240
2/2
✓ Branch 1 taken 119 times.
✓ Branch 2 taken 10003 times.
10122 if (!timeZone.empty()) {
241
4/7
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 84 times.
✓ Branch 3 taken 35 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 35 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
119 static const boost::regex utcOffsetRegex("^([+-])(\\d{2})(\\d{1,2})$");
242
2/3
✓ Branch 1 taken 119 times.
✓ Branch 3 taken 119 times.
✗ Branch 4 not taken.
119 if (boost::regex_match(timeZone, m, utcOffsetRegex)) {
243
3/3
✓ Branch 1 taken 119 times.
✓ Branch 4 taken 119 times.
✓ Branch 7 taken 119 times.
238 std::string sign(m[1].first, m[1].second);
244
3/3
✓ Branch 1 taken 119 times.
✓ Branch 4 taken 119 times.
✓ Branch 7 taken 119 times.
238 std::string hour(m[2].first, m[2].second);
245
3/3
✓ Branch 1 taken 119 times.
✓ Branch 4 taken 119 times.
✓ Branch 7 taken 119 times.
119 std::string min(m[3].first, m[3].second);
246
2/2
✓ Branch 1 taken 119 times.
✓ Branch 4 taken 119 times.
119 utcOffset += boost::lexical_cast<double>(sign + hour);
247
1/2
✓ Branch 1 taken 119 times.
✗ Branch 2 not taken.
119 if (!min.empty()) {
248
2/2
✓ Branch 1 taken 119 times.
✓ Branch 4 taken 119 times.
119 utcOffset += boost::lexical_cast<double>(sign + min) / MINUTES_PER_HOUR;
249 }
250 119 }
251 }
252
253 10122 OptionalDateTime result;
254 try {
255
1/1
✓ Branch 1 taken 10122 times.
10122 boost::posix_time::ptime pt = boost::posix_time::from_iso_string(finalStr);
256
6/6
✓ Branch 1 taken 10122 times.
✓ Branch 4 taken 10122 times.
✓ Branch 7 taken 10122 times.
✓ Branch 10 taken 10122 times.
✓ Branch 13 taken 10122 times.
✓ Branch 16 taken 10122 times.
10122 result = DateTime(Date(pt.date()), Time(pt.time_of_day()), utcOffset);
257 ✗ } catch (...) {
258 ✗ LOG(Error, "Could not convert '" << str << "' to DateTime using boost::posix_time::from_iso_string.");
259 ✗ }
260
261 20244 return result;
262 10122 }
263
264 64 boost::optional<DateTime> DateTime::fromXsdDateTime(const std::string& str) {
265 64 return fromISO8601(str);
266 }
267
268 4 DateTime DateTime::fromEpoch(const std::time_t& time) {
269
1/1
✓ Branch 1 taken 4 times.
4 boost::posix_time::ptime pt = boost::posix_time::from_time_t(time);
270
5/5
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
4 return DateTime(Date(pt.date()), Time(pt.time_of_day()), 0.0);
271 }
272
273 // ensure that stored time is less than 24 hrs and adjust date accordingly
274 1550478 void DateTime::normalize() {
275 // total number of whole days in the time
276 1550478 int wholeDays = m_time.days();
277
278 // if time is longer than a day (either positive or negative) normalize it to remainder less than 24 hrs
279
2/2
✓ Branch 0 taken 313022 times.
✓ Branch 1 taken 1237456 times.
1550478 if (wholeDays != 0) {
280
1/1
✓ Branch 1 taken 313022 times.
313022 Time adjustTime(wholeDays, 0, 0, 0);
281
1/1
✓ Branch 1 taken 313022 times.
313022 m_date += adjustTime;
282
1/1
✓ Branch 1 taken 313022 times.
313022 m_time -= adjustTime;
283 }
284
285 // total number of fractional days in the time
286 1550478 double fracDays = m_time.totalDays();
287
288 // if time is negative add a day to make it positive
289
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1550454 times.
1550478 if (fracDays < 0) {
290
1/1
✓ Branch 1 taken 24 times.
24 Time adjustTime(1, 0, 0, 0);
291
1/1
✓ Branch 1 taken 24 times.
24 m_date -= adjustTime;
292
1/1
✓ Branch 1 taken 24 times.
24 m_time += adjustTime;
293 }
294 1550478 }
295
296 12419074 int DateTime::utcOffsetHours() const {
297 12419074 return (int)m_utcOffset;
298 }
299
300 6209537 int DateTime::utcOffsetMinutes() const {
301 6209537 double minutes = 60 * (m_utcOffset - utcOffsetHours());
302 6209537 return (int)minutes;
303 }
304
305 // std::ostream operator<<
306 1795944 std::ostream& operator<<(std::ostream& os, const DateTime& dateTime) {
307
2/2
✓ Branch 1 taken 1795944 times.
✓ Branch 4 taken 1795944 times.
1795944 os << dateTime.toString();
308 1795944 return os;
309 }
310
311 } // namespace openstudio
312