00001 /* 00002 00003 This file is part of OpenFLUID software 00004 Copyright(c) 2007, INRA - Montpellier SupAgro 00005 00006 00007 == GNU General Public License Usage == 00008 00009 OpenFLUID is free software: you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation, either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 OpenFLUID is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>. 00021 00022 00023 == Other Usage == 00024 00025 Other Usage means a use of OpenFLUID that is inconsistent with the GPL 00026 license, and requires a written agreement between You and INRA. 00027 Licensees for Other Usage of OpenFLUID may use this file in accordance 00028 with the terms contained in the written agreement between You and INRA. 00029 00030 */ 00031 00032 00033 00034 /** 00035 @file 00036 00037 @author Jean-Christophe FABRE <fabrejc@supagro.inra.fr> 00038 */ 00039 00040 00041 #ifndef __DATETIME_HPP__ 00042 #define __DATETIME_HPP__ 00043 00044 00045 #include <sys/types.h> 00046 #include <time.h> 00047 #include <string> 00048 00049 #include <openfluid/dllexport.hpp> 00050 00051 00052 namespace openfluid { namespace core { 00053 00054 /** 00055 Type for raw time (seconds since 4713BC) 00056 */ 00057 typedef unsigned long long RawTime_t; 00058 00059 /** 00060 Type for time indexes (in seconds) 00061 */ 00062 typedef unsigned long long TimeIndex_t; 00063 00064 00065 /** 00066 Type for durations (in seconds) 00067 */ 00068 typedef unsigned long long Duration_t; 00069 00070 00071 /** 00072 @brief Class for management of date and time information. 00073 00074 Class for management of date and time information. It includes arithmetics and comparisons operations. 00075 00076 Sources: 00077 @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 00078 @li http://en.wikipedia.org/wiki/Julian_day 00079 00080 <BR> 00081 00082 <I>Example : creating a date</I> 00083 @code 00084 openfluid::core::DateTime aDate(2009,01,25,12,05,00); 00085 @endcode 00086 00087 00088 <I>Example : adding time span to a date</I> 00089 @code 00090 // adding 45 seconds 00091 aDate.addSeconds(45); 00092 00093 // adding 5 minutes 00094 aDate.addSeconds(openfluid::core::DateTime::Minutes(5)); 00095 00096 // adding an hour 00097 aDate.addSeconds(openfluid::core::DateTime::Hour()); 00098 00099 // adding 60 days 00100 aDate.addSeconds(openfluid::core::DateTime::Days(60)); 00101 @endcode 00102 00103 00104 <I>Example : subtracting time span to a date</I> 00105 @code 00106 // subtracting 10 seconds 00107 aDate.subtractSeconds(10); 00108 00109 // subtracting 30 days 00110 aDate.subtractSeconds(openfluid::core::DateTime::Days(30)); 00111 @endcode 00112 00113 00114 <I>Example : getting difference in seconds between two dates</I> 00115 @code 00116 openfluid::core::DateTime FirstDate(2009,01,25,12,05,00); 00117 openfluid::core::DateTime SecondDate(2009,02,28,00,00,00); 00118 00119 openfluid::core::RawTime_t Diff; 00120 00121 Diff = SecondDate - FirstDate; 00122 00123 if (Diff > openfluid::core::DateTime::Days(60)) 00124 std::cout << "The difference between the two dates is greater than 60 days" << std::end; 00125 else 00126 std::cout << "The difference between the two dates is lesser than 60 days" << std::end; 00127 @endcode 00128 00129 00130 */ 00131 class DLLEXPORT DateTime 00132 { 00133 private: 00134 00135 /** 00136 The tm struct contains: 00137 int tm_sec; 00138 int tm_min; 00139 int tm_hour; 00140 int tm_mday; 00141 int tm_mon; 00142 int tm_year; 00143 int tm_wday; 00144 int tm_yday; 00145 int tm_isdst 00146 */ 00147 00148 /** 00149 The date and time stored as broken down structure 00150 */ 00151 struct tm m_TM; 00152 00153 /** 00154 The date and time stored as number of seconds since 4713BC 00155 */ 00156 RawTime_t m_RawTime; 00157 00158 void updateYMDHMSFromRawTime(); 00159 00160 void updateRawTimeFromYMDHMS(); 00161 00162 00163 public: 00164 00165 /** 00166 Default constructor 00167 */ 00168 DateTime(); 00169 00170 /** 00171 Constructor 00172 */ 00173 DateTime(int Year, int Month, int Day, int Hour, int Minute, int Second); 00174 00175 /** 00176 Constructor 00177 */ 00178 DateTime(RawTime_t SecondsSince0000); 00179 00180 /** 00181 Destructor 00182 */ 00183 ~DateTime(); 00184 00185 /** 00186 Sets the date and time from the parameters 00187 */ 00188 bool set(int Year, int Month, int Day, int Hour, int Minute, int Second); 00189 00190 /** 00191 Sets the date and time from the number of seconds since first day of 4713BC 00192 */ 00193 00194 void set(const RawTime_t& SecondsSince0000); 00195 00196 00197 /** 00198 Sets the date and time from an ISO formatted string (YYYY-MM-DD hh:mm:ss) 00199 */ 00200 bool setFromISOString(const std::string& DateTimeStr); 00201 00202 00203 /** 00204 Sets the date and time from a string using the given format 00205 */ 00206 bool setFromString(const std::string& DateTimeStr, const std::string& FormatStr); 00207 00208 00209 /** 00210 Returns Year (4 digits) 00211 @return an int 00212 */ 00213 int getYear() const {return (m_TM.tm_year+1900); }; 00214 00215 /** 00216 Retourns Month [1-12] 00217 @return an int 00218 */ 00219 int getMonth() const {return (m_TM.tm_mon+1) ;}; 00220 00221 /** 00222 Returns Day [1-31] 00223 @return an int 00224 */ 00225 int getDay() const {return m_TM.tm_mday;}; 00226 00227 /** 00228 Returns Hour [0-23] 00229 @return an int 00230 */ 00231 int getHour() const {return m_TM.tm_hour;}; 00232 00233 /** 00234 Returns Minute [0-59] 00235 @return an int 00236 */ 00237 int getMinute() const {return m_TM.tm_min;}; 00238 00239 /** 00240 Returns Second [0-59] 00241 @return an int 00242 */ 00243 int getSecond() const {return m_TM.tm_sec;}; 00244 00245 /** 00246 Returns date-time in raw format (number of seconds since first day of 4713 BC) 00247 @return a rawtime_t 00248 */ 00249 RawTime_t getRawTime() const; 00250 00251 00252 /** 00253 Returns date-time as string, using format YYYT-MM-DD hh:mm:ss 00254 @return a string 00255 */ 00256 std::string getAsISOString() const; 00257 00258 /** 00259 Returns date-time as string, using strftime() format string 00260 @param[in] Format strftime()-like format string 00261 @return a string 00262 */ 00263 std::string getAsString(std::string Format) const; 00264 00265 00266 /** 00267 Returns date as string, using format YYYY-MM-DD 00268 @return a string 00269 */ 00270 std::string getDateAsISOString() const; 00271 00272 /** 00273 Returns time as string, using format hh:mm:ss 00274 @return a string 00275 */ 00276 std::string getTimeAsISOString() const; 00277 00278 00279 /** 00280 Adds the given seconds to the current date and time 00281 */ 00282 void addSeconds(const RawTime_t& Seconds); 00283 00284 /** 00285 Subtracts the given seconds from the current date and time 00286 */ 00287 void subtractSeconds(const RawTime_t& Seconds); 00288 00289 /** 00290 Returns the difference in seconds between the date-time and the given date-time (Self - Given) 00291 */ 00292 RawTime_t diffInSeconds(const DateTime& DT) const; 00293 00294 /** 00295 Returns true if the date-time is between the two given date-time 00296 */ 00297 bool isBetween(const DateTime& FirstDT, const DateTime& SecondDT); 00298 00299 /** 00300 Returns true if the date-time is strictly between the two given date-time 00301 */ 00302 bool isStrictlyBetween(const DateTime& FirstDT, const DateTime& SecondDT); 00303 00304 00305 /** 00306 Assignment operator 00307 */ 00308 DateTime& operator =(const DateTime &Right); 00309 00310 /** 00311 Equal operator 00312 */ 00313 bool operator ==(const DateTime &Right) const; 00314 00315 /** 00316 Difference operator 00317 */ 00318 bool operator !=(const DateTime &Right) const; 00319 00320 /** 00321 Greater than operator 00322 */ 00323 bool operator >(const DateTime &Right) const; 00324 00325 /** 00326 Greater than or equal operator 00327 */ 00328 bool operator >=(const DateTime &Right) const; 00329 00330 /** 00331 Lower than operator 00332 */ 00333 bool operator <(const DateTime &Right) const; 00334 00335 /** 00336 Lower than or equal operator 00337 */ 00338 bool operator <=(const DateTime &Right) const; 00339 00340 /** 00341 Add operator 00342 */ 00343 DateTime operator +(const RawTime_t& Seconds) const; 00344 00345 /** 00346 Subtract operator 00347 */ 00348 DateTime operator -(const RawTime_t& Seconds) const; 00349 00350 /** 00351 Returns the number of seconds for one minute 00352 */ 00353 static inline RawTime_t Minute() { return 60; }; 00354 00355 /** 00356 Returns the number of seconds for N minutes 00357 */ 00358 static inline RawTime_t Minutes(int N) { return (60*N); }; 00359 00360 /** 00361 Returns the number of seconds for one hour 00362 */ 00363 static inline RawTime_t Hour() { return 3600; }; 00364 00365 /** 00366 Returns the number of seconds for N hours 00367 */ 00368 static inline RawTime_t Hours(int N) { return (3600*N); }; 00369 00370 /** 00371 Returns the number of seconds for one day 00372 */ 00373 static inline RawTime_t Day() { return 86400; }; 00374 00375 /** 00376 Returns the number of seconds for N days 00377 */ 00378 static inline RawTime_t Days(int N) { return (86400*N); }; 00379 00380 /** 00381 Returns the number of seconds for one week 00382 */ 00383 static inline RawTime_t Week() { return 604800; }; 00384 00385 /** 00386 Returns the number of seconds for N weeks 00387 */ 00388 static inline RawTime_t Weeks(int N) { return (604800*N); }; 00389 00390 /** 00391 Returns true if the given year is a leap year 00392 */ 00393 static bool isLeapYear(int Year); 00394 00395 /** 00396 Returns the number of days in the given month for the given year 00397 */ 00398 static int getNumOfDaysInMonth(int Year, int Month); 00399 00400 /** 00401 Returns true if the given date time is valid 00402 */ 00403 static bool isValidDateTime(int Year, int Month, int Day, int Hour, int Minute, int Second); 00404 00405 /** 00406 Returns true if the given date is the same, without comparing time parts 00407 */ 00408 bool isSameDate(const DateTime& DT) const; 00409 00410 /** 00411 Returns true if the given time is the same, without comparing dates 00412 */ 00413 bool isSameTime(const DateTime& DT) const; 00414 00415 }; 00416 00417 00418 00419 } } // namespaces 00420 00421 #endif 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432