Time.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 #include "common/h/Time.h"
00032 #include <iostream>
00033 #include <time.h>
00034 #include <string.h>
00035 #include <iomanip>
00036 #include <stdio.h>
00037 #include <assert.h>
00038 #include <limits.h>
00039 #include "common/h/int64iostream.h"
00040 
00041 
00042 timeUnit::ostream_fmt timeUnit::curFmt = timeUnit::sparse;
00043 
00044 const timeUnit *timeUnit::_ns = NULL;
00045 const timeUnit *timeUnit::_us = NULL;
00046 const timeUnit *timeUnit::_ms = NULL;
00047 const timeUnit *timeUnit::_sec = NULL;
00048 const timeUnit *timeUnit::_minute = NULL;
00049 const timeUnit *timeUnit::_hour = NULL;
00050 const timeUnit *timeUnit::_day = NULL;
00051 const timeUnit *timeUnit::_year = NULL;
00052 const timeUnit *timeUnit::_leapYear = NULL;
00053 
00054 // these helper functions will help reduce code bloat
00055 // The ...Help functions are not inlined (or not included with their inlined
00056 // user functions) because the Help functions call constructors which are
00057 // also inlined and the result is the possibility for a lot of code bloat.
00058 // So the reference is inlined, but the creation step (which only will happen
00059 // once) is not inlined.
00060 
00061 const timeUnit *timeUnit::nsHelp() {
00062   return new timeUnit(fraction(1));
00063 }
00064 const timeUnit *timeUnit::usHelp() {
00065   return new timeUnit(fraction(1000));
00066 }
00067 const timeUnit *timeUnit::msHelp() {
00068   return new timeUnit(fraction(1000000));
00069 }
00070 const timeUnit *timeUnit::secHelp() {
00071   return new timeUnit(fraction(1000000000));
00072 }
00073 const timeUnit *timeUnit::minHelp() {
00074   int64_t ns_per_sec = sec().get_ns_per_unit().getNumer();
00075   return new timeUnit(fraction(I64_C(60) * ns_per_sec));
00076 }
00077 const timeUnit *timeUnit::hourHelp() {
00078   int64_t ns_per_min = minute().get_ns_per_unit().getNumer();
00079   return new timeUnit(fraction(I64_C(60) * ns_per_min));
00080 }
00081 const timeUnit *timeUnit::dayHelp() {
00082   int64_t ns_per_hour = hour().get_ns_per_unit().getNumer();
00083   return new timeUnit(fraction(I64_C(24) * ns_per_hour));
00084 }
00085 const timeUnit *timeUnit::yearHelp() {
00086   double ns_per_day =static_cast<double>(day().get_ns_per_unit().getNumer());
00087   fraction ratio(static_cast<int64_t>(365 * ns_per_day));
00088   return new timeUnit(ratio);
00089 }
00090 const timeUnit *timeUnit::leapYearHelp() {
00091   double ns_per_day =static_cast<double>(day().get_ns_per_unit().getNumer());
00092   fraction ratio(static_cast<int64_t>(366 * ns_per_day));
00093   return new timeUnit(ratio);
00094 }
00095 
00096 const timeBase *timeBase::_bStd = NULL;
00097 const timeBase *timeBase::_b1970 = NULL;
00098 
00099 const timeBase *timeBase::bStdHelp() {
00100   return new timeBase(0);
00101 }
00102 // if StdBase is changed, might need to change calculation of numLeapYears
00103 const timeBase *timeBase::b1970Help() {
00104   int64_t nsPerYear = timeUnit::year().get_ns_per_unit().getNumer();
00105   int64_t nsPerLeapYear = timeUnit::leapYear().get_ns_per_unit().getNumer();
00106   int numLeapYears = (StdBaseMark - 1970)/4;
00107   int numStdYears  = (StdBaseMark - 1970) - numLeapYears;
00108   return new timeBase(numStdYears * nsPerYear + 
00109               numLeapYears * nsPerLeapYear);
00110 }
00111 
00112 
00113 int64_t timeUnit::cvtTo_ns(double units) const {
00114   double tns = ns_per_unit * units;
00115   return static_cast<int64_t>(tns);
00116 }
00117 
00118 int64_t timeUnit::cvtTo_ns(int64_t units) const {
00119   int64_t nsec;
00120   nsec = ns_per_unit.multReturnInt64(units);
00121   return nsec;
00122 }
00123 
00124 double timeUnit::cvtFrom_nsD(int64_t nsec) const {
00125   return units_per_ns * (double)nsec;
00126 }
00127 
00128 int64_t timeUnit::cvtFrom_nsI(int64_t nsec) const {
00129   int64_t units;
00130   units = units_per_ns.multReturnInt64(nsec);
00131   return units;
00132 }
00133 
00134 int64_t timeParent::getRolloverTime(double dt) {
00135   // a modulus with doubles
00136   int num = (int) (dt / (double)I64_MAX);
00137   double rolloverAmt = dt - ((double) num * (double)I64_MAX);
00138   return (int64_t) rolloverAmt;
00139 }
00140 
00141 const timeStamp *timeStamp::_ts1800  = NULL;
00142 const timeStamp *timeStamp::_ts1970  = NULL;
00143 const timeStamp *timeStamp::_tsStd   = NULL;
00144 const timeStamp *timeStamp::_ts2200  = NULL;
00145 // This 88 quintillion number is some 280 years.  For the timeLength class,
00146 // this amount of time should be safely out of reach.  The timeStamp class is
00147 // internally based at the year of timeBase::StdBaseMark, ie. year 2000, the
00148 // uninitialized value represents a timeStamp around year 2280, plenty safe
00149 // out of range for a long time.  We want the same number, eg. 8, for every
00150 // digit of this number so it can be easily recognized from a debugger.
00151 const int64_t    timeParent::uninitializedValue = I64_C(8888888888888888888);
00152 
00153 const timeStamp *timeStamp::ts1800Help() {
00154   timeStamp *p_ts1970 = new timeStamp(0, timeUnit::year(), timeBase::b1970());
00155   *p_ts1970 -= 53*timeLength::year() + 17*timeLength::leapYear(); //1900 - 1970
00156   // beginning 2000 (leap year) - beg 2100 (no leap year), 25 4 year spans
00157   *p_ts1970 -= 76*timeLength::year() + 24*timeLength::leapYear();// 1800 - 1900
00158   return p_ts1970;
00159 }
00160 
00161 const timeStamp *timeStamp::ts1970Help() {
00162   return new timeStamp(0, timeUnit::sec(), timeBase::b1970());
00163 }
00164 
00165 const timeStamp *timeStamp::tsStdHelp() {
00166   return new timeStamp(0, timeUnit::year(), timeBase::bStd());
00167 }
00168 
00169 const timeStamp *timeStamp::ts2200Help() {
00170   timeStamp *p_ts1970 = new timeStamp(0, timeUnit::year(), timeBase::b1970());
00171   *p_ts1970 += 23*timeLength::year() + 7*timeLength::leapYear(); // 1970 - 2000
00172   // beginning 2000 (leap year) - beg 2100 (no leap year), 25 4 year spans
00173   *p_ts1970 += 75*timeLength::year() + 25*timeLength::leapYear();
00174   // beg 2100 (no leap year) - beg 2200 (no leap year), 25 4 year spans
00175   *p_ts1970 += 76*timeLength::year() + 24*timeLength::leapYear();
00176   return p_ts1970;
00177 }
00178 
00179 void timeStamp::initI(int64_t iTime, const timeUnit &u, timeBase b) {
00180   assign( b.cvtTo_bStd( u.cvtTo_ns(iTime)));
00181   /*  // if we allow exceptions in the future, we will be able to handle
00182       // nicely cases of overflows in the fraction class
00183       } catch(finalMultOverflowExc &) {
00184       double dRes = static_cast<double>(b.cvtTo_bStd( u.cvtTo_ns(
00185       static_cast<double>(iTime))));
00186       assign( getRolloverTime(dRes));
00187       }
00188   */
00189 }
00190 
00191 void timeLength::initI(int64_t iTime, const timeUnit &u) {
00192   assign( u.cvtTo_ns(iTime));
00193   /*  // if we allow exceptions in the future, we will be able to handle
00194       // nicely cases of overflows in the fraction class  
00195       } catch(finalMultOverflowExc &) {
00196       double dRes= static_cast<double>(u.cvtTo_ns(static_cast<double>(iTime)));
00197       assign( getRolloverTime(dRes));
00198       }
00199   */
00200 }
00201 
00202 void relTimeStamp::initI(int64_t iTime, const timeUnit &u) {
00203   assign( u.cvtTo_ns(iTime));
00204   /*  // if we allow exceptions in the future, we will be able to handle
00205       // nicely cases of overflows in the fraction class  
00206       } catch(finalMultOverflowExc &) {
00207       double dRes= static_cast<double>(u.cvtTo_ns(static_cast<double>(iTime)));
00208       assign( getRolloverTime(dRes));
00209       }
00210   */
00211 }
00212 
00213 timeStamp::timeStamp(const timeLength &tl, timeBase b) : timeParent() {
00214   initI(tl.getI(timeUnit::ns()), timeUnit::ns(), b);
00215 }
00216 
00217 timeStamp::timeStamp(double dTime, const timeUnit &u, timeBase b): timeParent()
00218 {
00219   double dRes = static_cast<double>(b.cvtTo_bStd( u.cvtTo_ns(dTime)));
00220   if(dRes <= static_cast<double>(I64_MAX)) 
00221     assign( static_cast<int64_t>(dRes));
00222   else
00223     assign( getRolloverTime(dRes));
00224 }
00225 
00226 const relTimeStamp *relTimeStamp::_Zero      = NULL;
00227 
00228 const relTimeStamp *relTimeStamp::ZeroHelp() {
00229   return new relTimeStamp(0, timeUnit::sec());
00230 }
00231 
00232 const timeLength *timeLength::_zero = NULL;
00233 const timeLength *timeLength::ZeroHelp() {
00234   return new timeLength(0, timeUnit::sec());
00235 }
00236 
00237 relTimeStamp::relTimeStamp(const timeLength &tl) : timeParent() {
00238   initI(tl.getI(timeUnit::ns()), timeUnit::ns());
00239 }
00240 
00241 relTimeStamp::relTimeStamp(double dTime, const timeUnit &u): timeParent()
00242 {
00243   double dRes = static_cast<double>( u.cvtTo_ns(dTime));
00244   if(dRes <= static_cast<double>(I64_MAX)) 
00245     assign( static_cast<int64_t>(dRes));
00246   else
00247     assign( getRolloverTime(dRes));
00248 }
00249 
00250 const timeLength *timeLength::_ns = NULL;
00251 const timeLength *timeLength::nsHelp() {
00252   return new timeLength(1, timeUnit::ns());
00253 }
00254 const timeLength *timeLength::_us = NULL;
00255 const timeLength *timeLength::usHelp() {
00256   return new timeLength(1, timeUnit::us());
00257 }
00258 const timeLength *timeLength::_ms = NULL;
00259 const timeLength *timeLength::msHelp() {
00260   return new timeLength(1, timeUnit::ms());
00261 }
00262 const timeLength *timeLength::_sec = NULL;
00263 const timeLength *timeLength::secHelp() {
00264   return new timeLength(1, timeUnit::sec());
00265 }
00266 const timeLength *timeLength::_minute = NULL;
00267 const timeLength *timeLength::minHelp() {
00268   return new timeLength(1, timeUnit::minute());
00269 }
00270 const timeLength *timeLength::_hour = NULL;
00271 const timeLength *timeLength::hourHelp() {
00272   return new timeLength(1, timeUnit::hour());
00273 }
00274 const timeLength *timeLength::_day = NULL;
00275 const timeLength *timeLength::dayHelp() {
00276   return new timeLength(1, timeUnit::day());
00277 }
00278 const timeLength *timeLength::_year = NULL;
00279 const timeLength *timeLength::yearHelp() {
00280   return new timeLength(1, timeUnit::year());
00281 }
00282 const timeLength *timeLength::_leapYear = NULL;
00283 const timeLength *timeLength::leapYearHelp() {
00284   return new timeLength(1, timeUnit::leapYear());
00285 }
00286 
00287 
00288 timeLength::timeLength(double dTime, const timeUnit &u) : timeParent() {
00289   double dRes = static_cast<double>(u.cvtTo_ns(dTime));
00290   if(dRes <= static_cast<double>(I64_MAX)) 
00291     assign( static_cast<int64_t>(dRes));
00292   else
00293     assign( getRolloverTime(dRes));
00294 }
00295 
00296 ostream& operator<<(ostream&s, const timeUnit::ostream_fmt &u) {
00297   timeUnit::curFmt = u;
00298   return s;
00299 }
00300 
00301 ostream& operator<<(ostream&s, const timeUnit &u) {
00302   if(timeUnit::curFmt == timeUnit::sparse) {
00303     s << "["<< fraction::sparse << u.get_ns_per_unit()<<"]";
00304   } else { // timeUnit::verbose
00305     s << fraction::verbose << "[ns_per_unit: " << u.get_ns_per_unit() 
00306       << ", units_per_ns: " << u.get_units_per_ns() << "]";
00307   }
00308   return s;
00309 }
00310 
00311 ostream& operator<<(ostream&s, timeBase b) {
00312   s << timeStamp(-b.get_ns2StdBaseMark(),timeUnit::ns(),timeBase::bStd());
00313   return s;
00314 }
00315 
00316 /*
00317 ostream& operator<<(ostream& s, const timeParent &tp) {
00318   return tp.put(s);
00319 }
00320 */
00321 
00322 // only handles positives right now
00323 // string buffer should be atleast 16
00324 void insCommas(int num, char strbuf[]) {
00325     char nsStr[20];
00326     sprintf(nsStr,"%12d",num);
00327     int nextIdx=0;
00328     if(num>=1000000000) {
00329       strncpy(&strbuf[nextIdx], &nsStr[0], 3);
00330       nextIdx+=3;
00331       strbuf[nextIdx++] = ',';  
00332     }
00333     if(num>=1000000) {
00334       strncpy(&strbuf[nextIdx], &nsStr[3], 3);
00335       nextIdx+=3;
00336       strbuf[nextIdx++] = ',';  
00337     }
00338     if(num>=1000) {
00339       strncpy(&strbuf[nextIdx], &nsStr[6], 3);
00340       nextIdx+=3;
00341       strbuf[nextIdx++] = ',';  
00342     }
00343     if(num>=0) {
00344       strncpy(&strbuf[nextIdx], &nsStr[9], 3);
00345       nextIdx+=3;
00346     }
00347     strbuf[nextIdx]=0;
00348 }
00349 
00350 ostream& operator<<(ostream&s, timeLength z) {
00351   timeLength tUser = z;
00352   int years = static_cast<int>(tUser.getI(timeUnit::year()));
00353   timeLength tempT(years,timeUnit::year());
00354   tUser -= tempT;
00355   int days  = static_cast<int>(tUser.getI(timeUnit::day()));
00356   tUser -= timeLength(days,timeUnit::day());
00357   int hours = static_cast<int>(tUser.getI(timeUnit::hour()));
00358   tUser -= timeLength(hours,timeUnit::hour());
00359   int mins  = static_cast<int>(tUser.getI(timeUnit::minute()));
00360   tUser -= timeLength(mins,timeUnit::minute());
00361   int secs  = static_cast<int>(tUser.getI(timeUnit::sec()));
00362   tUser -= timeLength(secs,timeUnit::sec());
00363   int ms    = static_cast<int>(tUser.getI(timeUnit::ms()));
00364   tUser -= timeLength(ms,timeUnit::ms());
00365   int us    = static_cast<int>(tUser.getI(timeUnit::us()));
00366   tUser -= timeLength(us,timeUnit::us());
00367   int ns    = static_cast<int>(tUser.getI(timeUnit::ns()));
00368 
00369   bool prev = false, compact = false;
00370   char cSt[5] = ", ";
00371   // this is a hack for those that want to print the time compressed
00372   if(s.flags() & ostream::oct) {  compact=true;  s.flags(ostream::dec);  }
00373   if(compact == true) strcpy(cSt,",");
00374   s << "[";
00375   if(z.get_ns() == 0) {  s << "0 time"; }
00376   else {
00377     if(years!=0) {  s << (prev? cSt:"") << years << " years";   prev=true; }
00378     if(days!=0)  {  s << (prev? cSt:"") << days  << " days";    prev=true; }
00379     if(hours!=0) {  s << (prev? cSt:"") << hours << " hours";   prev=true; }
00380     if(mins!=0)  {  s << (prev? cSt:"") << mins  << " mins";    prev=true; }
00381     if(secs!=0)  {  s << (prev? cSt:"") << secs  << " secs";    prev=true; }
00382     if(ms!=0)    {  s << (prev? cSt:"") << ms    << " ms";      prev=true; }
00383     if(us!=0)    {  s << (prev? cSt:"") << us    << " us";      prev=true; }
00384     if(ns!=0)    {  s << (prev? cSt:"") << ns    << " ns";      prev=true; }
00385   }
00386   s << "]";
00387   return s;
00388 }
00389 
00390 ostream& operator<<(ostream&s, timeStamp z) {
00391   time_t s1970 = static_cast<time_t>(z.getI(timeUnit::sec(), timeBase::b1970()));
00392   int64_t nsI1970 = static_cast<int64_t>(s1970) * I64_C(1000000000);
00393   if(! z.isInitialized()) {  
00394     s << "[uninitialized]";
00395   } else if(z < timeStamp(0, timeUnit::year(), timeBase::b1970())) {
00396     timeLength tl(z - timeStamp(0, timeUnit::year(), timeBase::b1970()));
00397     // setting the oct flag is just a hack to tell the timeLength ostream<<
00398     // to print the time compressed
00399 #if !defined(os_windows)
00400     ostream::fmtflags oldflags;
00401 #else
00402     long oldflags;  // irix,nt environment doesn't define ostream::fmtflags
00403 #endif
00404     oldflags = s.flags(ostream::oct);
00405     s << "[1970 + " << tl << "]"; 
00406     s.flags(oldflags);
00407   } else {
00408     int64_t nstot = z.getI(timeUnit::ns(), timeBase::b1970());
00409     int64_t nsrem = nstot - nsI1970;
00410     char dateStr[50];
00411     s1970 = mktime(localtime(&s1970));
00412     strcpy(dateStr, ctime(&s1970));
00413     char *p = strstr(dateStr, "\n");  // erase the nl
00414     if(p != NULL) {  *p++ = 0;   *p = 0; }
00415     char strFmt[30];
00416     insCommas(static_cast<int>(nsrem), strFmt);
00417     s << "[" << dateStr << "  " << strFmt << "ns]";
00418   }
00419   return s;
00420 }
00421 
00422 ostream& operator<<(ostream&s, relTimeStamp z) {
00423   if(! z.isInitialized()) {  
00424     s << "[uninitialized]";
00425   } else {
00426     timeLength t(z.getI(timeUnit::ns()), timeUnit::ns());
00427     s << t;
00428   }
00429   return s;
00430 }
00431 
00432 // timeLength / timeLength = double
00433 double operator/(const timeLength& a, const timeLength& b) {
00434   assert(a.isInitialized() && b.isInitialized());
00435   return static_cast<double>(a.get_ns()) / static_cast<double>(b.get_ns());
00436 }
00437 
00438 bool timeParent::isInitialized() const {  
00439   if(get_ns() == uninitializedValue) return false;
00440   else return true;
00441 }
00442 
00443 timeParent::timeParent() 
00444   : ns(uninitializedValue) 
00445 { 
00446 }
00447 
00448 timeParent::timeParent(int64_t _ns) 
00449   : ns(_ns) 
00450 { 
00451 }
00452 
00453 COMMON_EXPORT timeStamp::timeStamp(int64_t iTime, const timeUnit &u, timeBase b) 
00454   : timeParent()
00455 {
00456   initI(iTime, u, b);
00457 }
00458 
00459 COMMON_EXPORT timeStamp::timeStamp(int iTime, const timeUnit &u, timeBase b) 
00460   : timeParent() 
00461 {
00462   initI(iTime, u, b);
00463 }
00464 
00465 COMMON_EXPORT relTimeStamp::relTimeStamp(int64_t iTime, const timeUnit &u) : timeParent() {
00466   initI(iTime, u);
00467 }
00468 
00469 COMMON_EXPORT relTimeStamp::relTimeStamp(int iTime, const timeUnit &u) : timeParent() {
00470   initI(iTime, u);
00471 }
00472 
00473 COMMON_EXPORT timeLength::timeLength(int64_t iTime, const timeUnit &u)
00474   : timeParent() 
00475 {
00476   initI(iTime, u);
00477 }
00478 
00479 COMMON_EXPORT timeLength::timeLength(int iTime, const timeUnit &u)  
00480   : timeParent() 
00481 {
00482   initI(static_cast<int64_t>(iTime), u);
00483 }
00484 
00485 COMMON_EXPORT timeStamp::timeStamp(int64_t ns_)
00486   : timeParent(ns_)
00487 {
00488 }
00489 
00490 const timeLength &timeLength::Zero() {
00491   if(_zero == NULL) _zero = ZeroHelp();
00492   return *_zero;
00493 }
00494 const timeLength &timeLength::ns() {
00495   if(_ns == NULL) _ns = nsHelp();
00496   return *_ns;
00497 }
00498 const timeLength &timeLength::us() {
00499   if(_us == NULL) _us = usHelp();
00500   return *_us;
00501 }
00502 const timeLength &timeLength::ms() {
00503   if(_ms == NULL) _ms = msHelp();
00504   return *_ms;
00505 }
00506 const timeLength &timeLength::sec() {
00507   if(_sec == NULL) _sec = secHelp();
00508   return *_sec;
00509 }
00510 const timeLength &timeLength::minute() {
00511   if(_minute == NULL) _minute = minHelp();
00512   return *_minute;
00513 }
00514 const timeLength &timeLength::hour() {
00515   if(_hour == NULL) _hour = hourHelp();
00516   return *_hour;
00517 }
00518 const timeLength &timeLength::day() {
00519   if(_day == NULL) _day = dayHelp();
00520   return *_day;
00521 }
00522 const timeLength &timeLength::year() {
00523   if(_year == NULL) _year = yearHelp();
00524   return *_year;
00525 }
00526 const timeLength &timeLength::leapYear() {
00527   if(_leapYear == NULL) _leapYear = leapYearHelp();
00528   return *_leapYear;
00529 }
00530 
00531 // timeStamp +=/-= timeLength
00532 const timeStamp operator+=(timeStamp &ts, timeLength tl) {
00533   assert(ts.isInitialized() && tl.isInitialized());
00534   ts.assign(ts.get_ns() + tl.get_ns());
00535   return ts;
00536 }
00537 COMMON_EXPORT const timeStamp operator-=(timeStamp &ts, timeLength tl) {
00538   assert(ts.isInitialized() && tl.isInitialized());
00539   ts.assign(ts.get_ns() - tl.get_ns());
00540   return ts;
00541 }
00542 
00543 // timeLength +=/-= timeLength
00544 const timeLength operator+=(timeLength &t, timeLength tl) {
00545   assert(t.isInitialized() && tl.isInitialized());
00546   t.assign(t.get_ns() + tl.get_ns());
00547   return t;
00548 }
00549 const timeLength operator-=(timeLength &t, timeLength tl) {
00550   assert(t.isInitialized() && tl.isInitialized());
00551   t.assign(t.get_ns() - tl.get_ns());
00552   return t;
00553 }
00554 
00555 // timeLength *=, /= double
00556 const timeLength operator*=(timeLength &t, double d) {
00557   assert(t.isInitialized());
00558   t.assign(static_cast<int64_t>(static_cast<double>(t.get_ns()) * d));
00559   return t;
00560 }
00561 const timeLength operator/=(timeLength &t, double d) {
00562   assert(t.isInitialized());
00563   t.assign(static_cast<int64_t>(static_cast<double>(t.get_ns()) / d));
00564   return t;
00565 }
00566 
00567 // - timeLength
00568 const timeLength operator-(const timeLength &t) {
00569   assert(t.isInitialized());
00570   return timeLength(-t.get_ns());
00571 }
00572 
00573 // timeStamp - timeStamp = timeLength  ;  the length of time between time stamps
00574 const timeLength operator-(const timeStamp& a, const timeStamp& b) {
00575   assert(a.isInitialized() && b.isInitialized());
00576   return timeLength(a.get_ns() - b.get_ns());
00577 }
00578 
00579 // timeStamp +/- timeLength = timeStamp
00580 const timeStamp operator+(const timeStamp& a, const timeLength& b) {
00581   assert(a.isInitialized() && b.isInitialized());
00582   return timeStamp(a.get_ns() + b.get_ns());
00583 }
00584 const timeStamp operator-(const timeStamp& a, const timeLength& b) {
00585   assert(a.isInitialized() && b.isInitialized());
00586   return timeStamp(a.get_ns() - b.get_ns());
00587 }
00588 
00589 // timeLength + timeStamp = timeStamp
00590 const timeStamp operator+(const timeLength& a, const timeStamp& b) {
00591   assert(a.isInitialized() && b.isInitialized());
00592   return timeStamp(a.get_ns() + b.get_ns());
00593 }
00594 // timeLength - timeStamp doesn't make sense, ie. 3 days - Mar 9 = ?
00595 
00596 // timeLength +/- timeLength = timeLength
00597 const timeLength operator+(const timeLength& a, const timeLength& b) {  
00598   assert(a.isInitialized() && b.isInitialized());
00599   return timeLength(a.get_ns() + b.get_ns());
00600 }
00601 const timeLength operator-(const timeLength& a, const timeLength& b) {
00602   assert(a.isInitialized() && b.isInitialized());
00603   return timeLength(a.get_ns() - b.get_ns());
00604 }
00605 
00606 // timeLength */ double = timeLength
00607 const timeLength operator*(const timeLength& a, const double b) {
00608   assert(a.isInitialized());
00609   return timeLength(static_cast<int64_t>(static_cast<double>(a.get_ns()) * b));
00610 }
00611 const timeLength operator/(const timeLength& a, const double b) {
00612   assert(a.isInitialized());
00613   return timeLength(static_cast<int64_t>(static_cast<double>(a.get_ns()) / b));
00614 }
00615 
00616 // double */ timeLength = timeLength
00617 const timeLength operator*(const double a, const timeLength& b) {
00618   assert(b.isInitialized());
00619   return timeLength(static_cast<int64_t>(a * static_cast<double>(b.get_ns())));
00620 }
00621 const timeLength operator/(const double a, const timeLength& b) {
00622   assert(b.isInitialized());
00623   return timeLength(static_cast<int64_t>(a / static_cast<double>(b.get_ns())));
00624 }
00625 
00626 // Be careful if writing * operators because Time is based at nanosecond
00627 // level, which can overflow when multiplying times that seem small
00628 // eg. Time(1,timeUnit::day) * Time(2,timeUnit::day) will overflow
00629 
00630 // timeStamp @ timeStamp = bool
00631 bool operator==(const timeStamp& a, const timeStamp& b) {
00632   assert(a.isInitialized() && b.isInitialized());
00633   return (a.get_ns() == b.get_ns());
00634 }
00635 bool operator!=(const timeStamp& a, const timeStamp& b) {
00636   assert(a.isInitialized() && b.isInitialized());
00637   return (a.get_ns() != b.get_ns());
00638 }
00639 bool operator>(const timeStamp& a, const timeStamp& b) {
00640   assert(a.isInitialized() && b.isInitialized());
00641   return (a.get_ns() > b.get_ns());
00642 }
00643 bool operator>=(const timeStamp& a, const timeStamp& b) {
00644   assert(a.isInitialized() && b.isInitialized());
00645   return (a.get_ns() >= b.get_ns());
00646 }
00647 bool operator<(const timeStamp& a, const timeStamp& b) {
00648   assert(a.isInitialized() && b.isInitialized());
00649   return (a.get_ns() < b.get_ns());
00650 }
00651 bool operator<=(const timeStamp& a, const timeStamp& b) {
00652   assert(a.isInitialized() && b.isInitialized());
00653   return (a.get_ns() <= b.get_ns());
00654 }
00655 
00656 timeStamp earlier(const timeStamp& a, const timeStamp& b) {
00657   assert(a.isInitialized() && b.isInitialized());
00658   if(a <= b)  return a;
00659   else        return b;
00660 }
00661 
00662 timeStamp later(const timeStamp& a, const timeStamp& b) {
00663   assert(a.isInitialized() && b.isInitialized());
00664   if(a >= b)  return a;
00665   else        return b;
00666 }
00667 
00668 // timeLength @ timeLength = bool
00669 bool operator==(const timeLength& a, const timeLength& b) {
00670   assert(a.isInitialized() && b.isInitialized());
00671   return (a.get_ns() == b.get_ns());
00672 }
00673 bool operator!=(const timeLength& a, const timeLength& b) {
00674   assert(a.isInitialized() && b.isInitialized());
00675   return (a.get_ns() != b.get_ns());
00676 }
00677 bool operator>(const timeLength& a, const timeLength& b) {
00678   assert(a.isInitialized() && b.isInitialized());
00679   return (a.get_ns() > b.get_ns());
00680 }
00681 bool operator>=(const timeLength& a, const timeLength& b) {
00682   assert(a.isInitialized() && b.isInitialized());
00683   return (a.get_ns() >= b.get_ns());
00684 }
00685 bool operator<(const timeLength& a, const timeLength& b) {
00686   assert(a.isInitialized() && b.isInitialized());
00687   return (a.get_ns() < b.get_ns());
00688 }
00689 bool operator<=(const timeLength& a, const timeLength& b) {
00690   assert(a.isInitialized() && b.isInitialized());
00691   return (a.get_ns() <= b.get_ns());
00692 }
00693 
00694 
00695 timeLength minimum(const timeLength& a, const timeLength& b) {  
00696   assert(a.isInitialized() && b.isInitialized());
00697   if(a<=b)  return a;
00698   else      return b;
00699 }
00700 
00701 timeLength maximum(const timeLength& a, const timeLength& b) {  
00702   assert(a.isInitialized() && b.isInitialized());
00703   if(a>=b)  return a;
00704   else      return b;
00705 }
00706 
00707 const timeLength abs(const timeLength& a) {  
00708   assert(a.isInitialized());
00709   return maximum(a,-a);
00710 }
00711 
00712 // need to put this here so can get at timeStamp
00713 timeBase::timeBase(timeStamp mark) {
00714   ns2StdBaseMark = -mark.get_ns();  // in Std base
00715   // eg. (2001) 1 year of ns's -> -1 year of ns's to internalTimeBaseMark
00716 }
00717 
00718 // relTimeStamp +=/-= timeLength
00719 const relTimeStamp operator+=(relTimeStamp &ts, timeLength tl) {
00720   assert(ts.isInitialized() && tl.isInitialized());
00721   ts.assign(ts.get_ns() + tl.get_ns());
00722   return ts;
00723 }
00724 const relTimeStamp operator-=(relTimeStamp &ts, timeLength tl) {
00725   assert(ts.isInitialized() && tl.isInitialized());
00726   ts.assign(ts.get_ns() - tl.get_ns());
00727   return ts;
00728 }
00729 
00730 // relTimeStamp - relTimeStamp = timeLength  ;  the length of time between time stamps
00731 const timeLength operator-(const relTimeStamp& a, const relTimeStamp& b) {
00732   assert(a.isInitialized() && b.isInitialized());
00733   return timeLength(a.get_ns() - b.get_ns());
00734 }
00735 
00736 // relTimeStamp +/- relTimeLength = relTimeStamp
00737 const relTimeStamp operator+(const relTimeStamp& a, const timeLength& b) {
00738   assert(a.isInitialized() && b.isInitialized());
00739   return relTimeStamp(a.get_ns() + b.get_ns());
00740 }
00741 const relTimeStamp operator-(const relTimeStamp& a, const timeLength& b) {
00742   assert(a.isInitialized() && b.isInitialized());
00743   return relTimeStamp(a.get_ns() - b.get_ns());
00744 }
00745 
00746 // timeLength + relTimeStamp = relTimeStamp
00747 const relTimeStamp operator+(const timeLength& a, const relTimeStamp& b) {
00748   assert(a.isInitialized() && b.isInitialized());
00749   return relTimeStamp(a.get_ns() + b.get_ns());
00750 }
00751 // timeLength - timeStamp doesn't make sense, ie. 3 days - Mar 9 = ?
00752 
00753 
00754 // Be careful if writing * operators because Time is based at nanosecond
00755 // level, which can overflow when multiplying times that seem small
00756 // eg. Time(1,timeUnit::day) * Time(2,timeUnit::day) will overflow
00757 
00758 // relTimeStamp @ relTimeStamp = bool
00759 bool operator==(const relTimeStamp& a, const relTimeStamp& b) {
00760   assert(a.isInitialized() && b.isInitialized());
00761   return (a.get_ns() == b.get_ns());
00762 }
00763 bool operator!=(const relTimeStamp& a, const relTimeStamp& b) {
00764   assert(a.isInitialized() && b.isInitialized());
00765   return (a.get_ns() != b.get_ns());
00766 }
00767 bool operator>(const relTimeStamp& a, const relTimeStamp& b) {
00768   assert(a.isInitialized() && b.isInitialized());
00769   return (a.get_ns() > b.get_ns());
00770 }
00771 bool operator>=(const relTimeStamp& a, const relTimeStamp& b) {
00772   assert(a.isInitialized() && b.isInitialized());
00773   return (a.get_ns() >= b.get_ns());
00774 }
00775 bool operator<(const relTimeStamp& a, const relTimeStamp& b) {
00776   assert(a.isInitialized() && b.isInitialized());
00777   return (a.get_ns() < b.get_ns());
00778 }
00779 bool operator<=(const relTimeStamp& a, const relTimeStamp& b) {
00780   assert(a.isInitialized() && b.isInitialized());
00781   return (a.get_ns() <= b.get_ns());
00782 }
00783 
00784 relTimeStamp earlier(const relTimeStamp& a, const relTimeStamp& b) {
00785   assert(a.isInitialized() && b.isInitialized());
00786   if(a <= b)  return a;
00787   else        return b;
00788 }
00789 
00790 relTimeStamp later(const relTimeStamp& a, const relTimeStamp& b) {
00791   assert(a.isInitialized() && b.isInitialized());
00792   if(a >= b)  return a;
00793   else        return b;
00794 }
00795 
00796 timeUnit::timeUnit(fraction _ns_per_unit) : ns_per_unit(_ns_per_unit), 
00797   units_per_ns(ns_per_unit.reciprocal()) {
00798   ns_per_unit.reduce();
00799   units_per_ns.reduce();
00800 }
00801 
00802 // Selectors
00803 fraction timeUnit::get_ns_per_unit()  const {  return ns_per_unit;   }
00804 fraction timeUnit::get_units_per_ns() const {  return units_per_ns;  }
00805 
00806 // Mutators
00807 void timeUnit::set_ns_per_unit(const fraction &nspu) {
00808   ns_per_unit = nspu;
00809   units_per_ns = ns_per_unit.reciprocal();
00810 }
00811 
00812 const timeUnit &timeUnit::ns() {
00813   if(_ns == NULL)  _ns = nsHelp();
00814   return *_ns;
00815 }
00816 const timeUnit &timeUnit::us() {
00817   if(_us == NULL)  _us = usHelp();
00818   return *_us;
00819 }
00820 const timeUnit &timeUnit::ms() {
00821   if(_ms==NULL)  _ms = msHelp();
00822   return *_ms;
00823 }
00824 const timeUnit &timeUnit::sec() {
00825   if(_sec==NULL) _sec = secHelp();
00826   return *_sec;
00827 }
00828 const timeUnit &timeUnit::minute() {
00829   if(_minute==NULL)  _minute = minHelp();
00830   return *_minute;
00831 }
00832 const timeUnit &timeUnit::hour() {
00833   if(_hour==NULL) _hour = hourHelp();
00834   return *_hour;
00835 }
00836 const timeUnit &timeUnit::day() {
00837   if(_day==NULL) _day = dayHelp();
00838   return *_day;
00839 }
00840 const timeUnit &timeUnit::year() {
00841   if(_year==NULL) _year = yearHelp();
00842   return *_year;
00843 }
00844 const timeUnit &timeUnit::leapYear() {
00845   if(_leapYear==NULL) _leapYear = leapYearHelp();
00846   return *_leapYear;
00847 }
00848 
00849 
00850 timeBase::timeBase(int64_t ns2stdMark) {
00851   ns2StdBaseMark = ns2stdMark;
00852 }
00853 
00854 int64_t timeBase::get_ns2StdBaseMark() const {
00855   return ns2StdBaseMark;
00856 }
00857 
00858 int64_t timeBase::cvtTo_bStd(int64_t ns) const {
00859   // eg. 1994, b1970 -> bStd:  24 yrs - 30 yrs = -6 yrs
00860   return ns - ns2StdBaseMark;  
00861 }
00862 
00863 double  timeBase::cvtFrom_bStd(double ns) const {
00864   // eg. 1994, bStd -> b1970:  -6 yrs + 30 yrs = 24 yrs
00865    return ns + static_cast<double>(ns2StdBaseMark);
00866 }
00867 
00868 int64_t timeBase::cvtFrom_bStd(int64_t ns) const {
00869   return ns + ns2StdBaseMark;
00870 }
00871 
00872 const timeBase &timeBase::bStd() {
00873   if(_bStd == NULL) _bStd = bStdHelp();
00874   return *_bStd;
00875 }
00876 const timeBase &timeBase::b1970() {
00877   if(_b1970 == NULL) _b1970 = b1970Help();
00878   return *_b1970;
00879 }
00880 const timeBase &timeBase::bNone() {
00881   return bStd();
00882 }
00883 
00884 
00885 int64_t timeParent::get_ns() const {
00886   return ns;
00887 }
00888 
00889 void timeParent::assign(const int64_t v) 
00890 {  
00891   ns = v;  
00892 }
00893 
00894 timeStamp::timeStamp() { }
00895 
00896 double timeStamp::getD(const timeUnit &u, timeBase b) const {
00897   return u.cvtFrom_nsD( b.cvtFrom_bStd(get_ns()));
00898 }
00899 
00900 int64_t timeStamp::getI(const timeUnit &u, timeBase b) const {
00901   return u.cvtFrom_nsI( b.cvtFrom_bStd(get_ns()));
00902 }
00903 
00904 const timeStamp &timeStamp::ts1970() {
00905   if(_ts1970 == NULL)  _ts1970 = ts1970Help();
00906   return *_ts1970;
00907 }
00908 
00909 const timeStamp &timeStamp::tsStd() {
00910   if(_tsStd == NULL)  _tsStd = tsStdHelp();
00911   return *_tsStd;
00912 }
00913 
00914 const timeStamp &timeStamp::ts1800() {
00915   if(_ts1800 == NULL)  _ts1800 = ts1800Help();
00916   return *_ts1800;
00917 }
00918 
00919 const timeStamp &timeStamp::ts2200() {
00920   if(_ts2200 == NULL)  _ts2200 = ts2200Help();
00921   return *_ts2200;
00922 }
00923 
00924 const timeStamp &timeStamp::tsLongAgoTime() {
00925   return timeStamp::ts1800();
00926 }
00927 
00928 const timeStamp &timeStamp::tsFarOffTime() {
00929   return timeStamp::ts2200();
00930 }
00931 
00932 COMMON_EXPORT relTimeStamp::relTimeStamp() { }
00933 
00934 double relTimeStamp::getD(const timeUnit &u) const {
00935   return u.cvtFrom_nsD(get_ns());
00936 }
00937 
00938 int64_t relTimeStamp::getI(const timeUnit &u) const {
00939   return u.cvtFrom_nsI( get_ns());
00940 }
00941 
00942 relTimeStamp::relTimeStamp(int64_t ns_) : timeParent(ns_) { }
00943 
00944 const relTimeStamp &relTimeStamp::Zero() {
00945   if(_Zero == NULL)  _Zero = ZeroHelp();
00946   return *_Zero;
00947 }
00948 
00949 COMMON_EXPORT timeLength::timeLength() 
00950 {
00951 }
00952 
00953 double timeLength::getD(const timeUnit &u) const 
00954 {
00955     return u.cvtFrom_nsD( get_ns());
00956 }
00957 
00958 int64_t timeLength::getI(const timeUnit &u) const 
00959 {
00960   return u.cvtFrom_nsI( get_ns());
00961 }
00962 
00963 timeLength::timeLength(int64_t ns_) : timeParent(ns_)
00964 {
00965 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1