Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages
date.h
Go to the documentation of this file.00001 00004 /* 00005 * Copyright © 2004 Michael Geddes 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 */ 00018 00019 #ifndef INCLUDE_COMET_DATE_H 00020 #define INCLUDE_COMET_DATE_H 00021 00022 #ifdef _SHOW_INC 00023 #pragma message(" #Include " __FILE__) 00024 #endif 00025 #include <comet/datetime.h> 00026 00027 namespace comet 00028 { 00032 00034 00037 struct dateonly_t : protected impl::datetime_base<long> 00038 { 00039 00041 dateonly_t() { dt_ = 0; } 00042 00044 explicit dateonly_t( const datetime_t &dt) 00045 { 00046 if (dt == dt_invalid ) 00047 dt_ = dt_invalid_; 00048 else if (dt == dt_null) 00049 dt_ = dt_null_; 00050 else 00051 dt_ = split_oledate_as_absdate_( dt.get()); 00052 } 00053 00055 dateonly_t( const impl::auto_attach_t<long> &dateval) 00056 { 00057 dt_ = dateval.get(); 00058 } 00059 00061 long get() const { return dt_; } 00062 dateonly_t &operator=(const impl::auto_attach_t<long> & dateval) 00063 { 00064 dt_ = dateval.get(); 00065 return *this; 00066 } 00067 00069 dateonly_t( dt_invalid_t ) { dt_ = dt_invalid_; } 00071 dateonly_t( dt_null_t ) { dt_ = dt_null_; } 00073 dateonly_t( dt_zero_t) { dt_ = 0; } 00074 00075 00077 dateonly_t( int year, int month, int day ) 00078 { 00079 if (!absdate_from_date_( &dt_, year, month, day)) 00080 set_invalid_(); 00081 } 00082 00084 static dateonly_t get_null() { return dateonly_t( dt_null_ ); } 00086 static dateonly_t get_zero() { return dateonly_t( 0 ); } 00087 00089 static dateonly_t today() 00090 { 00091 return dateonly_t(datetime_t::now()); 00092 } 00094 static dateonly_t today_utc() 00095 { 00096 return dateonly_t(datetime_t::now_utc()); 00097 } 00098 00100 void split(int *year, int *month, int *day) const 00101 { 00102 if(!good() || ! date_from_absdate_( dt_, year, month, day)) 00103 throw datetime_exception("Invalid Date"); 00104 } 00105 00113 bool set_date( int year, int month, int day) 00114 { 00115 return absdate_from_date_( &dt_, year, month, day); 00116 } 00117 00119 operator datetime_t() const 00120 { 00121 if (!good()) return datetime_t((DATE)dt_); 00122 return datetime_t(join_absdate_as_oledate_( dt_, 0)); 00123 } 00124 00126 00127 00128 int year() const 00129 { 00130 int year; 00131 split(&year,NULL,NULL); 00132 return year; 00133 } 00135 int month() const 00136 { 00137 int year,month,day; 00138 split(&year,&month,&day); 00139 return month; 00140 } 00142 int day() const 00143 { 00144 int year,month,day; 00145 split(&year,&month,&day); 00146 return day; 00147 } 00149 datetime_t::day_of_week dow() const 00150 { 00151 int wday; 00152 if(!good() || ! dow_from_absdate_( dt_, &wday)) 00153 throw datetime_exception("Invalid Date"); 00154 return datetime_t::day_of_week(wday); 00155 } 00157 int year_day() const 00158 { 00159 if (good()) 00160 { 00161 int y,m,d; 00162 date_from_absdate_(dt_, &y,&m,&d); 00163 long firstday; 00164 if ( absdate_from_date_(&firstday, y, 1, 1)) 00165 return 1 + ( dt_ - firstday); 00166 } 00167 throw datetime_exception("Invalid Date"); 00168 } 00170 int days_in_month() const 00171 { 00172 int year,month,day; 00173 split(&year,&month,&day); 00174 return days_in_month(year,month); 00175 } 00176 00178 00182 dateonly_t &add_months(int inc_months) 00183 { 00184 int year,month,day; 00185 split(&year,&month,&day); 00186 long months = (month-1)+(year*12)+inc_months; 00187 00188 long quot,rem; 00189 quot = months/12; 00190 rem = months%12; 00191 set_date( quot, rem+1, day); 00192 return *this; 00193 } 00194 00196 dateonly_t &add_years(int inc_years) 00197 { 00198 int year,month,day; 00199 split(&year,&month,&day); 00200 set_date( year+inc_years, month, day); 00201 return *this; 00202 } 00203 00205 00206 bool operator==(const dateonly_t& date) const { return date.dt_ == dt_; } 00207 bool operator!=(const dateonly_t& date) const{ return date.dt_ != dt_; } 00208 bool operator<(const dateonly_t& date) const 00209 { 00210 COMET_ASSERT( good() ); 00211 COMET_ASSERT( date.good() ); 00212 return dt_ < date.dt_; 00213 } 00214 bool operator>(const dateonly_t& date) const 00215 { 00216 COMET_ASSERT( good() ); 00217 COMET_ASSERT( date.good() ); 00218 return dt_ > date.dt_; 00219 } 00220 bool operator<=(const dateonly_t& date) const 00221 { 00222 COMET_ASSERT( good() ); 00223 COMET_ASSERT( date.good() ); 00224 return dt_ <= date.dt_; 00225 } 00226 bool operator>=(const dateonly_t& date) const 00227 { 00228 COMET_ASSERT( good() ); 00229 COMET_ASSERT( date.good() ); 00230 return dt_ >= date.dt_; 00231 } 00232 bool operator==(dt_invalid_t) const { return invalid(); } 00233 bool operator!=(dt_invalid_t) const { return !invalid(); } 00234 bool operator==(dt_zero_t) const { return dt_==0; } 00235 bool operator!=(dt_zero_t) const { return dt_!=0; } 00236 bool operator==(dt_null_t) const { return null(); } 00237 bool operator!=(dt_null_t) const { return !null(); } 00239 00241 00242 dateonly_t operator+(long dateSpan) const 00243 { 00244 dateonly_t dt(*this); 00245 dt+=dateSpan; 00246 return dt; 00247 } 00248 dateonly_t operator-(long dateSpan) const 00249 { 00250 dateonly_t dt(*this); 00251 dt-=dateSpan; 00252 return dt; 00253 } 00254 dateonly_t& operator+=(long dateSpan) 00255 { 00256 COMET_ASSERT( good() ); 00257 dt_ += dateSpan; 00258 return *this; 00259 } 00260 dateonly_t& operator-=(long dateSpan) 00261 { 00262 COMET_ASSERT( good() ); 00263 dt_ -= dateSpan; 00264 return *this; 00265 } 00266 long operator-(const dateonly_t& date) const 00267 { 00268 COMET_ASSERT( good() ); 00269 COMET_ASSERT( date.good() ); 00270 return dt_ - date.dt_; 00271 } 00272 dateonly_t &operator++() 00273 { 00274 COMET_ASSERT( good() ); 00275 ++dt_; 00276 return *this; 00277 } 00278 dateonly_t operator++(int) 00279 { 00280 COMET_ASSERT( good() ); 00281 dateonly_t t(*this); 00282 ++dt_; 00283 return t; 00284 } 00285 dateonly_t &operator--() 00286 { 00287 COMET_ASSERT( good() ); 00288 --dt_; 00289 return *this; 00290 } 00291 dateonly_t operator--(int) 00292 { 00293 COMET_ASSERT( good() ); 00294 dateonly_t t(*this); 00295 --dt_; 00296 return t; 00297 } 00299 00301 inline bool invalid() const { return dt_ == ( dt_invalid_); } 00303 inline bool null() const { return dt_ == ( dt_null_); } 00304 00306 inline bool zero() const { return dt_ == 0; } 00307 00309 inline bool good() const 00310 { 00311 switch (dt_) 00312 { 00313 case 0: case dt_invalid_: case dt_null_: return false; 00314 default: return true; 00315 } 00316 } 00317 00322 bstr_t format( datetime_t::format_flags flags = datetime_t::ff_default , LCID locale = LOCALE_USER_DEFAULT) const 00323 { 00324 flags = datetime_t::format_flags((flags | datetime_t::ff_date_only) & ~datetime_t::ff_time_only); 00325 bstr_t strDate; 00326 if (null() || invalid()) 00327 return strDate; 00328 00329 DATE dt = join_absdate_as_oledate_( dt_, 0); 00330 VarBstrFromDate(dt, locale, flags, strDate.out()) | raise_exception; 00331 return strDate; 00332 } 00333 00339 dateonly_t &parse( const bstr_t &val, datetime_t::format_flags flags = datetime_t::ff_default, LCID locale = LOCALE_USER_DEFAULT) 00340 { 00341 flags = datetime_t::format_flags((flags | datetime_t::ff_date_only) & ~datetime_t::ff_time_only); 00342 DATE dt; 00343 VarDateFromStr( val.in(), locale, flags, &dt) | raise_exception; 00344 dt_ = split_oledate_as_absdate_( dt); 00345 00346 return *this; 00347 } 00348 00352 template<typename CHAR> 00353 std::basic_string<CHAR> format( const std::basic_string<CHAR> &fmt ) const 00354 { 00355 return format(fmt.c_str()); 00356 } 00357 00361 template<typename CHAR> 00362 std::basic_string<CHAR> format( const CHAR *fmt ) const 00363 { 00364 // Just use datetime_t to handle this. 00365 datetime_t dt(*this); 00366 return dt.format(fmt); 00367 } 00368 00370 friend 00371 std::basic_ostream<char> &operator<<(std::basic_ostream<char> &os, const dateonly_t &val) 00372 { 00373 os << val.format(); 00374 return os; 00375 } 00376 00378 friend 00379 std::basic_ostream<wchar_t> &operator<<(std::basic_ostream<wchar_t> &os, const dateonly_t &val) 00380 { 00381 os << val.format(); 00382 return os; 00383 } 00384 00385 static inline int days_in_month(int year, int month) 00386 { 00387 return impl::datetime_base<long>::days_in_month(year,month); 00388 } 00389 00390 protected: 00391 explicit dateonly_t(long l) { dt_ = l; } 00392 }; 00394 }; 00395 00396 #endif /* INCLUDE_COMET_DATE_H */