Line data Source code
1 : /* 2 : * Copyright (C) 2020-2024 MEmilio 3 : * 4 : * Authors: Daniel Abele 5 : * 6 : * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> 7 : * 8 : * Licensed under the Apache License, Version 2.0 (the "License"); 9 : * you may not use this file except in compliance with the License. 10 : * You may obtain a copy of the License at 11 : * 12 : * http://www.apache.org/licenses/LICENSE-2.0 13 : * 14 : * Unless required by applicable law or agreed to in writing, software 15 : * distributed under the License is distributed on an "AS IS" BASIS, 16 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 : * See the License for the specific language governing permissions and 18 : * limitations under the License. 19 : */ 20 : #ifndef MIO_ABM_TIME_H 21 : #define MIO_ABM_TIME_H 22 : 23 : #include "memilio/io/default_serialize.h" 24 : 25 : namespace mio 26 : { 27 : namespace abm 28 : { 29 : 30 : /** 31 : * @brief A duration of time. 32 : * Resolution 1 second. 33 : */ 34 : class TimeSpan 35 : { 36 : public: 37 : /** 38 : * @brief Default ctor, uninitialized. 39 : */ 40 37674 : TimeSpan() = default; 41 : 42 : /** 43 : * @brief Creates a TimeSpan that represents a number of seconds. 44 : * @param[in] seconds The number of seconds. 45 : */ 46 20942 : explicit TimeSpan(int seconds) 47 20942 : : m_seconds(seconds) 48 : { 49 20942 : } 50 : 51 : /** 52 : * @brief Length of time in days. 53 : */ 54 2844 : double days() const 55 : { 56 2844 : return double(m_seconds) / (24 * 60 * 60); 57 : } 58 : 59 : /** 60 : * @brief Length of time in hours. 61 : */ 62 56 : double hours() const 63 : { 64 56 : return double(m_seconds) / (60 * 60); 65 : }; 66 : 67 : /** 68 : * @brief Length of time in seconds. 69 : */ 70 15003 : int seconds() const 71 : { 72 15003 : return m_seconds; 73 : } 74 : 75 : /** 76 : * @name Comparison operators. 77 : * @{ 78 : */ 79 81 : bool operator==(const TimeSpan& other) const 80 : { 81 81 : return m_seconds == other.m_seconds; 82 : } 83 : bool operator!=(const TimeSpan& other) const 84 : { 85 : return !(*this == other); 86 : } 87 954 : bool operator<(const TimeSpan& other) const 88 : { 89 954 : return m_seconds < other.m_seconds; 90 : } 91 126 : bool operator<=(const TimeSpan& other) const 92 : { 93 126 : return m_seconds <= other.m_seconds; 94 : } 95 864 : bool operator>(const TimeSpan& other) const 96 : { 97 864 : return m_seconds > other.m_seconds; 98 : } 99 603 : bool operator>=(const TimeSpan& other) const 100 : { 101 603 : return m_seconds >= other.m_seconds; 102 : } 103 : /**@}*/ 104 : 105 : /** 106 : * @name Numeric operators for addition, subtraction, and scalar integer multiplication and division. 107 : * @{ 108 : */ 109 1629 : TimeSpan operator+(const TimeSpan& s) const 110 : { 111 1629 : return TimeSpan{m_seconds + s.m_seconds}; 112 : } 113 2844 : TimeSpan& operator+=(const TimeSpan& s) 114 : { 115 2844 : m_seconds += s.m_seconds; 116 2844 : return *this; 117 : } 118 : TimeSpan operator-(const TimeSpan& s) const 119 : { 120 : return TimeSpan{m_seconds - s.m_seconds}; 121 : } 122 : TimeSpan& operator-=(const TimeSpan& s) 123 : { 124 : m_seconds -= s.m_seconds; 125 : return *this; 126 : } 127 : 128 : TimeSpan operator*(int f) const 129 : { 130 : return TimeSpan{m_seconds * f}; 131 : } 132 : TimeSpan& operator*=(int f) 133 : { 134 : m_seconds *= f; 135 : return *this; 136 : } 137 1719 : TimeSpan operator/(int f) const 138 : { 139 1719 : return TimeSpan{m_seconds / f}; 140 : } 141 : TimeSpan& operator/=(int f) 142 : { 143 : m_seconds /= f; 144 : return *this; 145 : } 146 : /**@}*/ 147 : 148 : /// This method is used by the default serialization feature. 149 270 : auto default_serialize() 150 : { 151 540 : return Members("TimeSpan").add("seconds", m_seconds); 152 : } 153 : 154 : private: 155 : int m_seconds; ///< The duration of time in seconds. 156 : }; 157 : 158 : /** 159 : * @brief Represents a point in time. 160 : * Seconds from an unspecified monday at 00:00 (epoch). 161 : */ 162 : class TimePoint 163 : { 164 : public: 165 : /** 166 : * @brief Default ctor, unitinialized. 167 : */ 168 1062 : TimePoint() = default; 169 : /** 170 : * @brief Creates a TimePoint from a specified number of seconds. 171 : * @param[in] seconds The number of seconds after the epoch. 172 : */ 173 33373 : explicit TimePoint(int seconds) 174 33373 : : m_seconds(seconds) 175 : { 176 33373 : } 177 : 178 : /** 179 : * @brief Time since the epoch in days. 180 : */ 181 10058 : double days() const 182 : { 183 10058 : return double(m_seconds) / (24 * 60 * 60); 184 : } 185 : /** 186 : * @brief Time since the epoch in hours. 187 : */ 188 1692 : double hours() const 189 : { 190 1692 : return double(m_seconds) / (60 * 60); 191 : }; 192 : /** 193 : * @brief Time since the epoch in seconds. 194 : */ 195 2835 : int seconds() const 196 : { 197 2835 : return m_seconds; 198 : } 199 : 200 : /** 201 : * @brief Index of current day of the week (0,...,6 = Mo,...,Sun). 202 : */ 203 3051 : int day_of_week() const 204 : { 205 3051 : return int(days()) % 7; 206 : } 207 : 208 : /** 209 : * @brief If the current time is on a weekend, e.g. day_of_the_week is 5 = Sat or 6 = Sun. 210 : */ 211 756 : bool is_weekend() const 212 : { 213 756 : return (day_of_week() > 4) ? true : false; 214 : } 215 : 216 : /** 217 : * @brief Hour in the current day (0 - 23). 218 : */ 219 1692 : int hour_of_day() const 220 : { 221 1692 : return int(hours()) % 24; 222 : } 223 : 224 : /** 225 : * @brief Time since midnight. 226 : */ 227 1638 : TimeSpan time_since_midnight() const 228 : { 229 1638 : return TimeSpan(seconds() - ((int)days()) * 60 * 60 * 24); 230 : } 231 : 232 : /** 233 : * @name Comparison operators. 234 : * @{ 235 : */ 236 72 : bool operator==(const TimePoint& other) const 237 : { 238 72 : return m_seconds == other.m_seconds; 239 : } 240 : bool operator!=(const TimePoint& other) const 241 : { 242 : return !(*this == other); 243 : } 244 18549 : bool operator<(const TimePoint& other) const 245 : { 246 18549 : return m_seconds < other.m_seconds; 247 : } 248 990 : bool operator<=(const TimePoint& other) const 249 : { 250 990 : return m_seconds <= other.m_seconds; 251 : } 252 32679 : bool operator>(const TimePoint& other) const 253 : { 254 32679 : return m_seconds > other.m_seconds; 255 : } 256 2574 : bool operator>=(const TimePoint& other) const 257 : { 258 2574 : return m_seconds >= other.m_seconds; 259 : } 260 : /**@}*/ 261 : 262 : /** 263 : * @brief Add or subtract a TimeSpan. 264 : * @{ 265 : */ 266 6597 : TimePoint operator+(const TimeSpan& s) const 267 : { 268 6597 : return TimePoint{m_seconds + s.seconds()}; 269 : } 270 675 : TimePoint& operator+=(const TimeSpan& s) 271 : { 272 675 : m_seconds += s.seconds(); 273 675 : return *this; 274 : } 275 1395 : TimePoint operator-(const TimeSpan& s) const 276 : { 277 1395 : return TimePoint{m_seconds - s.seconds()}; 278 : } 279 : TimePoint& operator-=(const TimeSpan& s) 280 : { 281 : m_seconds -= s.seconds(); 282 : return *this; 283 : } 284 : /**@}*/ 285 : 286 : /** 287 : * @brief TimeSpan difference between this and another TimePoint. 288 : * @param[in] p2 The other TimePoint. 289 : */ 290 900 : TimeSpan operator-(const TimePoint& p2) const 291 : { 292 900 : return TimeSpan{m_seconds - p2.seconds()}; 293 : } 294 : 295 : /// This method is used by the default serialization feature. 296 189 : auto default_serialize() 297 : { 298 378 : return Members("TimePoint").add("seconds", m_seconds); 299 : } 300 : 301 : private: 302 : int m_seconds; ///< The number of seconds after the epoch. 303 : }; 304 : 305 : /** 306 : * @brief Create a TimeSpan of a specified number of seconds. 307 : * @param[in] seconds Number of seconds in the TimeSpan. 308 : */ 309 1215 : inline TimeSpan seconds(int seconds) 310 : { 311 1215 : return TimeSpan(seconds); 312 : } 313 : 314 : /** 315 : * @brief Create a TimeSpan of a specified number of minutes. 316 : * @param[in] minutes Number of minutes in the TimeSpan. 317 : */ 318 937 : inline TimeSpan minutes(int minutes) 319 : { 320 937 : return TimeSpan(minutes * 60); 321 : } 322 : 323 : /** 324 : * @brief Create a TimeSpan of a specified number of hours. 325 : * @param[in] seconds Number of hours in the TimeSpan. 326 : */ 327 6270 : inline TimeSpan hours(int hours) 328 : { 329 6270 : return TimeSpan(hours * 60 * 60); 330 : } 331 : 332 : /** 333 : * @brief Create a TimeSpan with a specified number of days. 334 : * @param[in] seconds Number of days in the TimeSpan. 335 : */ 336 2539 : inline TimeSpan days(int days) 337 : { 338 2539 : return TimeSpan(days * 24 * 60 * 60); 339 : } 340 : 341 1773 : inline TimeSpan days(double days) 342 : { 343 1773 : return TimeSpan((int)(days * 24 * 60 * 60)); 344 : } 345 : 346 : } // namespace abm 347 : } // namespace mio 348 : 349 : #endif