Manual for OpenFLUID 2.1.11

DateTime.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
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 
48 #include <openfluid/dllexport.hpp>
49 #include <openfluid/core/TypeDefs.hpp>
50 
51 
52 namespace 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" : ["Compute code", "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  */
124  DateTime();
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 details
138  */
139  DateTime(int Year, int Month, int Day, int Hour, int Minute, int Second);
140 
141  /**
142  Constructor
143  */
144  DateTime(RawTime_t SecondsSince0000);
145 
146  DateTime& operator=(const DateTime& Other) = default;
147 
148  DateTime& operator=(DateTime&& Other) = default;
149 
150  /**
151  Destructor
152  */
153  ~DateTime() = default;
154 
155  /**
156  Sets the date and time from the parameters
157  */
158  bool set(int Year, int Month, int Day, int Hour, int Minute, int Second);
159 
160  /**
161  Sets the date and time from the number of seconds since first day of 4713BC
162  */
163 
164  void set(const RawTime_t& SecondsSince0000);
165 
166 
167  /**
168  Sets the date and time from an ISO formatted string (YYYY-MM-DD hh:mm:ss)
169  */
170  bool setFromISOString(const std::string& DateTimeStr);
171 
172 
173  /**
174  Sets the date and time from a string using the given format
175  */
176  bool setFromString(const std::string& DateTimeStr, const std::string& FormatStr);
177 
178 
179  /**
180  Returns Year (4 digits)
181  @return an int
182  */
183  int getYear() const
184  {
185  return (m_TM.tm_year+1900);
186  };
187 
188  /**
189  Retourns Month [1-12]
190  @return an int
191  */
192  int getMonth() const
193  {
194  return (m_TM.tm_mon+1);
195  };
196 
197  /**
198  Returns Day [1-31]
199  @return an int
200  */
201  int getDay() const
202  {
203  return m_TM.tm_mday;
204  };
205 
206  /**
207  Returns Hour [0-23]
208  @return an int
209  */
210  int getHour() const
211  {
212  return m_TM.tm_hour;
213  };
214 
215  /**
216  Returns Minute [0-59]
217  @return an int
218  */
219  int getMinute() const
220  {
221  return m_TM.tm_min;
222  };
223 
224  /**
225  Returns Second [0-59]
226  @return an int
227  */
228  int getSecond() const
229  {
230  return m_TM.tm_sec;
231  };
232 
233  /**
234  Returns date-time in raw format (number of seconds since first day of 4713 BC)
235  @return a rawtime_t
236  */
237  RawTime_t getRawTime() const;
238 
239 
240  /**
241  Returns date-time as string, using format YYYT-MM-DD hh:mm:ss
242  @return a string
243  */
244  std::string getAsISOString() const;
245 
246  /**
247  Returns date-time as string, using strftime() format string
248  @param[in] Format strftime()-like format string
249  @return a string
250  */
251  std::string getAsString(const std::string& Format) const;
252 
253 
254  /**
255  Returns date as string, using format YYYY-MM-DD
256  @return a string
257  */
258  std::string getDateAsISOString() const;
259 
260  /**
261  Returns time as string, using format hh:mm:ss
262  @return a string
263  */
264  std::string getTimeAsISOString() const;
265 
266 
267  /**
268  Adds the given seconds to the current date and time
269  */
270  void addSeconds(const RawTime_t& Seconds);
271 
272  /**
273  Subtracts the given seconds from the current date and time
274  */
275  void subtractSeconds(const RawTime_t& Seconds);
276 
277  /**
278  Returns the difference in seconds between the date-time and the given date-time (Self - Given)
279  */
280  RawTime_t diffInSeconds(const DateTime& DT) const;
281 
282  /**
283  Returns true if the date-time is between the two given date-time
284  */
285  bool isBetween(const DateTime& FirstDT, const DateTime& SecondDT);
286 
287  /**
288  Returns true if the date-time is strictly between the two given date-time
289  */
290  bool isStrictlyBetween(const DateTime& FirstDT, const DateTime& SecondDT);
291 
292  /**
293  Equal operator
294  */
295  bool operator==(const DateTime& Right) const;
296 
297  /**
298  Difference operator
299  */
300  bool operator!=(const DateTime& Right) const;
301 
302  /**
303  Greater than operator
304  */
305  bool operator>(const DateTime& Right) const;
306 
307  /**
308  Greater than or equal operator
309  */
310  bool operator>=(const DateTime& Right) const;
311 
312  /**
313  Lower than operator
314  */
315  bool operator<(const DateTime& Right) const;
316 
317  /**
318  Lower than or equal operator
319  */
320  bool operator<=(const DateTime& Right) const;
321 
322  /**
323  Add operator
324  */
325  DateTime operator+(const RawTime_t& Seconds) const;
326 
327  /**
328  Subtract operator
329  */
330  DateTime operator-(const RawTime_t& Seconds) const;
331 
332  /**
333  Returns the number of seconds for one minute
334  */
335  static inline RawTime_t Minute()
336  { return 60; };
337 
338  /**
339  Returns the number of seconds for N minutes
340  */
341  static inline RawTime_t Minutes(int N)
342  { return (60*N); };
343 
344  /**
345  Returns the number of seconds for one hour
346  */
347  static inline RawTime_t Hour()
348  { return 3600; };
349 
350  /**
351  Returns the number of seconds for N hours
352  */
353  static inline RawTime_t Hours(int N)
354  { return (3600*N); };
355 
356  /**
357  Returns the number of seconds for one day
358  */
359  static inline RawTime_t Day()
360  { return 86400; };
361 
362  /**
363  Returns the number of seconds for N days
364  */
365  static inline RawTime_t Days(int N)
366  { return (86400*N); };
367 
368  /**
369  Returns the number of seconds for one week
370  */
371  static inline RawTime_t Week()
372  { return 604800; };
373 
374  /**
375  Returns the number of seconds for N weeks
376  */
377  static inline RawTime_t Weeks(int N)
378  { return (604800*N); };
379 
380  /**
381  Returns true if the given year is a leap year
382  */
383  static bool isLeapYear(int Year);
384 
385  /**
386  Returns the number of days in the given month for the given year
387  */
388  static int getNumOfDaysInMonth(int Year, int Month);
389 
390  /**
391  Returns true if the given date time is valid
392  */
393  static bool isValidDateTime(int Year, int Month, int Day, int Hour, int Minute, int Second);
394 
395  /**
396  Returns true if the given date is the same, without comparing time parts
397  */
398  bool isSameDate(const DateTime& DT) const;
399 
400  /**
401  Returns true if the given time is the same, without comparing dates
402  */
403  bool isSameTime(const DateTime& DT) const;
404 
405 };
406 
407 
408 } } // namespaces
409 
410 
411 #endif /* __OPENFLUID_CORE_DATETIME_HPP__ */
412 
openfluid::core::DateTime::getYear
int getYear() const
Definition: DateTime.hpp:183
OPENFLUID_API
#define OPENFLUID_API
Definition: dllexport.hpp:86
openfluid::core::DateTime::Day
static RawTime_t Day()
Definition: DateTime.hpp:359
openfluid::core::DateTime::Days
static RawTime_t Days(int N)
Definition: DateTime.hpp:365
openfluid::core::DateTime::getDay
int getDay() const
Definition: DateTime.hpp:201
openfluid::core::DateTime::Minutes
static RawTime_t Minutes(int N)
Definition: DateTime.hpp:341
openfluid::core::DateTime::getMonth
int getMonth() const
Definition: DateTime.hpp:192
openfluid::core::DateTime::Hour
static RawTime_t Hour()
Definition: DateTime.hpp:347
openfluid::core::DateTime::Minute
static RawTime_t Minute()
Definition: DateTime.hpp:335
openfluid::core::DateTime::getMinute
int getMinute() const
Definition: DateTime.hpp:219
openfluid
Definition: ApplicationException.hpp:47
openfluid::core::DateTime::getSecond
int getSecond() const
Definition: DateTime.hpp:228
openfluid::core::DateTime::Hours
static RawTime_t Hours(int N)
Definition: DateTime.hpp:353
openfluid::core::RawTime_t
std::uint64_t RawTime_t
Definition: TypeDefs.hpp:284
openfluid::core::DateTime::getHour
int getHour() const
Definition: DateTime.hpp:210
openfluid::core::DateTime::Weeks
static RawTime_t Weeks(int N)
Definition: DateTime.hpp:377
openfluid::core::DateTime
Class for management of date and time information.
Definition: DateTime.hpp:87
dllexport.hpp
openfluid::core::DateTime::Week
static RawTime_t Week()
Definition: DateTime.hpp:371