boost::optional<ScheduleWeek> ScheduleYear_Impl::getScheduleWeek(const openstudio::Date& date) const { YearDescription yd = this->model().getUniqueModelObject<YearDescription>(); if (yd.assumedYear() != date.assumedBaseYear()){ LOG(Error, "Assumed base year " << date.assumedBaseYear() << " of date does not match this model's assumed base year of " << yd.assumedYear()); return boost::none; } std::vector<ScheduleWeek> scheduleWeeks = this->scheduleWeeks(); // these are already sorted std::vector<openstudio::Date> dates = this->dates(); // these are already sorted unsigned N = dates.size(); OS_ASSERT(scheduleWeeks.size() == N); if (N == 0){ return boost::none; } boost::optional<ScheduleWeek> result; for (unsigned i = 0; i < N; ++i){ // want first date which is greater than or equal to the target date if(dates[i] >= date){ result = scheduleWeeks[i]; break; } } return result; }
openstudio::Date RunPeriodControlSpecialDays_Impl::getDate(const std::string& text) const { Date result; YearDescription yd = this->model().getUniqueModelObject<YearDescription>(); /* \note <number>/<number> (month/day) \note <number> <Month> \note <Month> <number> \note <Nth> <Weekday> in <Month) \note Last <WeekDay> in <Month> \note <Month> can be January, February, March, April, May, June, July, August, September, October, November, December \note Months can be the first 3 letters of the month \note <Weekday> can be Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday \note <Nth> can be 1 or 1st, 2 or 2nd, etc. up to 5(?) */ boost::smatch matches; if (boost::regex_search(text, matches, boost::regex("(\\d+)\\s?\\/\\s?(\\d+)"))){ std::string monthString(matches[1].first, matches[1].second); MonthOfYear monthOfYear(boost::lexical_cast<unsigned>(monthString)); std::string dayOfMonthString(matches[2].first, matches[2].second); unsigned dayOfMonth = boost::lexical_cast<unsigned>(dayOfMonthString); result = yd.makeDate(monthOfYear, dayOfMonth); return result; }else if (boost::regex_search(text, matches, boost::regex("(\\d+)\\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|January|February|March|April|May|June|July|August|September|October|November|December)", boost::regex::icase))){ std::string dayOfMonthString(matches[1].first, matches[1].second); unsigned dayOfMonth = boost::lexical_cast<unsigned>(dayOfMonthString); std::string monthString(matches[2].first, matches[2].second); result = yd.makeDate(monthOfYear(monthString), dayOfMonth); return result; }else if (boost::regex_search(text, matches, boost::regex("(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|January|February|March|April|May|June|July|August|September|October|November|December)\\s+(\\d+)", boost::regex::icase))){ std::string monthString(matches[1].first, matches[1].second); std::string dayOfMonthString(matches[2].first, matches[2].second); unsigned dayOfMonth = boost::lexical_cast<unsigned>(dayOfMonthString); result = yd.makeDate(monthOfYear(monthString), dayOfMonth); return result; }else if (boost::regex_search(text, matches, boost::regex("(1|2|3|4|5|1st|2nd|3rd|4th|5th|Last)\\s+(Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)\\s+in\\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|January|February|March|April|May|June|July|August|September|October|November|December)", boost::regex::icase))){ std::string nthString(matches[1].first, matches[1].second); std::string dayOfWeekString(matches[2].first, matches[2].second); std::string monthString(matches[3].first, matches[3].second); result = yd.makeDate(nthDayOfWeekInMonth(nthString), dayOfWeek(dayOfWeekString), monthOfYear(monthString)); return result; } LOG_AND_THROW("Could not determine date for startDate = '" << text << "'"); return Date(); }
std::vector<openstudio::Date> ScheduleRule_Impl::specificDates() const { YearDescription yd = this->model().getUniqueModelObject<model::YearDescription>(); std::vector<openstudio::Date> result; BOOST_FOREACH(const ModelExtensibleGroup& group,castVector<ModelExtensibleGroup>(extensibleGroups())) { OptionalInt month = group.getInt(0); BOOST_ASSERT(month); OptionalInt day = group.getInt(1); BOOST_ASSERT(day); result.push_back(yd.makeDate(MonthOfYear(*month), *day)); }
boost::optional<openstudio::Date> ScheduleRule_Impl::endDate() const { boost::optional<openstudio::Date> result; boost::optional<std::string> dateSpecificationType = this->getString(OS_Schedule_RuleFields::DateSpecificationType, true); BOOST_ASSERT(dateSpecificationType); if (istringEqual("DateRange", *dateSpecificationType)){ boost::optional<int> endMonth = this->getInt(OS_Schedule_RuleFields::EndMonth, true); BOOST_ASSERT(endMonth); boost::optional<int> endDay = this->getInt(OS_Schedule_RuleFields::EndDay, true); BOOST_ASSERT(endDay); YearDescription yd = this->model().getUniqueModelObject<model::YearDescription>(); result = yd.makeDate(MonthOfYear(*endMonth), *endDay); } return result; }
Date Date::fromDayOfYear(unsigned dayOfYear, const YearDescription& yearDescription) { Date result; result.m_assumedBaseYear = yearDescription.assumedYear(); result.initFromYearDayOfYear(result.m_assumedBaseYear, dayOfYear); return result; }
std::vector<openstudio::Date> ScheduleYear_Impl::dates() const { std::vector<openstudio::Date> result; YearDescription yd = this->model().getUniqueModelObject<YearDescription>(); for (const ModelExtensibleGroup& group : castVector<ModelExtensibleGroup>(this->extensibleGroups())) { OptionalUnsigned month = group.getUnsigned(0, true); OptionalUnsigned day = group.getUnsigned(1, true); if (month && day){ result.push_back(yd.makeDate(*month, *day)); }else{ LOG(Error, "Could not read date " << group.groupIndex() << " in " << briefDescription() << "." ); } } return result; }
bool ScheduleYear_Impl::addScheduleWeek(const openstudio::Date& untilDate, const ScheduleWeek& scheduleWeek) { YearDescription yd = this->model().getUniqueModelObject<YearDescription>(); if (yd.assumedYear() != untilDate.assumedBaseYear()){ LOG(Error, "Assumed base year " << untilDate.assumedBaseYear() << " of untilDate does not match this model's assumed base year of " << yd.assumedYear()); return false; } std::vector<ScheduleWeek> scheduleWeeks = this->scheduleWeeks(); // these are already sorted std::vector<openstudio::Date> dates = this->dates(); // these are already sorted bool inserted = false; unsigned N = dates.size(); OS_ASSERT(scheduleWeeks.size() == N); this->clearExtensibleGroups(); for (unsigned i = 0; i < N; ++i){ if (dates[i] == untilDate){ bool doEmit = (i == (N-1)); // push back just this schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(untilDate.monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(untilDate.dayOfMonth())); groupValues.push_back(scheduleWeek.name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, doEmit).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); inserted = true; }else{ // if we need to insert new schedule/date pair here if ((untilDate < dates[i]) && !inserted){ // push back this schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(untilDate.monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(untilDate.dayOfMonth())); groupValues.push_back(scheduleWeek.name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, false).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); inserted = true; } bool doEmit = (i == (N-1)) && inserted; // insert existing schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(dates[i].monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(dates[i].dayOfMonth())); groupValues.push_back(scheduleWeeks[i].name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, doEmit).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); } } if (!inserted){ // push back this schedule/date pair std::vector<std::string> groupValues; groupValues.push_back(boost::lexical_cast<std::string>(untilDate.monthOfYear().value())); groupValues.push_back(boost::lexical_cast<std::string>(untilDate.dayOfMonth())); groupValues.push_back(scheduleWeek.name().get()); ModelExtensibleGroup group = pushExtensibleGroup(groupValues, true).cast<ModelExtensibleGroup>(); OS_ASSERT(!group.empty()); } return true; }
/// Date from month, day of month, and YearDescription Date::Date(MonthOfYear monthOfYear, unsigned dayOfMonth, const YearDescription& yearDescription) : m_assumedBaseYear(yearDescription.assumedYear()) { initFromYearMonthDay(m_assumedBaseYear, monthOfYear, dayOfMonth); }
TEST_F(ModelFixture, YearDescription) { Model model; YearDescription yd = model.getUniqueModelObject<YearDescription>(); EXPECT_FALSE(yd.calendarYear()); EXPECT_EQ("UseWeatherFile", yd.dayofWeekforStartDay()); EXPECT_TRUE(yd.isDayofWeekforStartDayDefaulted()); EXPECT_FALSE(yd.isLeapYear()); EXPECT_TRUE(yd.isIsLeapYearDefaulted()); yd.setCalendarYear(2008); ASSERT_TRUE(yd.calendarYear()); EXPECT_EQ(2008, yd.calendarYear().get()); EXPECT_EQ("Tuesday", yd.dayofWeekforStartDay()); EXPECT_TRUE(yd.isDayofWeekforStartDayDefaulted()); EXPECT_TRUE(yd.isLeapYear()); EXPECT_TRUE(yd.isIsLeapYearDefaulted()); EXPECT_FALSE(yd.setDayofWeekforStartDay("Monday")); EXPECT_EQ("Tuesday", yd.dayofWeekforStartDay()); EXPECT_TRUE(yd.isDayofWeekforStartDayDefaulted()); EXPECT_FALSE(yd.setIsLeapYear(false)); EXPECT_TRUE(yd.isLeapYear()); EXPECT_TRUE(yd.isIsLeapYearDefaulted()); yd.resetCalendarYear(); EXPECT_EQ("UseWeatherFile", yd.dayofWeekforStartDay()); EXPECT_TRUE(yd.isDayofWeekforStartDayDefaulted()); EXPECT_FALSE(yd.isLeapYear()); EXPECT_TRUE(yd.isIsLeapYearDefaulted()); EXPECT_TRUE(yd.setDayofWeekforStartDay("Monday")); EXPECT_EQ("Monday", yd.dayofWeekforStartDay()); EXPECT_FALSE(yd.isDayofWeekforStartDayDefaulted()); EXPECT_TRUE(yd.setIsLeapYear(true)); EXPECT_TRUE(yd.isLeapYear()); EXPECT_FALSE(yd.isIsLeapYearDefaulted()); yd.setCalendarYear(2009); ASSERT_TRUE(yd.calendarYear()); EXPECT_EQ(2009, yd.calendarYear().get()); EXPECT_EQ("Thursday", yd.dayofWeekforStartDay()); EXPECT_TRUE(yd.isDayofWeekforStartDayDefaulted()); EXPECT_FALSE(yd.isLeapYear()); EXPECT_TRUE(yd.isIsLeapYearDefaulted()); }