00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _Dates_h
00018 #define _Dates_h
00019
00020 #include <stdlib.h>
00021
00022 #include <spl/types.h>
00023 #include <spl/Debug.h>
00024
00025 #ifdef _WINDOWS
00026 #include <spl/configwin32.h>
00027 #else
00028 #include <spl/autoconf/config.h>
00029 #endif
00030
00031 #ifdef HAVE_SYS_TIME_H
00032 #include <sys/time.h>
00033 #endif
00034 #ifdef HAVE_TIME_H
00035 #include <time.h>
00036 #endif
00037
00038 #include <spl/Date.h>
00039 #include <spl/Exception.h>
00040 #include <spl/Memory.h>
00041 #include <spl/String.h>
00042
00049 #define DATETIME_MAJIC 0x41FA //< Majic number for ASSERT's in Compare and Convert
00050
00054 class DateTime : public IMemoryValidate, public IComparable
00055 {
00056 protected:
00057 struct tm m_dtm;
00058 time_t m_timet;
00059
00060 public:
00061 DateTime();
00062 DateTime(const DateTime& dtm);
00063 DateTime(const Date& dt);
00064 DateTime( int year, int month, int day );
00065 DateTime( int year, int month, int day, int hour, int minute, int second );
00066 DateTime( const time_t t );
00067 virtual ~DateTime();
00068
00070 inline static DateTime Now()
00071 {
00072 return DateTime();
00073 }
00074
00075 inline int Year() const
00076 {
00077 return m_dtm.tm_year + 1900;
00078 }
00079
00080 inline int Month() const
00081 {
00082 return m_dtm.tm_mon + 1;
00083 }
00084
00085 inline int Day() const
00086 {
00087 return m_dtm.tm_mday;
00088 }
00089
00090 inline int Hour() const
00091 {
00092 return m_dtm.tm_hour;
00093 }
00094
00095 inline int Minutes() const
00096 {
00097 return m_dtm.tm_min;
00098 }
00099
00100 inline int Seconds() const
00101 {
00102 return m_dtm.tm_sec;
00103 }
00104
00105 inline bool IsDayLightSavingsTime() const
00106 {
00107 return m_dtm.tm_isdst != 0;
00108 }
00109
00110 inline int JulianDate() const
00111 {
00112 return m_dtm.tm_yday + 1;
00113 }
00114
00116 inline enum Date::DayOfWeek DayOfWeek() const
00117 {
00118 return (enum Date::DayOfWeek)m_dtm.tm_wday;
00119 }
00120
00121 inline Date DatePart() const
00122 {
00123 return Date( Year(), Month(), Day() );
00124 }
00125
00126 inline bool IsLeapYear() const
00127 {
00128 int year = Year();
00129 if ( (year % 4) == 0 )
00130 {
00131 if ( (year % 100) == 0 )
00132 {
00133 return (int)((year % 400) == 0);
00134 }
00135 return true;
00136 }
00137 return false;
00138 }
00139
00140 inline time_t ToSysTime() const
00141 {
00142
00143 return m_timet;
00144 }
00145
00146 inline void ToString( char *buf, int buflen ) const
00147 {
00148 #if defined(_MSC_VER) && _MSC_VER >= 1400
00149 asctime_s(buf, buflen, &m_dtm);
00150 #else
00151 char *cp = asctime( &m_dtm );
00152 spl::StrCpyLen( buf, cp, buflen );
00153 #endif
00154 }
00155
00156 inline StringPtr ToString() const
00157 {
00158 char buf[128];
00159 ToString(buf, 128);
00160 return StringPtr(new String(buf));
00161 }
00162
00164 StringPtr ToStringISO8601() const;
00165
00167 StringPtr ToTimeString() const;
00168
00169 DateTime AddSeconds(int64 seconds) const;
00170 DateTime AddMinutes(int minutes) const;
00171 DateTime AddHours(int hours) const;
00172 DateTime AddDays(int days) const;
00173 DateTime AddMonths(int months) const;
00174 DateTime AddYears(int years) const;
00175
00176 inline int64 DiffInSeconds( const DateTime dtm ) const
00177 {
00178 return (int64)difftime(m_timet, dtm.m_timet);
00179 }
00180
00181 inline int64 DiffInMinutes( const DateTime dtm ) const
00182 {
00183 return DiffInSeconds(dtm) / (60);
00184 }
00185
00186 inline int64 DiffInHours( const DateTime dtm ) const
00187 {
00188 return DiffInSeconds(dtm) / (60 * 60);
00189 }
00190
00191 inline int64 DiffInDays( const DateTime dtm ) const
00192 {
00193 return DiffInSeconds(dtm) / (60 * 60 * 24);
00194 }
00195
00196 inline bool Equals( const DateTime& dt ) const
00197 {
00198 return 0 == memcmp(&m_dtm, &dt.m_dtm, sizeof(struct tm));
00199 }
00200
00201 inline bool Equals( const DateTime* dt ) const
00202 {
00203 return 0 == memcmp(&m_dtm, &dt->m_dtm, sizeof(struct tm));
00204 }
00205
00206 inline DateTime& operator =(const DateTime& dtm )
00207 {
00208 m_dtm = dtm.m_dtm;
00209 m_timet = dtm.m_timet;
00210 return *this;
00211 }
00212
00213 inline bool operator <(const DateTime& dt) const
00214 {
00215 return ToSysTime() < dt.ToSysTime();
00216 }
00217
00218 inline bool operator <=(const DateTime& dt) const
00219 {
00220 return ToSysTime() <= dt.ToSysTime();
00221 }
00222
00223 inline bool operator >(const DateTime& dt) const
00224 {
00225 return ToSysTime() > dt.ToSysTime();
00226 }
00227
00228 inline bool operator >=(const DateTime& dt) const
00229 {
00230 return ToSysTime() >= dt.ToSysTime();
00231 }
00232
00233 inline bool operator ==(const DateTime& dt) const
00234 {
00235 return ToSysTime() == dt.ToSysTime();
00236 }
00237
00238 inline bool operator !=(const DateTime& dt) const
00239 {
00240 return ToSysTime() != dt.ToSysTime();
00241 }
00242
00243 virtual int32 HashCode() const;
00244 virtual bool Equals( const IComparable *a ) const;
00245 virtual int Compare( const IComparable *a ) const;
00246 virtual bool Equals( const IComparable& a ) const;
00247 virtual int Compare( const IComparable& a ) const;
00248 virtual int32 MajicNumber() const;
00249
00250 static DateTime Parse(const String& str);
00251 static bool IsDateTime(const String& str);
00252
00253 #ifdef DEBUG
00254 virtual void ValidateMem() const;
00255 virtual void CheckMem() const;
00256 #endif
00257 };
00258
00259 inline void ValidateType(DateTime& dtm)
00260 {
00261 }
00262
00263 inline void ValidateType(DateTime *dtm)
00264 {
00265 ASSERT_MEM( dtm, sizeof(DateTime));
00266 DEBUG_NOTE_MEM( dtm );
00267 }
00268
00269 REGISTER_TYPEOF( 559, DateTime );
00270
00273 #endif