ML Reference
|
00001 // **InsertLicense** code 00002 //---------------------------------------------------------------------------------- 00005 00010 //---------------------------------------------------------------------------------- 00011 #ifndef __mlDateTime_H 00012 #define __mlDateTime_H 00013 00014 //ML-includes 00015 #ifndef __mlUtilsSystem_H 00016 #include "mlUtilsSystem.h" 00017 #endif 00018 #ifndef __mlSystemIncludes_H 00019 #include "mlSystemIncludes.h" 00020 #endif 00021 00022 00023 ML_UTILS_START_NAMESPACE 00024 00025 00026 //---------------------------------------------------------------------------------- 00029 00120 //---------------------------------------------------------------------------------- 00121 class ML_UTILS_EXPORT DateTime 00122 { 00123 00124 public: 00125 00127 typedef MLint32 DateTimeIntType; 00129 typedef MLdouble DateTimeFloatType; 00130 00131 00132 //------------------------------------------------------ 00134 00135 //------------------------------------------------------ 00136 00137 // Constructors 00138 00139 DateTime(); 00140 DateTime(const DateTime &dt); 00141 DateTime(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV, 00142 DateTimeIntType hourV = 0, DateTimeIntType minuteV = 0, DateTimeFloatType secondV = 0); 00143 DateTime(const char* dtString, const char* format = 0); 00144 DateTime(DateTimeIntType linearDaysV, DateTimeFloatType linearSecondsV); 00145 00147 00148 00149 //------------------------------------------------------ 00151 00152 //------------------------------------------------------ 00153 00154 // All set methods return \c true if given date is valid. 00155 // Invalid dates are converted to valid dates, so the 00156 // return value may be ignored. 00157 00158 DateTime &operator = (const DateTime &dt); 00159 00160 // Sets both date and time portion. 00161 bool set(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV, 00162 DateTimeIntType hourV = 0, DateTimeIntType minuteV = 0, DateTimeFloatType secondV = 0); 00163 bool set(const char *dtString, const char *format = 0); 00164 bool set(DateTimeIntType linDaysV, DateTimeFloatType linSecondsV); 00165 00166 // Sets date and time portion independently. 00167 bool setYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV); 00168 bool setHMS(DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV); 00169 00170 // Sets to current date/time. 00171 DateTime &now(); 00172 00174 00175 00176 //------------------------------------------------------ 00178 00179 //------------------------------------------------------ 00180 00181 void get(DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV, 00182 DateTimeIntType *hourV = 0, DateTimeIntType *minuteV = 0, DateTimeFloatType *secondV = 0) const; 00183 void get(char *dtString, const char *format = 0) const; 00184 00185 void getYMD(DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV) const; 00186 void getHMS(DateTimeIntType *hourV, DateTimeIntType *minuteV, DateTimeFloatType *secondV) const; 00187 00188 DateTimeIntType year() const; 00189 DateTimeIntType month() const; 00190 DateTimeIntType day() const; 00191 DateTimeIntType hour() const; 00192 DateTimeIntType minute() const; 00193 DateTimeFloatType second() const; 00194 00195 // Get day within year in the range 0..364(365) 00196 DateTimeIntType dayInYear() const; 00197 00198 // Get linear day and seconds counters. 00199 inline DateTimeIntType linearDays() const { return _ldays; } 00200 inline DateTimeFloatType linearSeconds() const { return _lseconds; } 00201 00202 // Test for zero date/time. 00203 // Returns NULL if date is zero. Returned pointer is not intended to be referenced. 00204 operator void *() const; // Return NULL if date is zero 00205 bool operator !() const; // Return true if date is zero 00206 00208 00209 00210 //------------------------------------------------------ 00212 00213 //------------------------------------------------------ 00214 00215 // Adds given number of days (subtract if days < 0). 00216 void addDays(DateTimeIntType daysV); 00217 00218 // Adds given number of days (subtract if days < 0). 00219 // Fractions of a day are allowed. 00220 void addDays(DateTimeFloatType daysV); 00221 00222 // Adds given number of seconds (subtract if seconds < 0). 00223 void addSeconds(DateTimeFloatType secondsV); 00224 00225 // Computes time passed since time point \p dateTime, giving the result 00226 // in days and fractions of a day. 00227 DateTimeFloatType daysSince(const DateTime &dateTime) const; 00228 00229 // Computes time passed since time point \p dateTime, giving the result 00230 // in seconds and fractions of a second. 00231 DateTimeFloatType secondsSince(const DateTime &dateTime) const; 00232 00234 00235 00236 //------------------------------------------------------ 00238 00239 //------------------------------------------------------ 00240 00241 bool operator == (const DateTime &dt) const; 00242 bool operator != (const DateTime &dt) const; 00243 bool operator < (const DateTime &dt) const; 00244 bool operator <= (const DateTime &dt) const; 00245 bool operator >= (const DateTime &dt) const; 00246 bool operator > (const DateTime &dt) const; 00247 00249 00250 00251 //------------------------------------------------------ 00253 00254 //------------------------------------------------------ 00255 00256 // Tests date/time specification for validity. 00257 static bool validYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV); 00258 static bool validHMS(DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV); 00259 00260 // Returns number of days for given month. 00261 static DateTimeIntType daysOfMonth(DateTimeIntType monthV, DateTimeIntType yearV); 00262 00263 // Returns number of days for given year. 00264 static DateTimeIntType daysOfYear(DateTimeIntType yearV); 00265 00266 // Returns \c true if given year is a leap year. 00267 static bool isLeapYear(DateTimeIntType yearV); 00268 00269 // Returns number of days for current month. 00270 DateTimeIntType daysOfMonth() const; 00271 00272 // Returns number of days for current year. 00273 DateTimeIntType daysOfYear() const; 00274 00275 // Returns \c true if current year is a leap year. 00276 bool isLeapYear() const; 00277 00279 00280 protected: 00281 00282 00283 //------------------------------------------------------ 00285 00286 //------------------------------------------------------ 00287 00288 // Make sure seconds counter is in the range 0..seconds_per_day 00289 void normalize(); 00290 00292 00293 00294 private: 00295 00296 00297 //------------------------------------------------------ 00299 00300 //------------------------------------------------------ 00301 00302 DateTimeIntType _ldays; 00303 DateTimeFloatType _lseconds; 00304 00306 00307 00308 #ifdef ML_DEPRECATED 00309 00311 00312 00313 public: 00314 00317 typedef MLint32 DTIType; 00320 typedef MLdouble DTFType; 00323 inline ML_DEPRECATED DateTimeIntType linDays() const { return linearDays(); } 00326 inline ML_DEPRECATED DateTimeFloatType linSeconds() const { return linearSeconds(); } 00327 00329 00330 #endif // ML_DEPRECATED 00331 00332 }; 00333 00334 ML_UTILS_END_NAMESPACE 00335 00336 00337 //----------------------------------------------------------------------------------- 00338 // Stream output for std::ostream 00339 //----------------------------------------------------------------------------------- 00340 namespace std { 00341 00342 //----------------------------------------------------------------------------------- 00343 // Stream output for std::ostream 00344 //----------------------------------------------------------------------------------- 00347 inline ostream& operator<<(ostream& s, const ML_UTILS_NAMESPACE::DateTime &dt) 00348 { 00349 s << dt.year() << ":" 00350 << dt.month() << ":" 00351 << dt.day() << ":" 00352 << dt.hour() << ":" 00353 << dt.minute() << ":" 00354 << dt.second(); 00355 return s; 00356 } 00357 } 00358 00359 #endif // __mlDateTime_H 00360 00361 00362 00363 00364