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