src/utilities/time/Calendar.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 "Calendar.hpp" | ||
| 7 | #include "DateTime.hpp" | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | using namespace boost; | ||
| 11 | |||
| 12 | namespace openstudio { | ||
| 13 | |||
| 14 | /// default constructor, uses assumed year | ||
| 15 | ✗ | Calendar::Calendar() | |
| 16 | ✗ | : m_startDate(Date(MonthOfYear::Jan, 1, YearDescription().assumedYear())), m_endDate(Date(MonthOfYear::Dec, 31, YearDescription().assumedYear())) {} | |
| 17 | |||
| 18 | /// constructor with year | ||
| 19 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | Calendar::Calendar(int year) : m_startDate(Date(MonthOfYear::Jan, 1, year)), m_endDate(Date(MonthOfYear::Dec, 31, year)) {} |
| 20 | |||
| 21 | /// constructor with start and end dates | ||
| 22 | ✗ | Calendar::Calendar(const Date& startDate, const Date& endDate) : m_startDate(startDate), m_endDate(endDate) {} | |
| 23 | |||
| 24 | /// set daylight savings time, [daylightSavingsStart, daylightSavingsEnd) | ||
| 25 | ✗ | void Calendar::daylightSavings(const Date& daylightSavingsStart, const Date& daylightSavingsEnd) { | |
| 26 | ✗ | if (daylightSavingsEnd <= daylightSavingsStart) { | |
| 27 | ✗ | LOG(Error, "End Date " << daylightSavingsEnd << " is before or equal to start date " << daylightSavingsStart); | |
| 28 | ✗ | return; | |
| 29 | } | ||
| 30 | |||
| 31 | ✗ | if (!includesDate(daylightSavingsStart)) { | |
| 32 | ✗ | LOG(Error, "Calendar does not include daylight savings start date " << daylightSavingsStart); | |
| 33 | ✗ | return; | |
| 34 | } | ||
| 35 | |||
| 36 | ✗ | if (!includesDate(daylightSavingsEnd)) { | |
| 37 | ✗ | LOG(Error, "Calendar does not include daylight savings end date " << daylightSavingsEnd); | |
| 38 | ✗ | return; | |
| 39 | } | ||
| 40 | |||
| 41 | ✗ | m_daylightSavingsStart = daylightSavingsStart; | |
| 42 | ✗ | m_daylightSavingsEnd = daylightSavingsEnd; | |
| 43 | } | ||
| 44 | |||
| 45 | /// standard daylight savings time, [2nd Sunday in March, 1st Sunday in November) | ||
| 46 | 3 | void Calendar::standardDaylightSavings() { | |
| 47 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
|
3 | m_daylightSavingsStart = Date::fromNthDayOfMonth(NthDayOfWeekInMonth::second, DayOfWeek::Sunday, MonthOfYear::Mar, m_startDate.year()); |
| 48 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
|
3 | m_daylightSavingsEnd = Date::fromNthDayOfMonth(NthDayOfWeekInMonth::first, DayOfWeek::Sunday, MonthOfYear::Nov, m_startDate.year()); |
| 49 | 3 | } | |
| 50 | |||
| 51 | /// add a standard set of holidays | ||
| 52 | 4 | void Calendar::standardHolidays() { | |
| 53 | // DLM@2009_07_14: do we need to "round these" to nearest Friday or Monday? maybe call it "XXX Observed" | ||
| 54 | // holidays with specific dates | ||
| 55 |
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.
|
8 | addHoliday(Date(MonthOfYear::Jan, 1, m_startDate.year()), "New Years Day"); |
| 56 |
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.
|
8 | addHoliday(Date(MonthOfYear::Jul, 4, m_startDate.year()), "Independence Day"); |
| 57 |
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.
|
8 | addHoliday(Date(MonthOfYear::Nov, 11, m_startDate.year()), "Veterans Day"); |
| 58 |
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.
|
8 | addHoliday(Date(MonthOfYear::Dec, 25, m_startDate.year()), "Christmas"); |
| 59 | |||
| 60 | // holidays expressed as nth day of month | ||
| 61 |
7/7✓ 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.
✓ Branch 16 taken 4 times.
✓ Branch 19 taken 4 times.
|
8 | addHoliday(Date::fromNthDayOfMonth(NthDayOfWeekInMonth::third, DayOfWeek::Monday, MonthOfYear::Jan, m_startDate.year()), "MLK Day"); |
| 62 |
7/7✓ 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.
✓ Branch 16 taken 4 times.
✓ Branch 19 taken 4 times.
|
8 | addHoliday(Date::fromNthDayOfMonth(NthDayOfWeekInMonth::third, DayOfWeek::Monday, MonthOfYear::Feb, m_startDate.year()), "Presidents Day"); |
| 63 |
7/7✓ 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.
✓ Branch 16 taken 4 times.
✓ Branch 19 taken 4 times.
|
8 | addHoliday(Date::fromNthDayOfMonth(NthDayOfWeekInMonth::fifth, DayOfWeek::Monday, MonthOfYear::May, m_startDate.year()), "Memorial Day"); |
| 64 |
7/7✓ 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.
✓ Branch 16 taken 4 times.
✓ Branch 19 taken 4 times.
|
8 | addHoliday(Date::fromNthDayOfMonth(NthDayOfWeekInMonth::first, DayOfWeek::Monday, MonthOfYear::Sep, m_startDate.year()), "Labor Day"); |
| 65 |
7/7✓ 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.
✓ Branch 16 taken 4 times.
✓ Branch 19 taken 4 times.
|
4 | addHoliday(Date::fromNthDayOfMonth(NthDayOfWeekInMonth::second, DayOfWeek::Monday, MonthOfYear::Oct, m_startDate.year()), "Columbus Day"); |
| 66 | |||
| 67 | // Thanksgiving is kind of weird, should we include Friday afterwards (e.g. office) or not (e.g. retail) | ||
| 68 |
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 | Date thanksgiving = Date::fromNthDayOfMonth(NthDayOfWeekInMonth::fourth, DayOfWeek::Thursday, MonthOfYear::Nov, m_startDate.year()); |
| 69 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
8 | addHoliday(thanksgiving, "Thanksgiving"); |
| 70 |
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 | addHoliday(DateTime(thanksgiving, Time(1, 0, 0, 0)).date(), "Day After Thanksgiving"); |
| 71 | 4 | } | |
| 72 | |||
| 73 | /// add a holiday | ||
| 74 | 47 | void Calendar::addHoliday(const Date& date, const std::string& name) { | |
| 75 |
2/3✓ Branch 1 taken 47 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 47 times.
|
47 | if (!includesDate(date)) { |
| 76 | ✗ | LOG(Error, "Calendar does not include date " << date); | |
| 77 | ✗ | return; | |
| 78 | } | ||
| 79 | |||
| 80 | 47 | CalendarDay day; | |
| 81 | 47 | day.isHoliday = true; | |
| 82 |
1/1✓ Branch 1 taken 47 times.
|
47 | day.name = name; |
| 83 | |||
| 84 |
2/2✓ Branch 1 taken 47 times.
✓ Branch 4 taken 47 times.
|
47 | m_dateMap[date] = day; |
| 85 | 47 | } | |
| 86 | |||
| 87 | /// add a non holiday named day | ||
| 88 | 3 | void Calendar::addNamedDay(const Date& date, const std::string& name) { | |
| 89 |
2/3✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | if (!includesDate(date)) { |
| 90 | ✗ | LOG(Error, "Calendar does not include date " << date); | |
| 91 | ✗ | return; | |
| 92 | } | ||
| 93 | |||
| 94 | 3 | CalendarDay day; | |
| 95 | 3 | day.isHoliday = false; | |
| 96 |
1/1✓ Branch 1 taken 3 times.
|
3 | day.name = name; |
| 97 | |||
| 98 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | m_dateMap[date] = day; |
| 99 | 3 | } | |
| 100 | |||
| 101 | /// does the calendar include this date, e.g. are they from the same year | ||
| 102 | 8969 | bool Calendar::includesDate(const Date& date) const { | |
| 103 |
2/4✓ Branch 1 taken 8969 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8969 times.
✗ Branch 5 not taken.
|
8969 | return ((date >= m_startDate) && (date <= m_endDate)); |
| 104 | } | ||
| 105 | |||
| 106 | /// is date within daylight savings | ||
| 107 | 54 | bool Calendar::isDaylightSavings(const Date& date) const { | |
| 108 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
|
54 | if (!includesDate(date)) { |
| 109 | ✗ | LOG(Error, "Calendar does not include date " << date); | |
| 110 | ✗ | return false; | |
| 111 | } | ||
| 112 | |||
| 113 | 54 | bool result(false); | |
| 114 |
5/6✓ Branch 1 taken 27 times.
✓ Branch 2 taken 27 times.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 27 times.
✓ Branch 7 taken 27 times.
|
135 | if (m_daylightSavingsStart && m_daylightSavingsEnd) { |
| 115 |
4/4✓ Branch 2 taken 21 times.
✓ Branch 3 taken 6 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 9 times.
|
27 | result = ((date >= *m_daylightSavingsStart) && (date < *m_daylightSavingsEnd)); |
| 116 | } | ||
| 117 | 54 | return result; | |
| 118 | } | ||
| 119 | |||
| 120 | /// is date a holiday | ||
| 121 | 8811 | bool Calendar::isHoliday(const Date& date) const { | |
| 122 |
2/3✓ Branch 1 taken 8811 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8811 times.
|
8811 | if (!includesDate(date)) { |
| 123 | ✗ | LOG(Error, "Calendar does not include date " << date); | |
| 124 | ✗ | return false; | |
| 125 | } | ||
| 126 | |||
| 127 | 8811 | bool result = false; | |
| 128 |
1/1✓ Branch 1 taken 8811 times.
|
8811 | auto it = m_dateMap.find(date); |
| 129 |
2/2✓ Branch 2 taken 279 times.
✓ Branch 3 taken 8532 times.
|
8811 | if (it != m_dateMap.end()) { |
| 130 | 279 | result = it->second.isHoliday; | |
| 131 | } | ||
| 132 | |||
| 133 | 8811 | return result; | |
| 134 | } | ||
| 135 | |||
| 136 | /// is the date on the calendar, either holiday or non holiday | ||
| 137 | 27 | bool Calendar::isNamedDay(const Date& date) const { | |
| 138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (!includesDate(date)) { |
| 139 | ✗ | LOG(Error, "Calendar does not include date " << date); | |
| 140 | ✗ | return false; | |
| 141 | } | ||
| 142 | |||
| 143 |
1/1✓ Branch 2 taken 27 times.
|
27 | return (m_dateMap.find(date) != m_dateMap.end()); |
| 144 | } | ||
| 145 | |||
| 146 | 2 | Date Calendar::startDate() const { | |
| 147 | 2 | return m_startDate; | |
| 148 | } | ||
| 149 | |||
| 150 | 367 | Date Calendar::endDate() const { | |
| 151 | 367 | return m_endDate; | |
| 152 | } | ||
| 153 | |||
| 154 | /// get the name of date, return empty if not on calendar | ||
| 155 | 27 | std::string Calendar::getName(const Date& date) const { | |
| 156 |
2/3✓ Branch 1 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
|
27 | if (!includesDate(date)) { |
| 157 | ✗ | LOG(Error, "Calendar does not include date " << date); | |
| 158 | ✗ | return {}; | |
| 159 | } | ||
| 160 | |||
| 161 | 27 | std::string result; | |
| 162 |
1/1✓ Branch 1 taken 27 times.
|
27 | auto it = m_dateMap.find(date); |
| 163 |
2/2✓ Branch 2 taken 15 times.
✓ Branch 3 taken 12 times.
|
27 | if (it != m_dateMap.end()) { |
| 164 |
1/1✓ Branch 2 taken 15 times.
|
15 | result = it->second.name; |
| 165 | } | ||
| 166 | |||
| 167 | 27 | return result; | |
| 168 | 27 | } | |
| 169 | |||
| 170 | } // namespace openstudio | ||
| 171 |