time.h Source File

CPP API: time.h Source File
time.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 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/config.h"
25 
26 namespace mio
27 {
28 namespace abm
29 {
30 
35 class TimeSpan
36 {
37 public:
41  TimeSpan() = default;
42 
47  explicit TimeSpan(int seconds)
49  {
50  }
51 
55  ScalarType days() const
56  {
57  return ScalarType(m_seconds) / (24 * 60 * 60);
58  }
59 
63  ScalarType hours() const
64  {
65  return ScalarType(m_seconds) / (60 * 60);
66  };
67 
71  int seconds() const
72  {
73  return m_seconds;
74  }
75 
80  bool operator==(const TimeSpan& other) const
81  {
82  return m_seconds == other.m_seconds;
83  }
84  bool operator!=(const TimeSpan& other) const
85  {
86  return !(*this == other);
87  }
88  bool operator<(const TimeSpan& other) const
89  {
90  return m_seconds < other.m_seconds;
91  }
92  bool operator<=(const TimeSpan& other) const
93  {
94  return m_seconds <= other.m_seconds;
95  }
96  bool operator>(const TimeSpan& other) const
97  {
98  return m_seconds > other.m_seconds;
99  }
100  bool operator>=(const TimeSpan& other) const
101  {
102  return m_seconds >= other.m_seconds;
103  }
110  TimeSpan operator+(const TimeSpan& s) const
111  {
112  return TimeSpan{m_seconds + s.m_seconds};
113  }
115  {
116  m_seconds += s.m_seconds;
117  return *this;
118  }
119  TimeSpan operator-(const TimeSpan& s) const
120  {
121  return TimeSpan{m_seconds - s.m_seconds};
122  }
124  {
125  m_seconds -= s.m_seconds;
126  return *this;
127  }
128 
129  TimeSpan operator*(int f) const
130  {
131  return TimeSpan{m_seconds * f};
132  }
133 
139  TimeSpan multiply(double f) const
140  {
141  return TimeSpan{int(m_seconds * f)};
142  }
143 
145  {
146  m_seconds *= f;
147  return *this;
148  }
149  TimeSpan operator/(int f) const
150  {
151  return TimeSpan{m_seconds / f};
152  }
154  {
155  m_seconds /= f;
156  return *this;
157  }
162  {
163  return Members("TimeSpan").add("seconds", m_seconds);
164  }
165 
166 private:
167  int m_seconds;
168 };
169 
175 {
176 public:
180  TimePoint() = default;
185  explicit TimePoint(int seconds)
186  : m_seconds(seconds)
187  {
188  }
189 
193  ScalarType days() const
194  {
195  return ScalarType(m_seconds) / (24 * 60 * 60);
196  }
201  {
202  return ScalarType(m_seconds) / (60 * 60);
203  };
207  int seconds() const
208  {
209  return m_seconds;
210  }
211 
215  int day_of_week() const
216  {
217  return int(days()) % 7;
218  }
219 
223  bool is_weekend() const
224  {
225  return (day_of_week() > 4) ? true : false;
226  }
227 
231  int hour_of_day() const
232  {
233  return int(hours()) % 24;
234  }
235 
240  {
241  return TimeSpan(seconds() - ((int)days()) * 60 * 60 * 24);
242  }
243 
248  bool operator==(const TimePoint& other) const
249  {
250  return m_seconds == other.m_seconds;
251  }
252  bool operator!=(const TimePoint& other) const
253  {
254  return !(*this == other);
255  }
256  bool operator<(const TimePoint& other) const
257  {
258  return m_seconds < other.m_seconds;
259  }
260  bool operator<=(const TimePoint& other) const
261  {
262  return m_seconds <= other.m_seconds;
263  }
264  bool operator>(const TimePoint& other) const
265  {
266  return m_seconds > other.m_seconds;
267  }
268  bool operator>=(const TimePoint& other) const
269  {
270  return m_seconds >= other.m_seconds;
271  }
278  TimePoint operator+(const TimeSpan& s) const
279  {
280  return TimePoint{m_seconds + s.seconds()};
281  }
283  {
284  m_seconds += s.seconds();
285  return *this;
286  }
287  TimePoint operator-(const TimeSpan& s) const
288  {
289  return TimePoint{m_seconds - s.seconds()};
290  }
292  {
293  m_seconds -= s.seconds();
294  return *this;
295  }
302  TimeSpan operator-(const TimePoint& p2) const
303  {
304  return TimeSpan{m_seconds - p2.seconds()};
305  }
306 
309  {
310  return Members("TimePoint").add("seconds", m_seconds);
311  }
312 
313 private:
314  int m_seconds;
315 };
316 
322 {
323  return TimeSpan(seconds);
324 }
325 
331 {
332  return TimeSpan(minutes * 60);
333 }
334 
339 inline TimeSpan hours(int hours)
340 {
341  return TimeSpan(hours * 60 * 60);
342 }
343 
348 inline TimeSpan days(int days)
349 {
350  return TimeSpan(days * 24 * 60 * 60);
351 }
352 
354 {
355  return TimeSpan((int)(days * 24 * 60 * 60));
356 }
357 
358 } // namespace abm
359 } // namespace mio
360 
361 #endif
Represents a point in time.
Definition: time.h:175
auto default_serialize()
This method is used by the default serialization feature.
Definition: time.h:308
TimePoint & operator+=(const TimeSpan &s)
Add or subtract a TimeSpan.
Definition: time.h:282
ScalarType days() const
Time since the epoch in days.
Definition: time.h:193
bool operator==(const TimePoint &other) const
Definition: time.h:248
TimePoint operator-(const TimeSpan &s) const
Add or subtract a TimeSpan.
Definition: time.h:287
bool operator<(const TimePoint &other) const
Definition: time.h:256
TimePoint & operator-=(const TimeSpan &s)
Add or subtract a TimeSpan.
Definition: time.h:291
int hour_of_day() const
Hour in the current day (0 - 23).
Definition: time.h:231
bool operator<=(const TimePoint &other) const
Definition: time.h:260
int m_seconds
The number of seconds after the epoch.
Definition: time.h:314
bool operator!=(const TimePoint &other) const
Definition: time.h:252
int day_of_week() const
Index of current day of the week (0,...,6 = Mo,...,Sun).
Definition: time.h:215
ScalarType hours() const
Time since the epoch in hours.
Definition: time.h:200
bool operator>(const TimePoint &other) const
Definition: time.h:264
TimePoint()=default
Default ctor, unitinialized.
bool operator>=(const TimePoint &other) const
Definition: time.h:268
TimePoint(int seconds)
Creates a TimePoint from a specified number of seconds.
Definition: time.h:185
bool is_weekend() const
If the current time is on a weekend, e.g.
Definition: time.h:223
int seconds() const
Time since the epoch in seconds.
Definition: time.h:207
TimeSpan operator-(const TimePoint &p2) const
TimeSpan difference between this and another TimePoint.
Definition: time.h:302
TimeSpan time_since_midnight() const
Time since midnight.
Definition: time.h:239
TimePoint operator+(const TimeSpan &s) const
Add or subtract a TimeSpan.
Definition: time.h:278
A duration of time.
Definition: time.h:36
TimeSpan operator/(int f) const
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:149
bool operator<(const TimeSpan &other) const
Definition: time.h:88
bool operator>=(const TimeSpan &other) const
Definition: time.h:100
auto default_serialize()
This method is used by the default serialization feature.
Definition: time.h:161
TimeSpan()=default
Default ctor, uninitialized.
TimeSpan & operator+=(const TimeSpan &s)
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:114
bool operator==(const TimeSpan &other) const
Definition: time.h:80
TimeSpan & operator*=(int f)
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:144
ScalarType hours() const
Length of time in hours.
Definition: time.h:63
TimeSpan(int seconds)
Creates a TimeSpan that represents a number of seconds.
Definition: time.h:47
ScalarType days() const
Length of time in days.
Definition: time.h:55
TimeSpan operator-(const TimeSpan &s) const
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:119
TimeSpan multiply(double f) const
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:139
TimeSpan & operator/=(int f)
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:153
TimeSpan operator+(const TimeSpan &s) const
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:110
int m_seconds
The duration of time in seconds.
Definition: time.h:167
int seconds() const
Length of time in seconds.
Definition: time.h:71
TimeSpan & operator-=(const TimeSpan &s)
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:123
bool operator!=(const TimeSpan &other) const
Definition: time.h:84
TimeSpan operator*(int f) const
Multiplication with double and rounding down afterwards to whole seconds.
Definition: time.h:129
bool operator<=(const TimeSpan &other) const
Definition: time.h:92
bool operator>(const TimeSpan &other) const
Definition: time.h:96
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
TimeSpan seconds(int seconds)
Create a TimeSpan of a specified number of seconds.
Definition: time.h:321
TimeSpan days(int days)
Create a TimeSpan with a specified number of days.
Definition: time.h:348
TimeSpan hours(int hours)
Create a TimeSpan of a specified number of hours.
Definition: time.h:339
TimeSpan minutes(int minutes)
Create a TimeSpan of a specified number of minutes.
Definition: time.h:330
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
List of a class's members.
Definition: default_serialize.h:113
Members< ValueTypes..., T > add(const char *member_name, T &member)
Add a class member.
Definition: default_serialize.h:139