GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 81.0% 17 / 1 / 22
Functions: 81.0% 17 / 1 / 22
Branches: 74.2% 49 / 1 / 67

src/utilities/time/Date.hpp
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 #ifndef UTILITIES_TIME_DATE_HPP
7 #define UTILITIES_TIME_DATE_HPP
8
9 #include "../UtilitiesAPI.hpp"
10
11 #include "Time.hpp"
12 #include "../core/Macro.hpp"
13 #include "../core/Logger.hpp"
14
15 #include <ostream>
16 #include <vector>
17
18 #include <boost/optional.hpp>
19 #include <boost/date_time/gregorian/gregorian.hpp>
20 #include <boost/date_time/date_defs.hpp>
21 #include <boost/date_time/gregorian/greg_calendar.hpp>
22 #include "../core/Enum.hpp"
23
24 namespace openstudio {
25
26 // clang-format off
27
28 /** \class DayOfWeek
29 * \brief DayOfWeek is an enumeration of the days of the week: Sunday, Monday, Tuesday,
30 * Wednesday, Thursday, Friday, Saturday.
31 * \details See the OPENSTUDIO_ENUM documentation in utilities/core/Enum.hpp. The actual
32 * macro call is:
33 * \code
34 OPENSTUDIO_ENUM(DayOfWeek,
35 ((Sunday)(Sunday)(boost::date_time::Sunday))
36 ((Monday)(Monday)(boost::date_time::Monday))
37 ((Tuesday)(Tuesday)(boost::date_time::Tuesday))
38 ((Wednesday)(Wednesday)(boost::date_time::Wednesday))
39 ((Thursday)(Thursday)(boost::date_time::Thursday))
40 ((Friday)(Friday)(boost::date_time::Friday))
41 ((Saturday)(Saturday)(boost::date_time::Saturday))
42 );
43 * \endcode */
44
19/22
openstudio::DayOfWeek::buildStringVec[abi:cxx11](bool):
✓ Branch 0 taken 497 times.
✓ Branch 1 taken 4242 times.
✓ Branch 3 taken 497 times.
✓ Branch 6 taken 497 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 497 times.
✓ Branch 12 taken 497 times.
✓ Branch 17 taken 4242 times.
✓ Branch 20 taken 4242 times.
✓ Branch 23 taken 4062 times.
✓ Branch 24 taken 1354 times.
✓ Branch 25 taken 677 times.
✓ Branch 26 taken 677 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 677 times.
openstudio::DayOfWeek::enumName[abi:cxx11]():
✗ Branch 1 not taken.
openstudio::operator<<(std::ostream&, openstudio::DayOfWeek const&):
✓ Branch 1 taken 206 times.
✓ Branch 4 taken 206 times.
✓ Branch 7 taken 206 times.
✓ Branch 10 taken 206 times.
✓ Branch 13 taken 206 times.
✓ Branch 16 taken 206 times.
26522 OPENSTUDIO_ENUM(DayOfWeek,
45 ((Sunday)(Sunday)(boost::date_time::Sunday))
46 ((Monday)(Monday)(boost::date_time::Monday))
47 ((Tuesday)(Tuesday)(boost::date_time::Tuesday))
48 ((Wednesday)(Wednesday)(boost::date_time::Wednesday))
49 ((Thursday)(Thursday)(boost::date_time::Thursday))
50 ((Friday)(Friday)(boost::date_time::Friday))
51 ((Saturday)(Saturday)(boost::date_time::Saturday))
52 );
53
54 /// Convert string to DayOfWeek. Accepts full string or first three letters.
55 UTILITIES_API DayOfWeek dayOfWeek(const std::string& day);
56
57 /** \class MonthOfYear
58 * \brief MonthOfYear is an enumeration of months: January, February, March,
59 * April, May, June, July, August, September, October, November, December.
60 * \details See the OPENSTUDIO_ENUM documentation in utilities/core/Enum.hpp. The actual
61 * macro call is:
62 * \code
63 OPENSTUDIO_ENUM(MonthOfYear,
64 ((Jan)(January)(boost::date_time::Jan))
65 ((Feb)(February)(boost::date_time::Feb))
66 ((Mar)(March)(boost::date_time::Mar))
67 ((Apr)(April)(boost::date_time::Apr))
68 ((May)(May)(boost::date_time::May))
69 ((Jun)(June)(boost::date_time::Jun))
70 ((Jul)(July)(boost::date_time::Jul))
71 ((Aug)(August)(boost::date_time::Aug))
72 ((Sep)(September)(boost::date_time::Sep))
73 ((Oct)(October)(boost::date_time::Oct))
74 ((Nov)(November)(boost::date_time::Nov))
75 ((Dec)(December)(boost::date_time::Dec))
76 ((NotAMonth)(Not a month)(boost::date_time::NotAMonth))
77 ((NumMonths)(Number of months)(boost::date_time::NumMonths))
78 );
79 * \endcode */
80
18/22
openstudio::MonthOfYear::buildStringVec[abi:cxx11](bool):
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 48244 times.
✓ Branch 3 taken 700 times.
✓ Branch 6 taken 700 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 700 times.
✓ Branch 12 taken 700 times.
✓ Branch 17 taken 48244 times.
✓ Branch 20 taken 48244 times.
✓ Branch 23 taken 48944 times.
✓ Branch 24 taken 3496 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 3496 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 3496 times.
openstudio::MonthOfYear::enumName[abi:cxx11]():
✗ Branch 1 not taken.
openstudio::operator<<(std::ostream&, openstudio::MonthOfYear const&):
✓ Branch 1 taken 56 times.
✓ Branch 4 taken 56 times.
✓ Branch 7 taken 56 times.
✓ Branch 10 taken 56 times.
✓ Branch 13 taken 56 times.
✓ Branch 16 taken 56 times.
37094048 OPENSTUDIO_ENUM(MonthOfYear,
81 ((Jan)(January)(boost::date_time::Jan))
82 ((Feb)(February)(boost::date_time::Feb))
83 ((Mar)(March)(boost::date_time::Mar))
84 ((Apr)(April)(boost::date_time::Apr))
85 ((May)(May)(boost::date_time::May))
86 ((Jun)(June)(boost::date_time::Jun))
87 ((Jul)(July)(boost::date_time::Jul))
88 ((Aug)(August)(boost::date_time::Aug))
89 ((Sep)(September)(boost::date_time::Sep))
90 ((Oct)(October)(boost::date_time::Oct))
91 ((Nov)(November)(boost::date_time::Nov))
92 ((Dec)(December)(boost::date_time::Dec))
93 ((NotAMonth)(Not a month)(boost::date_time::NotAMonth))
94 ((NumMonths)(Number of months)(boost::date_time::NumMonths))
95 );
96
97 /// Converts string to MonthOfYear. Accepts full string or first three letters.
98 UTILITIES_API MonthOfYear monthOfYear(const std::string& month);
99
100 /// convert unsigned to MonthOfYear
101 UTILITIES_API MonthOfYear monthOfYear(unsigned month);
102
103 /// convert MonthOfYear to unsigned
104 UTILITIES_API unsigned month(MonthOfYear monthOfYear);
105
106 /** \class NthDayOfWeekInMonth
107 * \brief NthDayOfWeekInMonth is an enum for the nth DayOfWeek in MonthOfYear
108 * (e.g. first Saturday in January).
109 * \details The fifth NthDayOfWeekInMonth is equivalent to the last DayOfWeek in
110 * MonthOfYear. See the OPENSTUDIO_ENUM documentation in utilities/core/Enum.hpp
111 * for documentation of the class structure. The actual macro call is:
112 * \code
113 OPENSTUDIO_ENUM( NthDayOfWeekInMonth,
114 ((first)(first)(boost::gregorian::nth_day_of_the_week_in_month::first))
115 ((second)(second)(boost::gregorian::nth_day_of_the_week_in_month::second))
116 ((third)(third)(boost::gregorian::nth_day_of_the_week_in_month::third))
117 ((fourth)(fourth)(boost::gregorian::nth_day_of_the_week_in_month::fourth))
118 ((fifth)(fifth)(boost::gregorian::nth_day_of_the_week_in_month::fifth))
119 );
120 * \endcode */
121
12/22
openstudio::NthDayOfWeekInMonth::buildStringVec[abi:cxx11](bool):
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 125 times.
✓ Branch 3 taken 25 times.
✓ Branch 6 taken 25 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 25 times.
✓ Branch 12 taken 25 times.
✓ Branch 17 taken 125 times.
✓ Branch 20 taken 125 times.
✓ Branch 23 taken 150 times.
✓ Branch 24 taken 30 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 30 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 30 times.
openstudio::NthDayOfWeekInMonth::enumName[abi:cxx11]():
✗ Branch 1 not taken.
openstudio::operator<<(std::ostream&, openstudio::NthDayOfWeekInMonth const&):
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
572 OPENSTUDIO_ENUM( NthDayOfWeekInMonth,
122 ((first)(first)(boost::gregorian::nth_day_of_the_week_in_month::first))
123 ((second)(second)(boost::gregorian::nth_day_of_the_week_in_month::second))
124 ((third)(third)(boost::gregorian::nth_day_of_the_week_in_month::third))
125 ((fourth)(fourth)(boost::gregorian::nth_day_of_the_week_in_month::fourth))
126 ((fifth)(fifth)(boost::gregorian::nth_day_of_the_week_in_month::fifth))
127 );
128
129 /// Convert string to NthDayOfWeekInMonth. Accepts full string, also accepts 1, 1st, 2, 2nd, etc.
130 UTILITIES_API NthDayOfWeekInMonth nthDayOfWeekInMonth(const std::string& nth);
131
132 // clang-format on
133
134 /// YearDescription describes a year with several optional parameters
135 struct UTILITIES_API YearDescription
136 {
137
138 /// constructor
139 YearDescription();
140
141 /// get the assumed year
142 int assumedYear() const;
143
144 /// is the year a leap year, defaults to false
145 bool isLeapYear;
146
147 /// first day of the year, defaults to not set
148 boost::optional<DayOfWeek> yearStartsOnDayOfWeek;
149
150 /// look for year in future, defaults to false
151 bool lookInFuture;
152 };
153
154 class DateTime;
155
156 /// Date is an absolute unit of time, resolution to the day
157 /// Date is simple wrapper around boost::gregorian::date
158 class UTILITIES_API Date
159 {
160 public:
161 friend UTILITIES_API std::ostream& operator<<(std::ostream& os, const Date& date);
162 friend UTILITIES_API std::ostream& operator<<(std::ostream& os, const DateTime& dateTime);
163 friend class DateTime;
164
165 /// impl type is boost::gregorian::date
166 using ImplType = boost::gregorian::date;
167 //typedef std::shared_ptr<ImplType> ImplPtr;
168
169 − REGISTER_LOGGER("utilities.time.Date");
170
171 /// nth weekday of month generators
172 static Date fromNthDayOfMonth(NthDayOfWeekInMonth n, DayOfWeek dayOfWeek, MonthOfYear monthOfYear);
173 static Date fromNthDayOfMonth(NthDayOfWeekInMonth n, DayOfWeek dayOfWeek, MonthOfYear monthOfYear, int year);
174
175 /// Day of year generators
176 static Date fromDayOfYear(unsigned dayOfYear);
177 static Date fromDayOfYear(unsigned dayOfYear, int year);
178 static Date fromDayOfYear(unsigned dayOfYear, const YearDescription& yearDescription);
179
180 /// is year a leap year
181 static bool isLeapYear(int year);
182
183 /// get current Date
184 static Date currentDate();
185
186 /// default constructor
187 Date();
188
189 /// destructor
190 ~Date() = default;
191
192 /// from impl
193 Date(const ImplType& impl);
194
195 /// Date from month, day of month
196 Date(MonthOfYear monthOfYear, unsigned dayOfMonth);
197
198 /// Date from month, day of month, year
199 Date(MonthOfYear monthOfYear, unsigned dayOfMonth, int year);
200
201 /// Date from YearDescription, month, and day of month
202 Date(MonthOfYear monthOfYear, unsigned dayOfMonth, const YearDescription& yearDescription);
203
204 /// constructor from string
205 Date(const std::string& string);
206
207 /// Date from a system tm structure
208 Date(tm t_tm);
209
210 /// copy constructor
211 Date(const Date& other);
212
213 /// addition operator
214 Date operator+(const Time& time) const;
215
216 /// assignment by addition operator
217 Date& operator+=(const Time& time);
218
219 /// difference operator
220 Date operator-(const Time& time) const;
221
222 /// assignment by difference operator
223 Date& operator-=(const Time& time);
224
225 /// time duration
226 Time operator-(const Date& date) const;
227
228 /// equality operator
229 bool operator==(const Date& other) const;
230
231 /// non-equality operator
232 bool operator!=(const Date& other) const;
233
234 /// less than operator
235 bool operator<(const Date& rhs) const;
236
237 /// less than equals operator
238 bool operator<=(const Date& rhs) const;
239
240 /// greater than operator
241 bool operator>(const Date& rhs) const;
242
243 /// greater than equals operator
244 bool operator>=(const Date& rhs) const;
245
246 /// assignment operator
247 Date& operator=(const Date& other);
248
249 /// user provided base year
250 boost::optional<int> baseYear() const;
251
252 /// set base year
253 void baseYear(int baseYear);
254
255 /// assumed base year
256 int assumedBaseYear() const;
257
258 /// year
259 int year() const;
260
261 /// month of year
262 MonthOfYear monthOfYear() const;
263
264 /// day of month
265 unsigned dayOfMonth() const;
266
267 /// day of year
268 unsigned dayOfYear() const;
269
270 /// is assumed year a leap year
271 bool isLeapYear() const;
272
273 /// day of the week
274 DayOfWeek dayOfWeek() const;
275
276 protected:
277 // reference to impl
278 const ImplType impl() const;
279
280 private:
281 // initFromYearMonthDay
282 void initFromYearMonthDay(int year, MonthOfYear monthOfYear, unsigned dayOfMonth);
283
284 // initFromYearDayOfYear
285 void initFromYearDayOfYear(int year, unsigned dayOfYear);
286
287 ImplType m_impl;
288
289 // user provided base year is optional
290 boost::optional<int> m_baseYear;
291
292 // always have assumed base year
293 int m_assumedBaseYear;
294 };
295
296 /// optional Date
297 using OptionalDate = boost::optional<Date>;
298
299 /// vector of Date
300 using DateVector = std::vector<Date>;
301
302 // std::ostream operator<<
303 UTILITIES_API std::ostream& operator<<(std::ostream& os, const Date& date);
304
305 /// DateCompare compares two dates, returns true if lhs is less than rhs
306 class UTILITIES_API DateCompare
307 {
308 public:
309 /// operator()
310 bool operator()(const Date& lhs, const Date& rhs) const;
311 };
312
313 /// return nthDayOfWeekInMonth for given date
314 UTILITIES_API NthDayOfWeekInMonth nthDayOfWeekInMonth(const Date& date);
315
316 } // namespace openstudio
317
318 #endif // UTILITIES_TIME_DATE_HPP
319