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  <BR>
66 
67  <I>Example : creating a date</I>
68  @code
69  openfluid::core::DateTime aDate(2009,01,25,12,05,00);
70  @endcode
71 
72 
73  <I>Example : adding time span to a date</I>
74  @code
75  // adding 45 seconds
76  aDate.addSeconds(45);
77 
78  // adding 5 minutes
79  aDate.addSeconds(openfluid::core::DateTime::Minutes(5));
80 
81  // adding an hour
82  aDate.addSeconds(openfluid::core::DateTime::Hour());
83 
84  // adding 60 days
85  aDate.addSeconds(openfluid::core::DateTime::Days(60));
86  @endcode
87 
88 
89  <I>Example : subtracting time span to a date</I>
90  @code
91  // subtracting 10 seconds
92  aDate.subtractSeconds(10);
93 
94  // subtracting 30 days
95  aDate.subtractSeconds(openfluid::core::DateTime::Days(30));
96  @endcode
97 
98 
99  <I>Example : getting difference in seconds between two dates</I>
100  @code
101  openfluid::core::DateTime FirstDate(2009,01,25,12,05,00);
102  openfluid::core::DateTime SecondDate(2009,02,28,00,00,00);
103 
104  openfluid::core::RawTime_t Diff;
105 
106  Diff = SecondDate - FirstDate;
107 
108  if (Diff > openfluid::core::DateTime::Days(60))
109  std::cout << "The difference between the two dates is greater than 60 days" << std::end;
110  else
111  std::cout << "The difference between the two dates is lesser than 60 days" << std::end;
112  @endcode
113 
114 
115 */
117 {
118  private:
119 
120  /**
121  The tm struct contains:
122  int tm_sec;
123  int tm_min;
124  int tm_hour;
125  int tm_mday;
126  int tm_mon;
127  int tm_year;
128  int tm_wday;
129  int tm_yday;
130  int tm_isdst
131  */
132 
133  /**
134  The date and time stored as broken down structure
135  */
136  struct tm m_TM;
137 
138  /**
139  The date and time stored as number of seconds since 4713BC
140  */
141  RawTime_t m_RawTime;
142 
143  void updateYMDHMSFromRawTime();
144 
145  void updateRawTimeFromYMDHMS();
146 
147 
148  public:
149 
150  /**
151  Default constructor
152  */
153  DateTime();
154 
155  /**
156  Constructor
157  */
158  DateTime(int Year, int Month, int Day, int Hour, int Minute, int Second);
159 
160  /**
161  Constructor
162  */
163  DateTime(RawTime_t SecondsSince0000);
164 
165  /**
166  Destructor
167  */
168  ~DateTime();
169 
170  /**
171  Sets the date and time from the parameters
172  */
173  bool set(int Year, int Month, int Day, int Hour, int Minute, int Second);
174 
175  /**
176  Sets the date and time from the number of seconds since first day of 4713BC
177  */
178 
179  void set(const RawTime_t& SecondsSince0000);
180 
181 
182  /**
183  Sets the date and time from an ISO formatted string (YYYY-MM-DD hh:mm:ss)
184  */
185  bool setFromISOString(const std::string& DateTimeStr);
186 
187 
188  /**
189  Sets the date and time from a string using the given format
190  */
191  bool setFromString(const std::string& DateTimeStr, const std::string& FormatStr);
192 
193 
194  /**
195  Returns Year (4 digits)
196  @return an int
197  */
198  int getYear() const
199  {
200  return (m_TM.tm_year+1900);
201  };
202 
203  /**
204  Retourns Month [1-12]
205  @return an int
206  */
207  int getMonth() const
208  {
209  return (m_TM.tm_mon+1);
210  };
211 
212  /**
213  Returns Day [1-31]
214  @return an int
215  */
216  int getDay() const
217  {
218  return m_TM.tm_mday;
219  };
220 
221  /**
222  Returns Hour [0-23]
223  @return an int
224  */
225  int getHour() const
226  {
227  return m_TM.tm_hour;
228  };
229 
230  /**
231  Returns Minute [0-59]
232  @return an int
233  */
234  int getMinute() const
235  {
236  return m_TM.tm_min;
237  };
238 
239  /**
240  Returns Second [0-59]
241  @return an int
242  */
243  int getSecond() const
244  {
245  return m_TM.tm_sec;
246  };
247 
248  /**
249  Returns date-time in raw format (number of seconds since first day of 4713 BC)
250  @return a rawtime_t
251  */
252  RawTime_t getRawTime() const;
253 
254 
255  /**
256  Returns date-time as string, using format YYYT-MM-DD hh:mm:ss
257  @return a string
258  */
259  std::string getAsISOString() const;
260 
261  /**
262  Returns date-time as string, using strftime() format string
263  @param[in] Format strftime()-like format string
264  @return a string
265  */
266  std::string getAsString(std::string Format) const;
267 
268 
269  /**
270  Returns date as string, using format YYYY-MM-DD
271  @return a string
272  */
273  std::string getDateAsISOString() const;
274 
275  /**
276  Returns time as string, using format hh:mm:ss
277  @return a string
278  */
279  std::string getTimeAsISOString() const;
280 
281 
282  /**
283  Adds the given seconds to the current date and time
284  */
285  void addSeconds(const RawTime_t& Seconds);
286 
287  /**
288  Subtracts the given seconds from the current date and time
289  */
290  void subtractSeconds(const RawTime_t& Seconds);
291 
292  /**
293  Returns the difference in seconds between the date-time and the given date-time (Self - Given)
294  */
295  RawTime_t diffInSeconds(const DateTime& DT) const;
296 
297  /**
298  Returns true if the date-time is between the two given date-time
299  */
300  bool isBetween(const DateTime& FirstDT, const DateTime& SecondDT);
301 
302  /**
303  Returns true if the date-time is strictly between the two given date-time
304  */
305  bool isStrictlyBetween(const DateTime& FirstDT, const DateTime& SecondDT);
306 
307  /**
308  Assignment operator
309  */
310  DateTime& operator =(const DateTime &Right);
311 
312  /**
313  Equal operator
314  */
315  bool operator ==(const DateTime &Right) const;
316 
317  /**
318  Difference operator
319  */
320  bool operator !=(const DateTime &Right) const;
321 
322  /**
323  Greater than operator
324  */
325  bool operator >(const DateTime &Right) const;
326 
327  /**
328  Greater than or equal operator
329  */
330  bool operator >=(const DateTime &Right) const;
331 
332  /**
333  Lower than operator
334  */
335  bool operator <(const DateTime &Right) const;
336 
337  /**
338  Lower than or equal operator
339  */
340  bool operator <=(const DateTime &Right) const;
341 
342  /**
343  Add operator
344  */
345  DateTime operator +(const RawTime_t& Seconds) const;
346 
347  /**
348  Subtract operator
349  */
350  DateTime operator -(const RawTime_t& Seconds) const;
351 
352  /**
353  Returns the number of seconds for one minute
354  */
355  static inline RawTime_t Minute() { return 60; };
356 
357  /**
358  Returns the number of seconds for N minutes
359  */
360  static inline RawTime_t Minutes(int N) { return (60*N); };
361 
362  /**
363  Returns the number of seconds for one hour
364  */
365  static inline RawTime_t Hour() { return 3600; };
366 
367  /**
368  Returns the number of seconds for N hours
369  */
370  static inline RawTime_t Hours(int N) { return (3600*N); };
371 
372  /**
373  Returns the number of seconds for one day
374  */
375  static inline RawTime_t Day() { return 86400; };
376 
377  /**
378  Returns the number of seconds for N days
379  */
380  static inline RawTime_t Days(int N) { return (86400*N); };
381 
382  /**
383  Returns the number of seconds for one week
384  */
385  static inline RawTime_t Week() { return 604800; };
386 
387  /**
388  Returns the number of seconds for N weeks
389  */
390  static inline RawTime_t Weeks(int N) { return (604800*N); };
391 
392  /**
393  Returns true if the given year is a leap year
394  */
395  static bool isLeapYear(int Year);
396 
397  /**
398  Returns the number of days in the given month for the given year
399  */
400  static int getNumOfDaysInMonth(int Year, int Month);
401 
402  /**
403  Returns true if the given date time is valid
404  */
405  static bool isValidDateTime(int Year, int Month, int Day, int Hour, int Minute, int Second);
406 
407  /**
408  Returns true if the given date is the same, without comparing time parts
409  */
410  bool isSameDate(const DateTime& DT) const;
411 
412  /**
413  Returns true if the given time is the same, without comparing dates
414  */
415  bool isSameTime(const DateTime& DT) const;
416 
417 };
418 
419 
420 } } // namespaces
421 
422 
423 #endif
424 
Class for management of date and time information.
Definition: DateTime.hpp:116
static RawTime_t Hours(int N)
Definition: DateTime.hpp:370
static RawTime_t Minute()
Definition: DateTime.hpp:355
int getSecond() const
Definition: DateTime.hpp:243
int getDay() const
Definition: DateTime.hpp:216
int getMonth() const
Definition: DateTime.hpp:207
static RawTime_t Weeks(int N)
Definition: DateTime.hpp:390
static RawTime_t Hour()
Definition: DateTime.hpp:365
int getMinute() const
Definition: DateTime.hpp:234
int getYear() const
Definition: DateTime.hpp:198
static RawTime_t Day()
Definition: DateTime.hpp:375
static RawTime_t Week()
Definition: DateTime.hpp:385
Definition: ApplicationException.hpp:47
int getHour() const
Definition: DateTime.hpp:225
static RawTime_t Days(int N)
Definition: DateTime.hpp:380
std::uint64_t RawTime_t
Definition: TypeDefs.hpp:176
static RawTime_t Minutes(int N)
Definition: DateTime.hpp:360
#define OPENFLUID_API
Definition: dllexport.hpp:86