TimeStamp.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 #include <eyedb/syscls.h>
00025 #include "ClockConverter.h"
00026 #include "CalendarConverter.h"
00027 #include "datelib.h"
00028 
00029 namespace eyedb {
00030 
00031   //
00032   // vars
00033   //
00034 
00035   std::string timeStamp_string;
00036 
00037   //
00038   // Utility functions
00039   //
00040 
00041 
00042 
00043   eyedblib::int64
00044   gmt2local_timeStamp(eyedblib::int64 gmt_time, eyedblib::int16 time_zone, eyedblib::int16 * day_offset = 0)
00045   {
00046 
00047     //
00048     // Computes the local time
00049     //
00050     eyedblib::int64 local_time = (gmt_time % USEC_OF_DAY) + (eyedblib::int64)(time_zone) * USEC_OF_MINUTE;
00051 
00052     //
00053     // Computes the day offset
00054     //
00055     if( day_offset != 0 )
00056       {
00057         if( local_time >= USEC_OF_DAY )
00058           {
00059             *day_offset = 1;
00060           }
00061         else if ( local_time < 0 )
00062           {
00063             *day_offset = -1;
00064           }
00065         else
00066           {
00067             *day_offset = 0;
00068           }
00069       }
00070 
00071   
00072     //
00073     // Give local time a valid value ( [0, USEC_OF_DAY[ )
00074     //
00075     local_time = local_time % USEC_OF_DAY;
00076 
00077     if( local_time < 0 )
00078       {
00079         local_time = USEC_OF_DAY + local_time;    
00080       }
00081 
00082 
00083     return local_time;
00084   }
00085 
00086   void parse_timeStamp(const char * time, eyedblib::int64 * gmt_usec, eyedblib::int16 * timezone)
00087   {
00088 
00089     // separate the string in 3 tokens : date, time and timezone
00090     char * s = strdup(time);
00091 
00092     char * s_julian = strtok(s, " ");
00093     char * s_usec = strtok(0, " ");
00094     char * s_tz = strtok(0, " ");
00095 
00096 
00097     // parse the tokens
00098 
00099     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00100     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00101 
00102     eyedblib::int32 julian = cal->ascii2jday(s_julian);
00103   
00104     eyedblib::int64 local_usec = 0;
00105     if( s_usec != 0 )
00106       {
00107         local_usec = clock->ascii2usec(s_usec);
00108       }
00109 
00110     if( s_tz == 0 )
00111       {
00112         *timezone = clock->local_timezone();
00113       }
00114     else
00115       {
00116         *timezone = clock->ascii2tz(s_tz);
00117       }
00118 
00119 
00120 
00121     // convert local time to gmt time
00122 
00123 
00124     *gmt_usec = julian * USEC_OF_DAY +
00125       local_usec - ((eyedblib::int64)(*timezone) * USEC_OF_MINUTE);
00126  
00127   
00128 
00129 
00130     free(s);
00131   }
00132 
00133 
00134 
00135   //
00136   // TimeStamp methods
00137   //
00138 
00139 
00140   void
00141   TimeStamp::userCopy(const Object &)
00142   {
00143     setClientData();
00144   }
00145 
00146   void
00147   TimeStamp::userInitialize()
00148   {
00149     setClientData();
00150   }
00151 
00152   void
00153   TimeStamp::setClientData()
00154   {
00155     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00156     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00157 
00158     //
00159     // set the string value
00160     //
00161 
00162 
00163     // preliminary 
00164     eyedblib::int16 day_offset = 0;
00165     eyedblib::int64 local_time = gmt2local_timeStamp(getUsecs(), getTz(), &day_offset);
00166     eyedblib::int32 local_julian = clock->usec2day(getUsecs()) + day_offset;
00167 
00168     char * s_date = cal->jday2ascii( local_julian );
00169     char * s_time = clock->usec2ascii( local_time );
00170     char * s_zone = clock->tz2ascii(getTz());
00171 
00172     // convert to string
00173 
00174     this->string_time_stamp[0] = '\0';
00175     strcat(this->string_time_stamp, s_date);
00176     strcat(this->string_time_stamp, " ");
00177     strcat(this->string_time_stamp, s_time);
00178     strcat(this->string_time_stamp, " ");
00179     strcat(this->string_time_stamp, s_zone);
00180 
00181     // release temporary data
00182     delete [] s_date;
00183     delete [] s_time;
00184     delete [] s_zone;
00185   
00186     //
00187     // Set date and time values
00188     //  
00189 
00190     ts_date.set_julian( clock->usec2day(getUsecs()) );
00191     ts_time.set_usecs(getUsecs() % USEC_OF_DAY, getTz());
00192 
00193  
00194   }
00195 
00196 
00197 
00198   const Date &
00199   TimeStamp::date() const
00200   {
00201     return ts_date;
00202   }
00203 
00204   const Time &
00205   TimeStamp::time() const
00206   {
00207     return ts_time;
00208   }
00209 
00210 
00211   eyedblib::int32
00212   TimeStamp::year() const
00213   {
00214     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00215     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00216 
00217     eyedblib::int32 julian = clock->usec2day(getUsecs());
00218 
00219     eyedblib::int32 year = 0;
00220     cal->jday2calendar(julian, &year, 0, 0);
00221 
00222     return  year;
00223   }
00224 
00225   eyedblib::int16
00226   TimeStamp::month() const
00227   {
00228     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00229     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00230 
00231     eyedblib::int32 julian = clock->usec2day(getUsecs());
00232 
00233     eyedblib::int16 month = 0;
00234     cal->jday2calendar(julian, 0, &month, 0);
00235 
00236     return  month;
00237   }
00238 
00239   eyedblib::int16
00240   TimeStamp::day() const
00241   {
00242     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00243     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00244 
00245     eyedblib::int32 julian = clock->usec2day(getUsecs());
00246 
00247     eyedblib::int16 day = 0;
00248     cal->jday2calendar(julian, 0, 0, &day);
00249 
00250     return  day;
00251 
00252   }
00253 
00254   eyedblib::int16
00255   TimeStamp::hour() const
00256   {
00257     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00258 
00259     eyedblib::int16 hour = 0;
00260     eyedblib::int64 local_time = gmt2local_timeStamp(getUsecs(), getTz());
00261     clock->usec2clock(local_time, &hour);
00262 
00263     return hour;
00264   }
00265 
00266   eyedblib::int16
00267   TimeStamp::minute() const
00268   {
00269     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00270 
00271     eyedblib::int16 min = 0;
00272     eyedblib::int64 local_time = gmt2local_timeStamp(getUsecs(), getTz());
00273     clock->usec2clock(local_time, 0, &min);
00274 
00275     return min;
00276   }
00277 
00278   eyedblib::int16
00279   TimeStamp::second() const
00280   {
00281     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00282 
00283     eyedblib::int16 sec = 0;
00284     clock->usec2clock(getUsecs(), 0, 0, &sec);
00285 
00286     return sec;
00287   }
00288 
00289   eyedblib::int16
00290   TimeStamp::millisecond() const
00291   {
00292     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00293 
00294     eyedblib::int16 ms = 0;
00295     clock->usec2clock(getUsecs(), 0, 0, 0, &ms);
00296 
00297     return ms;
00298   }
00299 
00300   eyedblib::int16
00301   TimeStamp::tz_hour() const
00302   {
00303     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00304 
00305     eyedblib::int16 hour = 0;
00306     clock->tz2clock(getTz(), &hour, 0);
00307 
00308     return hour;
00309   }
00310 
00311   eyedblib::int16
00312   TimeStamp::tz_minute() const
00313   {
00314     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00315 
00316     eyedblib::int16 min = 0;
00317     clock->tz2clock(getTz(), 0, &min);
00318 
00319     return min;
00320   }
00321 
00322   TimeStamp &
00323   TimeStamp::plus(const TimeInterval & i)
00324   {
00325     set_usecs(getUsecs() + i.getUsecs(), getTz());
00326 
00327     return *this;
00328   }
00329 
00330   TimeStamp &
00331   TimeStamp::minus(const TimeInterval & i)
00332   {
00333     set_usecs(getUsecs() - i.getUsecs(), getTz());
00334 
00335     return *this;
00336   }
00337 
00338   Bool
00339   TimeStamp::is_equal(const TimeStamp & ts) const
00340   {
00341     if( getUsecs() == ts.getUsecs() )
00342       {
00343         return True;
00344       }
00345     else
00346       {
00347         return False;
00348       }
00349   }
00350 
00351   Bool
00352   TimeStamp::is_greater(const TimeStamp & ts) const
00353   {
00354     if( getUsecs() > ts.getUsecs() )
00355       {
00356         return True;
00357       }
00358     else
00359       {
00360         return False;
00361       }
00362     return True;
00363   }
00364 
00365   Bool
00366   TimeStamp::is_greater_or_equal(const TimeStamp & ts) const
00367   {
00368     if( getUsecs() >= ts.getUsecs() )
00369       {
00370         return True;
00371       }
00372     else
00373       {
00374         return False;
00375       }
00376     return True;
00377   }
00378 
00379   Bool
00380   TimeStamp::is_less(const TimeStamp & ts) const
00381   {
00382     if( getUsecs() < ts.getUsecs() )
00383       {
00384         return True;
00385       }
00386     else
00387       {
00388         return False;
00389       }
00390     return True;
00391   }
00392 
00393   Bool
00394   TimeStamp::is_less_or_equal(const TimeStamp & ts) const
00395   {
00396     if( getUsecs() <= ts.getUsecs() )
00397       {
00398         return True;
00399       }
00400     else
00401       {
00402         return False;
00403       }
00404   }
00405 
00406   Bool
00407   TimeStamp::is_between(const TimeStamp & ts1, const TimeStamp & ts2) const
00408   {
00409     eyedblib::int64 usec = getUsecs();
00410 
00411     if( (usec < ts1.getUsecs()
00412          && usec > ts2.getUsecs() ) 
00413         || ( usec > ts1.getUsecs()
00414              && usec < ts2.getUsecs() ) )
00415       {
00416         return True;
00417       }
00418     else
00419       {
00420         return False;
00421       }
00422   }
00423 
00424   eyedblib::int16
00425   TimeStamp::gmt_hour() const
00426   {
00427     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00428 
00429     eyedblib::int16 hour = 0;
00430     clock->usec2clock(getUsecs() % USEC_OF_DAY, &hour);
00431 
00432     return hour;
00433   }
00434 
00435   eyedblib::int16
00436   TimeStamp::gmt_minute() const
00437   {
00438     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00439 
00440     eyedblib::int16 min = 0;
00441     clock->usec2clock(getUsecs(), 0, &min);
00442 
00443     return min;
00444   }
00445 
00446   eyedblib::int16
00447   TimeStamp::microsecond() const
00448   {
00449     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00450 
00451     eyedblib::int16 us = 0;
00452     clock->usec2clock(getUsecs(), 0, 0, 0, 0, &us);
00453 
00454     return us;
00455   }
00456 
00457   Status
00458   TimeStamp::set_usecs(eyedblib::int64 usec, eyedblib::int16 tz)
00459   {
00460   
00461     if(usec < 0)
00462       {
00463         return Exception::make("time_stamp out of range");
00464       }
00465     if(tz < MIN_TZ
00466        || tz > MAX_TZ)
00467       {
00468         return Exception::make("time_zone out of range");
00469       }
00470 
00471     Status s;
00472     s = setUsecs(usec);  
00473 
00474     // update client data
00475     setClientData();
00476 
00477     if(s)
00478       {
00479         return s;
00480       }
00481 
00482     s = setTz(tz);
00483     // update client data
00484     setClientData();
00485 
00486     return s;
00487   }
00488 
00489 
00490 
00491   TimeInterval *
00492   TimeStamp::substract(const TimeStamp & ts)
00493   {
00494     return TimeInterval::time_interval(getDatabase(), getUsecs() - ts.getUsecs());
00495  
00496   }
00497 
00498   TimeStamp *
00499   TimeStamp::gmt_time_stamp(Database * db)
00500   {
00501     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00502     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00503 
00504     TimeStamp * ts = new TimeStamp(db);
00505     ts->set_usecs(clock->current_time() + cal->current_date() * USEC_OF_DAY, 0);
00506 
00507     return ts;
00508   }
00509 
00510   TimeStamp *
00511   TimeStamp::local_time_stamp(Database * db)
00512   {
00513     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00514     CalendarConverter * cal = DateAlgorithmRepository::getDefaultCalendarConverter();
00515 
00516     TimeStamp * ts = new TimeStamp(db);
00517     ts->set_usecs(clock->current_time() + cal->current_date() * USEC_OF_DAY, 
00518                   clock->local_timezone());
00519 
00520     return ts;
00521 
00522   }
00523 
00524   TimeStamp *
00525   TimeStamp::time_stamp(Database * db, const TimeStamp & time_stamp)
00526   {
00527   
00528     TimeStamp * ts = new TimeStamp(db);
00529     ts->set_usecs(time_stamp.getUsecs(), ts->getTz());
00530 
00531 
00532     return ts;
00533   }
00534 
00535   TimeStamp *
00536   TimeStamp::time_stamp(Database * db, eyedblib::int64 usec)
00537   {
00538     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00539 
00540     TimeStamp * ts = new TimeStamp(db);
00541     ts->set_usecs(usec, clock->local_timezone());
00542 
00543     return ts;
00544   }
00545 
00546   TimeStamp *
00547   TimeStamp::time_stamp(Database * db, eyedblib::int64 usec, eyedblib::int16 tz)
00548   {
00549     TimeStamp * ts = new TimeStamp(db);
00550     ts->set_usecs(usec, tz);
00551 
00552     return ts;
00553   }
00554 
00555   TimeStamp *
00556   TimeStamp::time_stamp(Database * db, eyedblib::int32 julian_day, eyedblib::int64 usec)
00557   {
00558     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00559 
00560     TimeStamp * ts = new TimeStamp(db);
00561     ts->set_usecs(usec + julian_day * USEC_OF_DAY, 
00562                   clock->local_timezone());
00563  
00564 
00565     return ts;
00566   }
00567 
00568   TimeStamp *
00569   TimeStamp::time_stamp(Database * db, eyedblib::int32 julian_day, eyedblib::int64 usec, eyedblib::int16 tz)
00570   {
00571     TimeStamp * ts = new TimeStamp(db);
00572     ts->set_usecs(usec + julian_day * USEC_OF_DAY, 
00573                   tz);
00574 
00575     return ts;
00576   }
00577 
00578   TimeStamp *
00579   TimeStamp::time_stamp(Database * db, const Date & d, const Time & t)
00580   {
00581     TimeStamp * ts = new TimeStamp(db);
00582     ts->set_usecs(t.getUsecs() + d.getJulian() * USEC_OF_DAY, 
00583                   t.getTz());
00584  
00585 
00586     return ts;
00587   }
00588 
00589   TimeStamp *
00590   TimeStamp::time_stamp(Database * db, const Date & d)
00591   {
00592     ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00593 
00594     TimeStamp * ts = new TimeStamp(db);
00595     ts->set_usecs(d.getJulian() * USEC_OF_DAY, 
00596                   clock->local_timezone());
00597  
00598     return ts;
00599   }
00600 
00601   TimeStamp *
00602   TimeStamp::time_stamp(Database * db, const Date & d, eyedblib::int16 tz_hour, eyedblib::int16 tz_min)
00603   {
00604 
00605     TimeStamp * ts = new TimeStamp(db);
00606     ts->set_usecs(d.getJulian() * USEC_OF_DAY, 
00607                   tz_hour * 60 + tz_min);
00608 
00609 
00610     return ts;
00611   }
00612 
00613   TimeStamp *
00614   TimeStamp::time_stamp(Database * db, const char * s)
00615   {
00616     eyedblib::int64 gmt_usec = 0;
00617     eyedblib::int16 timezone = 0;
00618 
00619     parse_timeStamp(s, &gmt_usec, &timezone);
00620 
00621     TimeStamp * ts = new TimeStamp(db);
00622     ts->set_usecs(gmt_usec, timezone);
00623 
00624     return ts;
00625   }
00626 
00627   const char *
00628   TimeStamp::toString() const
00629   {
00630     return this->string_time_stamp;
00631   }
00632 
00633   eyedblib::int64
00634   TimeStamp::usec_time_stamp(const char * ts)
00635   {
00636     eyedblib::int64 gmt_usec;
00637     eyedblib::int16 timezone;
00638 
00639     parse_timeStamp(ts, &gmt_usec, &timezone);
00640 
00641     return gmt_usec;
00642   }
00643 }

Generated on Mon Dec 22 18:16:11 2008 for eyedb by  doxygen 1.5.3