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