00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _MPCL_TEXT_DATE__
00029 #define _MPCL_TEXT_DATE__
00030
00031 #include <algorithm>
00032 #include "../exceptions.hh"
00033 #include "string.hh"
00034
00035
00037 namespace mpcl
00038 {
00039
00041 namespace text
00042 {
00043
00045 class TTimeInterval
00046 {
00047
00048 private:
00049
00051 double dInterval;
00052
00053
00054 public:
00055
00056
00057
00058
00059
00061 TTimeInterval (void) :
00062 dInterval (0.0) {}
00063
00065 TTimeInterval (double dINTERVAL) :
00066 dInterval (dINTERVAL) {}
00067
00068
00069 public:
00070
00071
00072
00073
00074
00079 double seconds (void) { return dInterval; }
00080
00085 double minutes (void) { return dInterval / 60; }
00086
00091 double hours (void) { return dInterval / 3600; }
00092
00097 double days (void) { return dInterval / 86400; }
00098
00099 };
00100
00101
00103 class IDate
00104 {
00105
00106 protected:
00107
00109 static const int _kiMaxFormattedDate = 517;
00110
00112 time_t tSecondsFromEpoch;
00113
00114
00115 public:
00116
00118 enum EDateType { eEmpty, eNow, eTomorrow };
00119
00120
00121 public:
00122
00123
00124
00125
00126
00137 IDate ( const int kiYEAR ,
00138 const int kiMONTH ,
00139 const int kiDAY ,
00140 const int kiHOUR = 0 ,
00141 const int kiMINUTES = 0 ,
00142 const int kiSECONDS = 0 ) :
00143 tSecondsFromEpoch (-1)
00144 {
00145 set (kiYEAR, kiMONTH, kiDAY, kiHOUR, kiMINUTES, kiSECONDS);
00146 }
00147
00152 IDate (const EDateType keDATE_TYPE = eNow)
00153 throw (TConstraintException);
00154
00159 IDate& addSeconds (const int kiSECONDS)
00160 throw (TConstraintException)
00161 {
00162 if ( !empty() )
00163 {
00164 tSecondsFromEpoch += kiSECONDS;
00165 }
00166 else
00167 {
00168 throw TConstraintException ("invalid date operation", __FILE__, __LINE__);
00169 }
00170 return *this;
00171 }
00172
00177 IDate& addMinutes (const int kiMINUTES)
00178 {
00179 return addSeconds (kiMINUTES * 60);
00180 }
00181
00186 IDate& addHours (const int kiHOURS)
00187 {
00188 return addSeconds (kiHOURS * 3600);
00189 }
00190
00195 IDate& addDays (const int kiDAYS)
00196 {
00197 return addSeconds (kiDAYS * 86400);
00198 }
00199
00210 void set ( const int kiYEAR ,
00211 const int kiMONTH ,
00212 const int kiDAY ,
00213 const int kiHOUR = 0 ,
00214 const int kiMINUTES = 0 ,
00215 const int kiSECONDS = 0 )
00216 throw (TConstraintException);
00217
00218
00219 public:
00220
00221
00222
00223
00224
00226 bool empty (void) const
00227 {
00228 return ( (time_t) -1 == tSecondsFromEpoch );
00229 }
00230
00235 virtual TString get (const char* pkcDATE_FORMAT) const = 0;
00236
00241 TString get (const TString& rkyDATE_FORMAT) const
00242 {
00243 return get (rkyDATE_FORMAT.c_str());
00244 }
00245
00253 bool in ( const IDate& rktBEGIN_DATE ,
00254 const IDate& rktEND_DATE ) const
00255 {
00256 return ( ( *this >= rktBEGIN_DATE ) && ( *this <= rktEND_DATE ) );
00257 }
00258
00265 bool operator == (const IDate& rktDATE) const
00266 throw (TConstraintException)
00267 {
00268 if ( empty() || rktDATE.empty() )
00269 {
00270 throw TConstraintException ("invalid date comparison", __FILE__, __LINE__);
00271 }
00272 return ( tSecondsFromEpoch == rktDATE.tSecondsFromEpoch );
00273 }
00274
00275 bool operator != (const IDate& rktDATE) const
00276 {
00277 return !( *this == rktDATE );
00278 }
00279
00286 bool operator < (const IDate& rktDATE) const
00287 throw (TConstraintException)
00288 {
00289 if ( empty() || rktDATE.empty() )
00290 {
00291 throw TConstraintException ("invalid date comparison", __FILE__, __LINE__);
00292 }
00293 return ( tSecondsFromEpoch < rktDATE.tSecondsFromEpoch );
00294 }
00295
00296 bool operator > (const IDate& rktDATE) const
00297 {
00298 return ( rktDATE < *this );
00299 }
00300
00301 bool operator <= (const IDate& rktDATE) const
00302 {
00303 return !( rktDATE < *this );
00304 }
00305
00306 bool operator >= (const IDate& rktDATE) const
00307 {
00308 return !( *this < rktDATE );
00309 }
00310
00315 TTimeInterval operator - (const IDate& rktDATE) const
00316 {
00317 return TTimeInterval (difftime (tSecondsFromEpoch, rktDATE.tSecondsFromEpoch));
00318 }
00319
00320 };
00321
00322 }
00323
00324 }
00325
00326
00327 #endif // not _MPCL_TEXT_DATE__