Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

date.hh

00001 /*
00002 *  Name:         date.hh
00003 *  Author:       Francisco Rodrigo Escobedo Robles
00004 *  Contributor:  Rafael Jesus Alcantara Perez
00005 *  Summary:      Base date class
00006 *  Date:         $Date: 2003/04/19 10:46:01 $
00007 *  Revision:     $Revision: 1.2 $
00008 *
00009 *  Copyright (C) 2000-2001  Francisco Rodrigo Escobedo Robles <frer@pepix.net>
00010 *  Copyright (C) 2001-2003  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00011 *
00012 *  This program is free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  This program is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  You should have received a copy of the GNU General Public License
00023 *  along with this program; if not, write to the Free Software
00024 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00025 *  MA 02111-1307, USA.
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         //  C O N S T R U C T O R S
00058         //
00059 
00061         TTimeInterval (void) :
00062         dInterval (0.0)      {}
00063 
00065         TTimeInterval (double dINTERVAL) :
00066         dInterval (dINTERVAL)            {}
00067 
00068 
00069       public:
00070 
00071         //
00072         //  S E L E C T O R S
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     };  // class TTimeInterval
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         //  C O N S T R U C T O R S
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         //  S E L E C T O R S
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     };  // class IDate
00321 
00322   }  // namespace text
00323 
00324 }  // namespace mpcl
00325 
00326 
00327 #endif  // not _MPCL_TEXT_DATE__

Generated on Mon Oct 13 02:35:22 2003 for MPCL by doxygen1.2.18