• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/Date.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _Date_h
00018 #define _Date_h
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 
00023 #if defined(_WIN32) || defined(_WIN64)
00024 #include <spl/configwin32.h>
00025 #else
00026 #include <spl/autoconf/config.h>
00027 #endif
00028 
00029 #ifdef HAVE_SYS_TIME_H
00030 #include <sys/time.h>
00031 #endif
00032 #ifdef HAVE_TIME_H
00033 #include <time.h>
00034 #endif
00035 
00036 #include <spl/types.h>
00037 #include <spl/Debug.h>
00038 #include <spl/Exception.h>
00039 #include <spl/Memory.h>
00040 #include <spl/String.h>
00041 
00048 #define DATE_MAJIC 0xA011               //< Majic number for ASSERT's in Compare and Convert
00049 
00053 class Date : public IMemoryValidate, public IComparable
00054 {
00055 private:
00056         static long YmdToJd( const int iYear, const int iMonth, const int iDay );
00057         static void JdToYmd( const long lJD, int *piYear, int *piMonth, int *piDay );
00058         
00059         void Init(time_t t);
00060         
00061 protected:
00062     long m_lJulianDay;
00063         int m_y, m_m, m_d;
00064 
00065 public:
00066         enum DayOfWeek
00067         {
00068                 SUNDAY = 0,
00069                 MONDAY = 1,
00070                 TUESDAY = 2,
00071                 WEDNESDAY = 3,
00072                 THURSDAY = 4,
00073                 FRIDAY = 5,
00074                 SATURDAY = 6
00075         };
00076 
00077         Date();
00078         Date(const Date& dt);
00079         Date(int year, int mo, int day);
00080         Date( time_t tt );
00081         virtual ~Date();
00082 
00083         inline bool IsLeapYear()
00084         {
00085                 long jd1, jd2;
00086                 JdToYmd( m_lJulianDay, &m_y, &m_m, &m_d );
00087                 jd1 = YmdToJd( m_y, 2, 28 );
00088                 jd2 = YmdToJd( m_y, 3, 1 );
00089                 return ( ( jd2 - jd1 ) > 1 );
00090         }
00091 
00092         inline int Year() const
00093         {
00094                 ASSERT ( m_y > 0 );
00095                 return m_y;
00096         }
00097 
00098         inline int Month() const
00099         {
00100                 ASSERT ( m_m > 0 );
00101                 return m_m;
00102         }
00103 
00104         inline int Day() const
00105         {
00106                 ASSERT ( m_d > 0 );
00107                 return m_d;
00108         }
00109 
00110         inline enum DayOfWeek DayOfWeek() const
00111         {
00112                 return (enum DayOfWeek)( ( ( (int) ( m_lJulianDay % 7L ) ) + 1 ) % 7 );
00113         }
00114 
00115         inline int DayOfYear()
00116         {
00117                 long soy;
00118 
00119                 ASSERT ( m_y > 0 );
00120                 soy = YmdToJd( m_y, 1, 1 );
00121                 return (int) ( m_lJulianDay - soy + 1 );        
00122         }
00123 
00124         inline int DiffInDays( const Date *dt ) const
00125         {
00126                 return m_lJulianDay - dt->m_lJulianDay;
00127         }
00128 
00129         inline int DiffInDays( const Date& dt ) const
00130         {
00131                 return m_lJulianDay - dt.m_lJulianDay;
00132         }
00133 
00134         inline void AddDays( const int days )
00135         {
00136                 m_lJulianDay += days;
00137         }
00138 
00139         void AddMonths( int months );
00140 
00141         inline void AddYears( int years )
00142         {
00143                 m_y += years;
00144                 m_lJulianDay = YmdToJd( m_y, m_m, m_d );
00145         }
00146 
00147         bool IsHoliday() const;
00148 
00149         inline void ToString( char *buf, const int buflen ) const
00150         {
00151                 ASSERT ( m_y > 0 );
00152 #if defined(_MSC_VER) && _MSC_VER >= 1400
00153                 sprintf_s(buf, buflen, "%d/%d/%d", m_m, m_d, m_y);
00154 #else
00155                 sprintf(buf, "%d/%d/%d", m_m, m_d, m_y);
00156 #endif
00157         }
00158 
00159         inline StringPtr ToString() const
00160         {
00161                 char buf[24];
00162                 ToString(buf, 24);
00163                 return StringPtr(new String((char *)buf));
00164         }
00165 
00166         inline bool Equals( const Date *dt ) const
00167         {
00168                 return m_lJulianDay == dt->m_lJulianDay;
00169         }
00170         
00171         inline bool Equals( const Date& dt ) const
00172         {
00173                 return m_lJulianDay == dt.m_lJulianDay;
00174         }
00175 
00176         inline Date& operator =(const Date& dt )
00177         {
00178                 m_lJulianDay = dt.m_lJulianDay;
00179                 return *this;
00180         }
00181 
00182         inline bool operator ==(const Date& dt ) const
00183         {
00184                 return m_lJulianDay == dt.m_lJulianDay;
00185         }
00186 
00187         inline bool operator !=(const Date& dt ) const
00188         {
00189                 return m_lJulianDay != dt.m_lJulianDay;
00190         }
00191 
00192         inline bool operator >(const Date& dt) const
00193         {
00194                 return m_lJulianDay > dt.m_lJulianDay;
00195         }
00196 
00197         inline bool operator <(const Date& dt) const
00198         {
00199                 return m_lJulianDay < dt.m_lJulianDay;
00200         }
00201 
00202         inline bool operator >=(const Date& dt) const
00203         {
00204                 return m_lJulianDay >= dt.m_lJulianDay;
00205         }
00206 
00207         inline bool operator <=(const Date& dt) const
00208         {
00209                 return m_lJulianDay <= dt.m_lJulianDay;
00210         }
00211 
00212         inline time_t ToSysTime(  )
00213         {
00214                 struct tm tmRep;
00215                 time_t t;
00216                 
00217                 if ( m_y < 0 )
00218                 {
00219                         JdToYmd( m_lJulianDay, &m_y, &m_m, &m_d );
00220                 }
00221                 if ( m_y < 1970 )
00222                 {
00223                         m_y = 70;
00224                 }
00225                 tmRep.tm_year = m_y - 1900 ;
00226                 tmRep.tm_mon = m_m - 1;
00227                 tmRep.tm_mday = m_d;
00228                 tmRep.tm_hour = 0;
00229                 tmRep.tm_min = 0;
00230                 tmRep.tm_sec = 0;
00231                 tmRep.tm_isdst = 0;
00232                 
00233                 t = mktime( &tmRep );
00234                 return t;
00235         }
00236 
00237         inline int ToRevInt( ) const
00238         {
00239                 return Year() * 10000 + Month() * 100 + Day();
00240         }
00241 
00242         virtual int32 HashCode() const;
00243         virtual bool Equals( const IComparable *a ) const;
00244         virtual int Compare( const IComparable *a ) const;
00245         virtual bool Equals( const IComparable& a ) const;
00246         virtual int Compare( const IComparable& a ) const;
00247         virtual int32 MajicNumber() const;
00248 
00249         static Date Parse(const String& str);
00250         static bool IsDate(const String& str);
00251 
00252         inline static Date Now() { return Date(); }
00253 
00254         inline static bool IsLeapYear(int year)
00255         {
00256                 return ((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0;
00257         }
00258 
00259 #ifdef DEBUG
00260         virtual void ValidateMem() const;
00261         virtual void CheckMem() const;
00262 #endif
00263 };
00264 
00265 REGISTER_TYPEOF( 558, Date );
00266 
00269 #endif