Date.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Laurent Pereira
00022 */
00023 
00024 
00025 
00026 #include "CalendarConverter.h"
00027 #include "datelib.h"
00028 #include "eyedb/internals/ObjectPeer.h"
00029 
00030 namespace eyedb {
00031   //
00032   // Constants
00033   //
00034 
00035   const eyedblib::int32 MIN_JULIAN = 0;
00036   const eyedblib::int32 MAX_JULIAN = 211202371;
00037 
00038   //
00039   // Date user methods
00040   //
00041 
00042   void
00043   Date::userCopy(const Object &)
00044   {
00045     setClientData();
00046   }
00047 
00048   void
00049   Date::userInitialize()
00050   {
00051     setClientData();
00052   }
00053 
00054   void
00055   Date::setClientData()
00056   {
00057     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00058 
00059     char * s_date = cal->jday2ascii(getJulian());  
00060     strcpy(this->string_date, s_date);
00061   
00062     delete [] s_date;
00063   }
00064 
00065 
00066 
00067   eyedblib::int32
00068   Date::year() const
00069   {
00070     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00071 
00072     eyedblib::int32 y;
00073     cal->jday2calendar(getJulian(), &y, 0, 0);
00074 
00075     return y;
00076   }
00077 
00078   eyedblib::int16
00079   Date::month() const 
00080   {
00081     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00082 
00083     eyedblib::int16 m;
00084     cal->jday2calendar(getJulian(), 0, &m, 0);
00085 
00086     return m;
00087   }
00088 
00089   eyedblib::int16
00090   Date::day() const
00091   {
00092     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00093 
00094     eyedblib::int16 d;
00095     cal->jday2calendar(getJulian(), 0, 0, &d);
00096 
00097     return d;
00098   }
00099 
00100 
00101   eyedblib::int16
00102   Date::day_of_year() const
00103   {
00104 
00105     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00106 
00107     return cal->jday2day_of_year(getJulian());
00108   }
00109 
00110   Month::Type
00111   Date::month_of_year() const
00112   {
00113     return Month::Type(month());
00114   }
00115 
00116   Weekday::Type
00117   Date::day_of_week() const
00118   {
00119     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00120 
00121     return cal->jday2weekday(getJulian());
00122   }
00123 
00124   Bool
00125   Date::is_leap_year() const
00126   {
00127     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00128 
00129     return cal->jday2leap_year(getJulian());
00130   }
00131 
00132   Bool
00133   Date::is_equal(const Date & d) const
00134   {
00135   
00136     if(getJulian() == d.getJulian())
00137       {
00138         return True;
00139       }
00140     else
00141       {
00142         return False;
00143       }
00144   }
00145 
00146   Bool
00147   Date::is_greater(const Date & d) const
00148   {
00149 
00150   
00151     if(getJulian() > d.getJulian())
00152       {
00153         return True;
00154       }
00155     else
00156       {
00157         return False;
00158       }
00159 
00160   }
00161 
00162   Bool
00163   Date::is_greater_or_equal(const Date & d) const 
00164   {
00165     if(getJulian() >= d.getJulian())
00166       {
00167         return True;
00168       }
00169     else
00170       {
00171         return False;
00172       }
00173   }
00174 
00175   Bool
00176   Date::is_less(const Date & d) const
00177   {
00178     if(getJulian() < d.getJulian())
00179       {
00180         return True;
00181       }
00182     else
00183       {
00184         return False;
00185       }
00186   }
00187 
00188   Bool
00189   Date::is_less_or_equal(const Date & d) const
00190   {
00191     if(getJulian() <= d.getJulian())
00192       {
00193         return True;
00194       }
00195     else
00196       {
00197         return False;
00198       }
00199   }
00200 
00201   Bool
00202   Date::is_between(const Date & d1, const Date & d2) const
00203   {
00204     eyedblib::int32 j = getJulian();
00205 
00206     if( ( j > d1.getJulian()
00207           && j < d2.getJulian() )
00208         || ( j > d2.getJulian()
00209              && j < d1.getJulian()) )
00210       {
00211         return True;
00212       }
00213     else
00214       {
00215         return False;
00216       }
00217   }
00218 
00219   Date &
00220   Date::next(Weekday::Type day)
00221   {
00222     Weekday::Type today = day_of_week();
00223 
00224     eyedblib::int16 diff = day - today;
00225     if( diff < 0 )
00226       {
00227         diff = 7 + diff;
00228       }
00229 
00230     set_julian(getJulian() + diff);
00231 
00232     return *this;
00233   }
00234 
00235   Date &
00236   Date::previous(Weekday::Type day)
00237   {
00238     Weekday::Type today = day_of_week();
00239 
00240     eyedblib::int16 diff = day - today;
00241     if( diff > 0 )
00242       {
00243         diff = diff - 7;
00244       }
00245 
00246     set_julian(getJulian() + diff);
00247 
00248 
00249     return *this;
00250   }
00251 
00252 
00253 
00254   Date &
00255   Date::add_days(eyedblib::int32 days)
00256   {
00257     set_julian(getJulian() + days);
00258 
00259     return *this;
00260   }
00261 
00262   Date &
00263   Date::substract_days(eyedblib::int32 days)
00264   {
00265 
00266     set_julian(getJulian() - days); 
00267 
00268     return *this;
00269   }
00270 
00271   eyedblib::int32
00272   Date::substract_date(const Date & d) const
00273   {
00274     return (getJulian() - d.getJulian());
00275   }
00276 
00277   Status
00278   Date::set_julian(eyedblib::int32 julian_day)
00279   {
00280 
00281     if(julian_day < MIN_JULIAN
00282        || julian_day > MAX_JULIAN)
00283       {
00284         return Exception::make(IDB_ERROR, "date out of range");
00285       }
00286 
00287     Status s = setJulian(julian_day);
00288 
00289     // update client data
00290     setClientData();
00291 
00292     return s;
00293   }
00294 
00295   Date *
00296   Date::date(Database * db)
00297   {
00298     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00299   
00300     Date * d = new Date(db);
00301     d->set_julian(cal->current_date());
00302 
00303     return d;
00304   }
00305 
00306   Date *
00307   Date::date(Database * db, const Date & date)
00308   {
00309     Date * d = new Date(db);
00310     d->set_julian(date.getJulian());
00311     return  d;
00312   }
00313 
00314   Date * 
00315   Date::date(Database * db, eyedblib::int32 julian_day)
00316   {
00317     Date * d = new Date(db);
00318     d->set_julian(julian_day);
00319 
00320     return  d;
00321   }
00322 
00323   Date * 
00324   Date::date(Database * db, eyedblib::int32 year, Month::Type m, eyedblib::int16 day)
00325   {
00326     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00327   
00328     Date * d = new Date(db);
00329     eyedblib::int32 julian = 0;
00330     cal->calendar2jday(&julian, year, m, day);
00331     d->set_julian(julian);
00332     return  d;
00333   }
00334 
00335   Date *
00336   Date::date(Database * db, const char * s)
00337   {
00338     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00339   
00340     Date * d = new Date(db);
00341     d->set_julian(cal->ascii2jday(s));
00342 
00343 
00344     return d;
00345   }
00346 
00347   const char *
00348   Date::toString() const 
00349   {
00350     return this->string_date;
00351   }
00352 
00353  
00354   eyedblib::int32
00355   Date::julian(const char * d)
00356   {
00357     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00358     return cal->ascii2jday(d);
00359 
00360   }
00361 
00362 }

Generated on Mon Dec 22 18:15:53 2008 for eyedb by  doxygen 1.5.3