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