Stride Reference Manual  - generated for commit 9643b11
Calendar.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #include "Calendar.h"
22 
23 #include "util/FileSys.h"
24 
25 #include <boost/property_tree/json_parser.hpp>
26 #include <boost/property_tree/ptree.hpp>
27 
28 namespace stride {
29 
30 using namespace std;
31 using namespace boost::property_tree::json_parser;
32 using namespace stride::util;
33 using boost::property_tree::ptree;
34 
35 #ifdef BOOST_FOUND
36 
37 Calendar::Calendar(const ptree& configPt) : m_date(), m_holidays(), m_school_holidays(), m_day(0U)
38 {
39  // Set start date
40  m_date = boost::gregorian::from_simple_string(configPt.get<string>("run.start_date", "2016-01-01"));
41 
42  // Set holidays & school holidays
43  InitializeHolidays(configPt);
44 }
45 
47 {
48  m_day++;
49  m_date = m_date + boost::gregorian::date_duration(1);
50 }
51 
52 size_t Calendar::GetDay() const { return m_date.day(); }
53 
54 size_t Calendar::GetDayOfTheWeek() const { return m_date.day_of_week(); }
55 
56 size_t Calendar::GetMonth() const { return m_date.month(); }
57 
58 unsigned short int Calendar::GetSimulationDay() const { return m_day; }
59 
60 size_t Calendar::GetYear() const { return m_date.year(); }
61 
62 void Calendar::InitializeHolidays(const ptree& configPt)
63 {
64  // Load json file
65  ptree holidaysPt;
66  {
67  const string fName{configPt.get<string>("run.holidays_file", "holidays_flanders_2017.json")};
68  const filesys::path fPath{FileSys::GetDataDir() /= fName};
69  if (!is_regular_file(fPath)) {
70  throw runtime_error(string(__func__) + "Holidays file " + fPath.string() + " not present.");
71  }
72  read_json(fPath.string(), holidaysPt);
73  }
74 
75  // Read in holidays
76  for (int i = 1; i < 13; i++) {
77  const auto month = to_string(i);
78  const auto year = holidaysPt.get<string>("year", "2017");
79  const auto lead = string(year).append("-").append(month).append("-");
80 
81  // read in general holidays
82  for (const auto& date : holidaysPt.get_child("general." + month)) {
83  const auto d = string(lead).append(date.second.get_value<string>());
84  m_holidays.push_back(boost::gregorian::from_simple_string(d));
85  }
86 
87  // read in school holidays
88  for (const auto& date : holidaysPt.get_child("school." + month)) {
89  const string d = string(lead).append(date.second.get_value<string>());
90  m_school_holidays.push_back(boost::gregorian::from_simple_string(d));
91  }
92  }
93 }
94 
95 #else
96 
97 date::year_month_day ConvertFromString(const string& day)
98 {
99  tm timeinfo{};
100  stringstream ss(day);
101  ss >> get_time(&timeinfo, "%Y-%m-%d");
102  auto date = date::year{timeinfo.tm_year + 1900} / date::month{static_cast<unsigned int>(timeinfo.tm_mon + 1)} /
103  date::day{static_cast<unsigned int>(timeinfo.tm_mday)};
104  return date;
105 }
106 
107 Calendar::Calendar(const boost::property_tree::ptree& configPt)
108  : m_date(), m_holidays(), m_school_holidays(), m_day(static_cast<size_t>(0))
109 {
110  const string start_date{configPt.get<string>("run.start_date", "2016-01-01")};
111  // Set start date
112  m_date = ConvertFromString(start_date);
113  // Set holidays & school holidays
114  InitializeHolidays(configPt);
115 }
116 
118 {
119  m_day++;
120  m_date = static_cast<date::year_month_day>(static_cast<date::sys_days>(m_date) + date::days(1));
121 }
122 
123 void Calendar::InitializeHolidays(const ptree& configPt)
124 {
125  // Load json file
126  ptree holidaysPt;
127  {
128  const string fName{configPt.get<string>("run.holidays_file", "holidays_flanders_2017.json")};
129  const filesystem::path fPath{FileSys::GetDataDir() /= fName};
130  if (!is_regular_file(fPath)) {
131  throw runtime_error(string(__func__) + "Holidays file " + fPath.string() + " not present.");
132  }
133  read_json(fPath.string(), holidaysPt);
134  }
135 
136  // Read in holidays
137  for (int i = 1; i < 13; i++) {
138  const auto month = to_string(i);
139  const auto year = holidaysPt.get<string>("year", "2017");
140 
141  // read in general holidays
142  for (const auto& date : holidaysPt.get_child("general." + month)) {
143  stringstream d;
145  d << year << "-" << setw(2) << setfill('0') << month << "-" << setw(2) << setfill('0')
146  << date.second.get_value<string>();
147  m_holidays.push_back(ConvertFromString(d.str()));
148  }
149 
150  // read in school holidays
151  for (const auto& date : holidaysPt.get_child("school." + month)) {
152  stringstream d;
154  d << year << "-" << setw(2) << setfill('0') << month << "-" << setw(2) << setfill('0')
155  << date.second.get_value<string>();
156  m_school_holidays.push_back(ConvertFromString(d.str()));
157  }
158  }
159 }
160 
161 size_t Calendar::GetDay() const { return static_cast<unsigned int>(m_date.day()); }
162 
164 {
165  return static_cast<unsigned>(static_cast<date::year_month_weekday>(m_date).weekday());
166 }
167 
168 size_t Calendar::GetMonth() const { return static_cast<unsigned int>(m_date.month()); }
169 
170 unsigned short int Calendar::GetSimulationDay() const { return m_day; }
171 
172 size_t Calendar::GetYear() const { return static_cast<size_t>(static_cast<int>(m_date.year())); }
173 
174 #endif
175 
176 } // namespace stride
Calendar(const boost::property_tree::ptree &configPt)
Constructor.
Definition: Calendar.cpp:107
static filesys::path GetDataDir()
/// Return data dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:88
void InitializeHolidays(const boost::property_tree::ptree &configPt)
Definition: Calendar.cpp:123
std::vector< date::year_month_day > m_school_holidays
Vector of school holidays.
Definition: Calendar.h:91
unsigned short int m_day
Current day since start of simulation.
Definition: Calendar.h:93
void AdvanceDay()
Advance the simulated calendar by one day.
Definition: Calendar.cpp:117
Utilities for the project.
std::vector< date::year_month_day > m_holidays
Vector of general holidays.
Definition: Calendar.h:90
date::year_month_day m_date
Current simulated date.
Definition: Calendar.h:89
std::size_t GetDayOfTheWeek() const
Current day of the week (0 (Sunday), ..., 6 (Saturday)) in the simulated calendar.
Definition: Calendar.cpp:163
std::size_t GetYear() const
Current year in the simulated calendar.
Definition: Calendar.cpp:172
STL namespace.
std::size_t GetDay() const
Current day of the month in the simulated calendar.
Definition: Calendar.cpp:161
date::year_month_day ConvertFromString(const string &day)
Definition: Calendar.cpp:97
unsigned short int GetSimulationDay() const
Current simulated day since the start of the simulation.
Definition: Calendar.cpp:170
Interface for install directory queries.
Header file for the Calendar class.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
std::size_t GetMonth() const
Current month in the simulated calendar.
Definition: Calendar.cpp:168