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(); }
void YearSettingsWidget::refresh() { if( m_dirty ) { m_startWeekBox->blockSignals(true); m_startDayBox->blockSignals(true); m_startMonthBox->blockSignals(true); m_startDateEdit->blockSignals(true); m_endWeekBox->blockSignals(true); m_endDayBox->blockSignals(true); m_endMonthBox->blockSignals(true); m_endDateEdit->blockSignals(true); // Refresh Select By: if( m_yearDescription->calendarYear() ) { m_calendarYearButton->setChecked(true); int index = m_calendarYearEdit->findText(QString::number(m_yearDescription->calendarYear().get())); if (index != -1){ m_calendarYearEdit->blockSignals(true); m_calendarYearEdit->setCurrentIndex(index); m_calendarYearEdit->blockSignals(false); } m_calendarYearEdit->setEnabled(true); m_firstDayOfYearEdit->setEnabled(false); } else // First Day of Year { m_firstDayOfYearButton->setChecked(true); m_calendarYearEdit->setEnabled(false); unsigned dayOfWeekIndex = 0; std::string dayOfWeekString = m_yearDescription->dayofWeekforStartDay(); std::vector<std::string> dayOfWeekValues = model::YearDescription::validDayofWeekforStartDayValues(); for( dayOfWeekIndex = 0; dayOfWeekIndex < dayOfWeekValues.size(); dayOfWeekIndex++ ) { if( istringEqual(dayOfWeekValues[dayOfWeekIndex],dayOfWeekString) ) { break; } } m_firstDayOfYearEdit->blockSignals(true); m_firstDayOfYearEdit->setCurrentIndex(dayOfWeekIndex); m_firstDayOfYearEdit->blockSignals(false); m_firstDayOfYearEdit->setEnabled(true); } // check if we have a weather file // DLM: this is not the right place for this code, however this is an example of how to get the start day // of week from the epw file. The right approach would be to put all this in the model, however the model // would have to know the resource path. boost::optional<EpwFile> epwFile; boost::optional<model::WeatherFile> weatherFile = m_model.getOptionalUniqueModelObject<model::WeatherFile>(); if (weatherFile){ boost::shared_ptr<OSDocument> doc = OSAppBase::instance()->currentDocument(); openstudio::path resourcesPath = openstudio::toPath(doc->modelTempDir()) / openstudio::toPath("resources"); epwFile = weatherFile->file(resourcesPath); } // Refresh Daylight Savings Time boost::optional<model::RunPeriodControlDaylightSavingTime> dst = m_model.getOptionalUniqueModelObject<model::RunPeriodControlDaylightSavingTime>(); if( dst ) { m_dstOnOffButton->setChecked(true); m_dayOfWeekAndMonthStartButton->setEnabled(true); m_dateStartButton->setEnabled(true); m_dayOfWeekAndMonthEndButton->setEnabled(true); m_dateEndButton->setEnabled(true); // DST Start Date startDate = dst->startDate(); QDate qstartDate(startDate.year(), startDate.monthOfYear().value(), startDate.dayOfMonth()); boost::optional<openstudio::NthDayOfWeekInMonth> startNthDayOfWeekInMonth = dst->startNthDayOfWeekInMonth(); if (startNthDayOfWeekInMonth){ m_dayOfWeekAndMonthStartButton->setChecked(true); m_startWeekBox->setEnabled(true); m_startWeekBox->setCurrentIndex(startNthDayOfWeekInMonth->value() - 1); m_startDayBox->setEnabled(true); m_startDayBox->setCurrentIndex(startDate.dayOfWeek().value()); m_startMonthBox->setEnabled(true); m_startMonthBox->setCurrentIndex(startDate.monthOfYear().value() - 1); m_startDateEdit->setEnabled(false); m_startDateEdit->setDate(qstartDate); }else{ m_dateStartButton->setChecked(true); m_startWeekBox->setEnabled(false); m_startWeekBox->setCurrentIndex(nthDayOfWeekInMonth(startDate).value() - 1); m_startDayBox->setEnabled(false); m_startDayBox->setCurrentIndex(startDate.dayOfWeek().value()); m_startMonthBox->setEnabled(false); m_startMonthBox->setCurrentIndex(startDate.monthOfYear().value() - 1); m_startDateEdit->setEnabled(true); m_startDateEdit->setDate(qstartDate); } // DST End Date endDate = dst->endDate(); QDate qendDate(endDate.year(), endDate.monthOfYear().value(), endDate.dayOfMonth()); boost::optional<openstudio::NthDayOfWeekInMonth> endNthDayOfWeekInMonth = dst->endNthDayOfWeekInMonth(); if (endNthDayOfWeekInMonth){ m_dayOfWeekAndMonthEndButton->setChecked(true); m_endWeekBox->setEnabled(true); m_endWeekBox->setCurrentIndex(endNthDayOfWeekInMonth->value() - 1); m_endDayBox->setEnabled(true); m_endDayBox->setCurrentIndex(endDate.dayOfWeek().value()); m_endMonthBox->setEnabled(true); m_endMonthBox->setCurrentIndex(endDate.monthOfYear().value() - 1); m_endDateEdit->setEnabled(false); m_endDateEdit->setDate(qendDate); }else{ m_dateEndButton->setChecked(true); m_endWeekBox->setEnabled(false); m_endWeekBox->setCurrentIndex(nthDayOfWeekInMonth(endDate).value() - 1); m_endDayBox->setEnabled(false); m_endDayBox->setCurrentIndex(endDate.dayOfWeek().value()); m_endMonthBox->setEnabled(false); m_endMonthBox->setCurrentIndex(endDate.monthOfYear().value() - 1); m_endDateEdit->setEnabled(true); m_endDateEdit->setDate(qendDate); } }else{ m_dstOnOffButton->setChecked(false); m_dayOfWeekAndMonthStartButton->setEnabled(false); m_dateStartButton->setEnabled(false); m_dayOfWeekAndMonthEndButton->setEnabled(false); m_dateEndButton->setEnabled(false); m_startWeekBox->setEnabled(false); m_startDayBox->setEnabled(false); m_startMonthBox->setEnabled(false); m_endWeekBox->setEnabled(false); m_endDayBox->setEnabled(false); m_endMonthBox->setEnabled(false); m_startDateEdit->setEnabled(false); m_endDateEdit->setEnabled(false); } m_startWeekBox->blockSignals(false); m_startDayBox->blockSignals(false); m_startMonthBox->blockSignals(false); m_startDateEdit->blockSignals(false); m_endWeekBox->blockSignals(false); m_endDayBox->blockSignals(false); m_endMonthBox->blockSignals(false); m_endDateEdit->blockSignals(false); m_dirty = false; } }