timestamp_base.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <ecl/config.hpp>
00014 #include <ecl/exceptions/standard_exception.hpp>
00015 #include "../../include/ecl/time/timestamp_base.hpp"
00016 
00017 /*****************************************************************************
00018 ** Namespaces
00019 *****************************************************************************/
00020 
00021 namespace ecl {
00022 
00023 /*****************************************************************************
00024 ** Implementation
00025 *****************************************************************************/
00026 
00027 TimeStampBase::TimeStampBase (const double& decimal_time_value) ecl_assert_throw_decl(StandardException)
00028 {
00029     ecl_assert_throw( decimal_time_value >= 0.0, StandardException(LOC,InvalidInputError,"Timestamps must always be positive (design requirement), make sure your input argument is also positive.") );
00030     time.tv_sec = static_cast<long>(decimal_time_value);
00031     time.tv_nsec = (decimal_time_value - static_cast<long>(decimal_time_value))*1000000000L;
00032 }
00033 
00034 TimeStampBase::TimeStampBase (const time_t& seconds, const long& nanoseconds) ecl_assert_throw_decl(StandardException) {
00035     ecl_assert_throw( (seconds >= 0) && (nanoseconds >= 0), StandardException(LOC,InvalidInputError,"Timestamps must always be positive (design requirement), make sure your input arguments are also positive.") );
00036     time.tv_sec = seconds;
00037     time.tv_nsec = nanoseconds;
00038 }
00039 
00040 /*****************************************************************************
00041 ** Implementation [Stamps]
00042 *****************************************************************************/
00043 
00044 const TimeStampBase& TimeStampBase::stamp (const double& decimal_time_value) ecl_assert_throw_decl(StandardException) {
00045     ecl_assert_throw( decimal_time_value >= 0.0, StandardException(LOC,InvalidInputError,"Timestamps must always be positive (design requirement), make sure your input argument is also positive.") );
00046     time.tv_sec = static_cast<time_t>(decimal_time_value);
00047     time.tv_nsec = (decimal_time_value - static_cast<long>(decimal_time_value))*1000000000L;
00048     return (*this);
00049 }
00050 
00051 const TimeStampBase& TimeStampBase::stamp (const time_t& seconds, const long& nanoseconds) ecl_assert_throw_decl(StandardException) {
00052     ecl_assert_throw( (seconds >= 0) && (nanoseconds >= 0), StandardException(LOC,InvalidInputError,"Timestamps must always be positive (design requirement), make sure your input arguments are also positive.") );
00053     time.tv_sec = seconds;
00054     time.tv_nsec = nanoseconds;
00055     return (*this);
00056 }
00057 
00058 /*****************************************************************************
00059 ** Implementation [Comparison Operators]
00060 *****************************************************************************/
00061 
00062 bool TimeStampBase::operator==(const TimeStampBase& time_stamp ) {
00063     if( ( time_stamp.time.tv_sec == time.tv_sec ) && ( time_stamp.time.tv_nsec == time.tv_nsec ) ) {
00064         return true;
00065     } else {
00066         return false;
00067     }
00068 }
00069 
00070 bool TimeStampBase::operator!=(const TimeStampBase& time_stamp ) {
00071     if( ( time_stamp.time.tv_sec == time.tv_sec ) && ( time_stamp.time.tv_nsec == time.tv_nsec ) ) {
00072         return false;
00073     } else {
00074         return true;
00075     }
00076 }
00077 
00078 bool TimeStampBase::operator<=(const TimeStampBase& time_stamp ) {
00079     if( time.tv_sec > time_stamp.time.tv_sec ) {
00080         return false;
00081     } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00082         if ( time.tv_nsec > time_stamp.time.tv_nsec ) {
00083             return false;
00084         }
00085     }
00086     return true;
00087 }
00088 
00089 bool TimeStampBase::operator>=(const TimeStampBase& time_stamp ) {
00090     if( time.tv_sec < time_stamp.time.tv_sec ) {
00091         return false;
00092     } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00093         if ( time.tv_nsec < time_stamp.time.tv_nsec ) {
00094             return false;
00095         }
00096     }
00097     return true;
00098 }
00099 
00100 bool TimeStampBase::operator<(const TimeStampBase& time_stamp ) {
00101     if( time.tv_sec > time_stamp.time.tv_sec ) {
00102         return false;
00103     } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00104         if ( time.tv_nsec >= time_stamp.time.tv_nsec ) {
00105             return false;
00106         }
00107     }
00108     return true;
00109 }
00110 
00111 bool TimeStampBase::operator>(const TimeStampBase& time_stamp ) {
00112     if( time.tv_sec < time_stamp.time.tv_sec ) {
00113         return false;
00114     } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00115         if ( time.tv_nsec <= time_stamp.time.tv_nsec ) {
00116             return false;
00117         }
00118     }
00119     return true;
00120 }
00121 /*****************************************************************************
00122 ** Implementation [TimeStamp][Mathematical Operators]
00123 *****************************************************************************/
00124 
00125 void TimeStampBase::operator+=(const TimeStampBase& time_stamp ) {
00126     time.tv_sec += time_stamp.time.tv_sec;
00127     int64 nsec = time.tv_nsec + time_stamp.time.tv_nsec;
00128     time.tv_nsec = nsec%1000000000L;
00129     if ( nsec > 1000000000L ) {
00130         time.tv_sec += 1;
00131     }
00132 }
00133 
00134 TimeStampBase TimeStampBase::operator+(const TimeStampBase& time_stamp ) {
00135     long sec = time.tv_sec + time_stamp.time.tv_sec;
00136     int64 nsec = time.tv_nsec + time_stamp.time.tv_nsec;
00137     if ( nsec > 1000000000L ) {
00138         sec += 1;
00139         nsec = nsec%1000000000L;
00140     }
00141     return TimeStampBase(sec,nsec);
00142 }
00143 
00144 void TimeStampBase::operator-=(const TimeStampBase& time_stamp ) ecl_assert_throw_decl(StandardException) {
00145     time_t sec = time.tv_sec - time_stamp.time.tv_sec;
00146     long nsec = time.tv_nsec - time_stamp.time.tv_nsec;
00147 
00148     ecl_assert_throw( ( ( sec > 0 ) || ((sec == 0) && (nsec >= 0)) ), StandardException(LOC,InvalidInputError,"Timestamps must always be positive (design requirement), possibly you have your timestamps in the wrong order.") );
00149 
00150     if ( nsec < 0 ) {
00151         sec -= 1;
00152         nsec += 1000000000L;
00153     }
00154     time.tv_sec = sec;
00155     time.tv_nsec = nsec;
00156 }
00157 
00158 TimeStampBase TimeStampBase::operator-(const TimeStampBase& time_stamp ) ecl_assert_throw_decl(StandardException) {
00159     time_t sec = time.tv_sec - time_stamp.time.tv_sec;
00160     long nsec = time.tv_nsec - time_stamp.time.tv_nsec;
00161 
00162     ecl_assert_throw( ( ( sec > 0 ) || ((sec == 0) && (nsec >= 0)) ), StandardException(LOC,InvalidInputError,"Timestamps must always be positive (design requirement), possibly you have your timestamps in the wrong order.") );
00163 
00164     if ( nsec < 0 ) {
00165         sec -= 1;
00166         nsec += 1000000000L;
00167     }
00168     return TimeStampBase(sec,nsec);
00169 }
00170 
00171 
00172 
00173 } // namespace ecl
00174 


ecl_time
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:17