Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
DateTime.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
5
6
7 == GNU General Public License Usage ==
8
9 OpenFLUID is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OpenFLUID is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21
22
23 == Other Usage ==
24
25 Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26 license, and requires a written agreement between You and INRA.
27 Licensees for Other Usage of OpenFLUID may use this file in accordance
28 with the terms contained in the written agreement between You and INRA.
29
30*/
31
32
33/**
34 @file DateTime.hpp
35
36 @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37*/
38
39
40#ifndef __OPENFLUID_CORE_DATETIME_HPP__
41#define __OPENFLUID_CORE_DATETIME_HPP__
42
43
44#include <sys/types.h>
45#include <time.h>
46#include <string>
47
49#include <openfluid/core/TypeDefs.hpp>
50
51
52namespace openfluid { namespace core {
53
54
55/**
56 @brief Class for management of date and time information.
57
58 Class for management of date and time information. It includes arithmetics and comparisons operations.
59
60 Sources:
61 @li Fliegel, H. F. and van Flandern, T. C. (1968). Communications of the ACM, Vol. 11, No. 10 (October, 1968).
62 http://www.decimaltime.hynes.net/index.html
63 @li http://en.wikipedia.org/wiki/Julian_day
64
65
66 <I>Example : creating a date</I>
67 @snippet misc/datetime.cpp dt_decl
68
69 <I>Example : adding time span to a date</I>
70 @snippet misc/datetime.cpp dt_add
71
72 <I>Example : subtracting time span to a date</I>
73 @snippet misc/datetime.cpp dt_sub
74
75 <I>Example : getting difference in seconds between two dates</I>
76 @snippet misc/datetime.cpp dt_diff
77
78 @cond OpenFLUID:completion
79 {
80 "contexts" : ["ANYWARE"],
81 "menupath" : ["Types", "Time"],
82 "title" : "Date and time",
83 "text" : "openfluid::core::DateTime %%SEL_START%%DT%%SEL_END%%"
84 }
85 @endcond
86*/
88{
89 private:
90
91 /**
92 The tm struct contains:
93 int tm_sec;
94 int tm_min;
95 int tm_hour;
96 int tm_mday;
97 int tm_mon;
98 int tm_year;
99 int tm_wday;
100 int tm_yday;
101 int tm_isdst;
102 */
103
104 /**
105 The date and time stored as broken down structure
106 */
107 struct tm m_TM;
108
109 /**
110 The date and time stored as number of seconds since 4713BC
111 */
112 RawTime_t m_RawTime;
113
114 void updateYMDHMSFromRawTime();
115
116 void updateRawTimeFromYMDHMS();
117
118
119 public:
120
121 /**
122 Default constructor
123 */
125
126 /**
127 Copy constructor
128 */
129 DateTime(const DateTime&) = default;
130
131 /**
132 Move constructor
133 */
134 DateTime(DateTime&&) = default;
135
136 /**
137 Constructor from date and time details
138 */
139 DateTime(int Year, int Month, int Day, int Hour, int Minute, int Second);
140
141 /**
142 Constructor from date only details
143 */
144 DateTime(int Year, int Month, int Day) : DateTime(Year,Month,Day,0,0,0)
145 { }
146
147 /**
148 Constructor
149 */
150 DateTime(RawTime_t SecondsSince0000);
151
152 DateTime& operator=(const DateTime& Other) = default;
153
154 DateTime& operator=(DateTime&& Other) = default;
155
156 /**
157 Destructor
158 */
159 ~DateTime() = default;
160
161 /**
162 Creates a DateTime object with current date and time
163 */
164 static DateTime now();
165
166 /**
167 Creates a DateTime object from a string
168 @param[in] DateTimeStr The date and time string
169 @param[in] FormatStr The format string, using strftime() style
170 */
171 static DateTime fromString(const std::string& DateTimeStr, const std::string& FormatStr);
172
173 /**
174 Creates a DateTime object from an ISO formatted string
175 @param[in] DateTimeStr The ISO formatted date and time string (YYYY-MM-DD hh:mm:ss)
176 */
177 static DateTime fromISOString(const std::string& DateTimeStr);
178
179 /**
180 Sets the date and time from the parameters
181 */
182 bool set(int Year, int Month, int Day, int Hour, int Minute, int Second);
183
184 /**
185 Sets the date and time from the number of seconds since first day of 4713BC
186 */
187 void set(const RawTime_t& SecondsSince0000);
188
189
190 /**
191 Sets the date and time from an ISO formatted string (YYYY-MM-DD hh:mm:ss)
192 @param[in] DateTimeStr The ISO formatted date and time string (YYYY-MM-DD hh:mm:ss)
193 @return true if the operation is successful
194 */
195 bool setFromISOString(const std::string& DateTimeStr);
196
197
198 /**
199 Sets the date and time from a string using the given format
200 @param[in] DateTimeStr The date and time string
201 @param[in] FormatStr The format string, using strftime() style
202 @return true if the operation is successful
203 */
204 bool setFromString(const std::string& DateTimeStr, const std::string& FormatStr);
205
206
207 /**
208 Returns Year (4 digits)
209 @return an int
210 */
211 int getYear() const
212 {
213 return (m_TM.tm_year+1900);
214 };
215
216 /**
217 Retourns Month [1-12]
218 @return an int
219 */
220 int getMonth() const
221 {
222 return (m_TM.tm_mon+1);
223 };
224
225 /**
226 Returns Day [1-31]
227 @return an int
228 */
229 int getDay() const
230 {
231 return m_TM.tm_mday;
232 };
233
234 /**
235 Returns Hour [0-23]
236 @return an int
237 */
238 int getHour() const
239 {
240 return m_TM.tm_hour;
241 };
242
243 /**
244 Returns Minute [0-59]
245 @return an int
246 */
247 int getMinute() const
248 {
249 return m_TM.tm_min;
250 };
251
252 /**
253 Returns Second [0-59]
254 @return an int
255 */
256 int getSecond() const
257 {
258 return m_TM.tm_sec;
259 };
260
261 /**
262 Returns date-time in raw format (number of seconds since first day of 4713 BC)
263 @return a rawtime_t
264 */
266
267
268 /**
269 Returns date-time as string, using format YYYT-MM-DD hh:mm:ss
270 @return a string
271 */
272 std::string getAsISOString() const; //FIXME It is not ISO format (should be YYYTMMDDThh:mm:ss)
273
274
275 /**
276 Returns date-time as string, using format YYYT-MM-DDThh:mm:ss
277 @return a string
278 */
279 std::string getAsISOEXTString() const;
280
281
282 /**
283 Returns date-time as string, using strftime() format string
284 @param[in] Format strftime()-like format string
285 @return a string
286 */
287 std::string getAsString(const std::string& Format) const;
288
289
290 /**
291 Returns date as string, using format YYYY-MM-DD
292 @return a string
293 */
294 std::string getDateAsISOString() const;
295
296 /**
297 Returns time as string, using format hh:mm:ss
298 @return a string
299 */
300 std::string getTimeAsISOString() const;
301
302
303 /**
304 Adds the given seconds to the current date and time
305 */
306 void addSeconds(const RawTime_t& Seconds);
307
308 /**
309 Subtracts the given seconds from the current date and time
310 */
311 void subtractSeconds(const RawTime_t& Seconds);
312
313 /**
314 Returns the difference in seconds between the date-time and the given date-time (Self - Given)
315 */
317
318 /**
319 Returns true if the date-time is between the two given date-time
320 */
321 bool isBetween(const DateTime& FirstDT, const DateTime& SecondDT);
322
323 /**
324 Returns true if the date-time is strictly between the two given date-time
325 */
326 bool isStrictlyBetween(const DateTime& FirstDT, const DateTime& SecondDT);
327
328 /**
329 Equal operator
330 */
331 bool operator==(const DateTime& Right) const;
332
333 /**
334 Difference operator
335 */
336 bool operator!=(const DateTime& Right) const;
337
338 /**
339 Greater than operator
340 */
341 bool operator>(const DateTime& Right) const;
342
343 /**
344 Greater than or equal operator
345 */
346 bool operator>=(const DateTime& Right) const;
347
348 /**
349 Lower than operator
350 */
351 bool operator<(const DateTime& Right) const;
352
353 /**
354 Lower than or equal operator
355 */
356 bool operator<=(const DateTime& Right) const;
357
358 /**
359 Add operator
360 */
361 DateTime operator+(const RawTime_t& Seconds) const;
362
363 /**
364 Subtract operator
365 */
366 DateTime operator-(const RawTime_t& Seconds) const;
367
368 /**
369 Returns the number of seconds for one minute
370 */
371 static inline RawTime_t Minute()
372 { return 60; }
373
374 /**
375 Returns the number of seconds for N minutes
376 */
377 static inline RawTime_t Minutes(int N)
378 { return (60*N); }
379
380 /**
381 Returns the number of seconds for one hour
382 */
383 static inline RawTime_t Hour()
384 { return 3600; }
385
386 /**
387 Returns the number of seconds for N hours
388 */
389 static inline RawTime_t Hours(int N)
390 { return (3600*N); }
391
392 /**
393 Returns the number of seconds for one day
394 */
395 static inline RawTime_t Day()
396 { return 86400; }
397
398 /**
399 Returns the number of seconds for N days
400 */
401 static inline RawTime_t Days(int N)
402 { return (86400*N); }
403
404 /**
405 Returns the number of seconds for one week
406 */
407 static inline RawTime_t Week()
408 { return 604800; }
409
410 /**
411 Returns the number of seconds for N weeks
412 */
413 static inline RawTime_t Weeks(int N)
414 { return (604800*N); }
415
416 /**
417 Returns true if the given year is a leap year
418 */
419 static bool isLeapYear(int Year);
420
421 /**
422 Returns the number of days in the given month for the given year
423 */
424 static int getNumOfDaysInMonth(int Year, int Month);
425
426 /**
427 Returns true if the given date time is valid
428 */
429 static bool isValidDateTime(int Year, int Month, int Day, int Hour, int Minute, int Second);
430
431 /**
432 Returns true if the given date is the same, without comparing time parts
433 */
434 bool isSameDate(const DateTime& DT) const;
435
436 /**
437 Returns true if the given time is the same, without comparing dates
438 */
439 bool isSameTime(const DateTime& DT) const;
440
441};
442
443
444} } // namespaces
445
446
447#endif /* __OPENFLUID_CORE_DATETIME_HPP__ */
448
Class for management of date and time information.
Definition DateTime.hpp:88
int getYear() const
Definition DateTime.hpp:211
static RawTime_t Minute()
Definition DateTime.hpp:371
std::string getDateAsISOString() const
bool operator>(const DateTime &Right) const
int getSecond() const
Definition DateTime.hpp:256
static RawTime_t Hour()
Definition DateTime.hpp:383
DateTime(DateTime &&)=default
std::string getAsISOString() const
bool setFromString(const std::string &DateTimeStr, const std::string &FormatStr)
DateTime operator+(const RawTime_t &Seconds) const
bool operator>=(const DateTime &Right) const
static DateTime now()
static RawTime_t Week()
Definition DateTime.hpp:407
DateTime & operator=(const DateTime &Other)=default
int getHour() const
Definition DateTime.hpp:238
bool operator!=(const DateTime &Right) const
bool operator<(const DateTime &Right) const
static RawTime_t Hours(int N)
Definition DateTime.hpp:389
void set(const RawTime_t &SecondsSince0000)
RawTime_t getRawTime() const
int getDay() const
Definition DateTime.hpp:229
int getMonth() const
Definition DateTime.hpp:220
bool isSameTime(const DateTime &DT) const
void addSeconds(const RawTime_t &Seconds)
static RawTime_t Minutes(int N)
Definition DateTime.hpp:377
static DateTime fromISOString(const std::string &DateTimeStr)
DateTime(int Year, int Month, int Day)
Definition DateTime.hpp:144
static int getNumOfDaysInMonth(int Year, int Month)
bool set(int Year, int Month, int Day, int Hour, int Minute, int Second)
static RawTime_t Day()
Definition DateTime.hpp:395
bool operator==(const DateTime &Right) const
bool setFromISOString(const std::string &DateTimeStr)
static DateTime fromString(const std::string &DateTimeStr, const std::string &FormatStr)
static bool isLeapYear(int Year)
DateTime(int Year, int Month, int Day, int Hour, int Minute, int Second)
static RawTime_t Days(int N)
Definition DateTime.hpp:401
std::string getAsString(const std::string &Format) const
void subtractSeconds(const RawTime_t &Seconds)
bool isStrictlyBetween(const DateTime &FirstDT, const DateTime &SecondDT)
int getMinute() const
Definition DateTime.hpp:247
std::string getAsISOEXTString() const
std::string getTimeAsISOString() const
DateTime(RawTime_t SecondsSince0000)
static RawTime_t Weeks(int N)
Definition DateTime.hpp:413
RawTime_t diffInSeconds(const DateTime &DT) const
DateTime operator-(const RawTime_t &Seconds) const
bool operator<=(const DateTime &Right) const
static bool isValidDateTime(int Year, int Month, int Day, int Hour, int Minute, int Second)
DateTime & operator=(DateTime &&Other)=default
bool isBetween(const DateTime &FirstDT, const DateTime &SecondDT)
bool isSameDate(const DateTime &DT) const
DateTime(const DateTime &)=default
#define OPENFLUID_API
Definition dllexport.hpp:86
std::uint64_t RawTime_t
Definition TypeDefs.hpp:284
Definition ApplicationException.hpp:47