Go to the documentation of this file.00001
00009
00010
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
00019
00020
00021 namespace ecl {
00022
00023
00024
00025
00026
00027 TimeStampBase::TimeStampBase (const double& decimal_time_value) ecl_assert_throw_decl(StandardException)
00028 {
00029 time.tv_sec = static_cast<long>(decimal_time_value);
00030 time.tv_nsec = (decimal_time_value - static_cast<long>(decimal_time_value))*1000000000L;
00031 }
00032
00033 TimeStampBase::TimeStampBase (const time_t& seconds, const long& nanoseconds) ecl_assert_throw_decl(StandardException) {
00034 ecl_assert_throw( ( (seconds > 0) && (nanoseconds >= 0) ) ||
00035 ( (seconds < 0) && (nanoseconds <= 0) ) ||
00036 ( (seconds == 0) ),
00037 StandardException(LOC,InvalidInputError,"Positive timestamps require positive nanoseconds, negative timestamps require negative nanoseconds.") );
00038 time.tv_sec = seconds;
00039 time.tv_nsec = nanoseconds;
00040 }
00041
00042
00043
00044
00045
00046 const TimeStampBase& TimeStampBase::stamp (const double& decimal_time_value) ecl_assert_throw_decl(StandardException) {
00047 time.tv_sec = static_cast<time_t>(decimal_time_value);
00048 time.tv_nsec = (decimal_time_value - static_cast<long>(decimal_time_value))*1000000000L;
00049 return (*this);
00050 }
00051
00052 const TimeStampBase& TimeStampBase::stamp (const time_t& seconds, const long& nanoseconds) ecl_assert_throw_decl(StandardException) {
00053 ecl_assert_throw( ( (seconds > 0) && (nanoseconds >= 0) ) ||
00054 ( (seconds < 0) && (nanoseconds <= 0) ) ||
00055 ( (seconds == 0) ),
00056 StandardException(LOC,InvalidInputError,"Positive timestamps require positive nanoseconds, negative timestamps require negative nanoseconds.") );
00057 time.tv_sec = seconds;
00058 time.tv_nsec = nanoseconds;
00059 return (*this);
00060 }
00061
00062
00063
00064
00065
00066 bool TimeStampBase::operator==(const TimeStampBase& time_stamp ) {
00067 if( ( time_stamp.time.tv_sec == time.tv_sec ) && ( time_stamp.time.tv_nsec == time.tv_nsec ) ) {
00068 return true;
00069 } else {
00070 return false;
00071 }
00072 }
00073
00074 bool TimeStampBase::operator!=(const TimeStampBase& time_stamp ) {
00075 if( ( time_stamp.time.tv_sec == time.tv_sec ) && ( time_stamp.time.tv_nsec == time.tv_nsec ) ) {
00076 return false;
00077 } else {
00078 return true;
00079 }
00080 }
00081
00082 bool TimeStampBase::operator<=(const TimeStampBase& time_stamp ) {
00083 if( time.tv_sec > time_stamp.time.tv_sec ) {
00084 return false;
00085 } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00086 if ( time.tv_nsec > time_stamp.time.tv_nsec ) {
00087 return false;
00088 }
00089 }
00090 return true;
00091 }
00092
00093 bool TimeStampBase::operator>=(const TimeStampBase& time_stamp ) {
00094 if( time.tv_sec < time_stamp.time.tv_sec ) {
00095 return false;
00096 } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00097 if ( time.tv_nsec < time_stamp.time.tv_nsec ) {
00098 return false;
00099 }
00100 }
00101 return true;
00102 }
00103
00104 bool TimeStampBase::operator<(const TimeStampBase& time_stamp ) {
00105 if( time.tv_sec > time_stamp.time.tv_sec ) {
00106 return false;
00107 } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00108 if ( time.tv_nsec >= time_stamp.time.tv_nsec ) {
00109 return false;
00110 }
00111 }
00112 return true;
00113 }
00114
00115 bool TimeStampBase::operator>(const TimeStampBase& time_stamp ) {
00116 if( time.tv_sec < time_stamp.time.tv_sec ) {
00117 return false;
00118 } else if ( time.tv_sec == time_stamp.time.tv_sec ) {
00119 if ( time.tv_nsec <= time_stamp.time.tv_nsec ) {
00120 return false;
00121 }
00122 }
00123 return true;
00124 }
00125
00126
00127
00128
00129 void TimeStampBase::operator+=(const TimeStampBase& time_stamp ) {
00130 time.tv_sec += time_stamp.time.tv_sec;
00131 int64 nsec = time.tv_nsec + time_stamp.time.tv_nsec;
00132 if ( nsec >= 1000000000L ) {
00133 time.tv_sec += 1;
00134 nsec -= 1000000000L;
00135 } else if ( nsec <= -1000000000L ) {
00136 time.tv_sec -= 1;
00137 nsec += 1000000000L;
00138 }
00139 if ( ( time.tv_sec > 0 ) && ( nsec < 0 ) ) {
00140 time.tv_sec -= 1;
00141 nsec = 1000000000L + nsec;
00142 }
00143 if ( ( time.tv_sec < 0 ) && ( nsec > 0 ) ) {
00144 time.tv_sec += 1;
00145 nsec = nsec - 1000000000L;
00146 }
00147 time.tv_nsec = nsec;
00148 }
00149
00150 TimeStampBase TimeStampBase::operator+(const TimeStampBase& time_stamp ) {
00151 long sec = time.tv_sec + time_stamp.time.tv_sec;
00152 int64 nsec = time.tv_nsec + time_stamp.time.tv_nsec;
00153 if ( nsec >= 1000000000L ) {
00154 sec += 1;
00155 nsec -= 1000000000L;
00156 } else if ( nsec <= -1000000000L ) {
00157 sec -= 1;
00158 nsec += 1000000000L;
00159 }
00160 if ( ( sec > 0 ) && ( nsec < 0 ) ) {
00161 sec -= 1;
00162 nsec = 1000000000L + nsec;
00163 }
00164 if ( ( sec < 0 ) && ( nsec > 0 ) ) {
00165 sec += 1;
00166 nsec = nsec - 1000000000L;
00167 }
00168 return TimeStampBase(sec,nsec);
00169 }
00170
00171 void TimeStampBase::operator-=(const TimeStampBase& time_stamp ) ecl_assert_throw_decl(StandardException) {
00172 time_t sec = time.tv_sec - time_stamp.time.tv_sec;
00173 long nsec = time.tv_nsec - time_stamp.time.tv_nsec;
00174
00175 if ( (sec > 0) && ( nsec < 0 )) {
00176 sec -= 1;
00177 nsec += 1000000000L;
00178 } else if ( (sec < 0) && ( nsec > 0 )) {
00179 sec += 1;
00180 nsec -= 1000000000L;
00181 }
00182 time.tv_sec = sec;
00183 time.tv_nsec = nsec;
00184 }
00185
00186 TimeStampBase TimeStampBase::operator-(const TimeStampBase& time_stamp ) ecl_assert_throw_decl(StandardException) {
00187 time_t sec = time.tv_sec - time_stamp.time.tv_sec;
00188 long nsec = time.tv_nsec - time_stamp.time.tv_nsec;
00189
00190 if ( (sec > 0) && ( nsec < 0 )) {
00191 sec -= 1;
00192 nsec += 1000000000L;
00193 } else if ( (sec < 0) && ( nsec > 0 )) {
00194 sec += 1;
00195 nsec -= 1000000000L;
00196 }
00197 return TimeStampBase(sec,nsec);
00198 }
00199
00200
00201
00202 }
00203