00001 00008 /***************************************************************************** 00009 ** Ifdefs 00010 *****************************************************************************/ 00011 00012 #ifndef ECL_TIME_TIMESTAMP_BASE_HPP_ 00013 #define ECL_TIME_TIMESTAMP_BASE_HPP_ 00014 00015 /***************************************************************************** 00016 ** Includes 00017 *****************************************************************************/ 00018 00019 #include <ecl/time_lite/types.hpp> 00020 #include <ecl/config/macros.hpp> 00021 #include <ecl/exceptions/standard_exception.hpp> 00022 00023 /***************************************************************************** 00024 ** Namespaces 00025 *****************************************************************************/ 00026 00027 namespace ecl { 00028 00029 /***************************************************************************** 00030 ** Implementation 00031 *****************************************************************************/ 00038 class ECL_PUBLIC TimeStampBase { 00039 public: 00040 /********************* 00041 ** Constructors 00042 **********************/ 00043 explicit TimeStampBase() {}; 00054 explicit TimeStampBase (const double& decimal_time_value) ecl_assert_throw_decl(StandardException); 00064 TimeStampBase (const time_t &seconds, const long &nanoseconds) ecl_assert_throw_decl(StandardException); 00065 00066 virtual ~TimeStampBase() {} 00067 00068 /****************************************** 00069 ** Stamps 00070 *******************************************/ 00081 const TimeStampBase& stamp (const double& decimal_time_value) ecl_assert_throw_decl(StandardException); 00092 const TimeStampBase& stamp (const time_t &seconds, const long &nanoseconds) ecl_assert_throw_decl(StandardException); 00093 00094 /****************************************** 00095 ** Accessors 00096 *******************************************/ 00103 long sec() const { return time.tv_sec; } 00110 long msec() const { return time.tv_nsec/1000000L; } 00117 long usec() const { return time.tv_nsec/1000; } 00124 long nsec() const { return time.tv_nsec; } 00125 00132 operator double() const { return ( time.tv_sec + time.tv_nsec*0.000000001); } 00133 00134 /****************************************** 00135 ** Comparison Operators 00136 *******************************************/ 00142 bool operator==(const TimeStampBase& time_stamp); 00148 bool operator!=(const TimeStampBase& time_stamp); 00154 bool operator<=(const TimeStampBase& time_stamp); 00160 bool operator>=(const TimeStampBase& time_stamp); 00166 bool operator<(const TimeStampBase& time_stamp); 00172 bool operator>(const TimeStampBase& time_stamp); 00173 00174 /****************************************** 00175 ** Mathematical Operators 00176 *******************************************/ 00182 TimeStampBase operator+(const TimeStampBase& time_stamp ); 00187 void operator+=(const TimeStampBase& time_stamp); 00197 TimeStampBase operator-(const TimeStampBase& time_stamp ) ecl_assert_throw_decl(StandardException); 00206 void operator-=(const TimeStampBase& time_stamp) ecl_assert_throw_decl(StandardException); 00207 00208 /****************************************** 00209 ** Insertion Operator 00210 *******************************************/ 00211 template <typename OutputStream> 00212 friend OutputStream& operator << ( OutputStream &ostream , const TimeStampBase& time_stamp ); 00213 00214 protected: 00215 TimeStructure time; 00216 }; 00217 00218 00219 /***************************************************************************** 00220 ** Implementation [Insertion Operator] 00221 *****************************************************************************/ 00222 00223 template <typename OutputStream> 00224 OutputStream& operator <<( OutputStream &ostream , const TimeStampBase& time_stamp ) 00225 { 00226 ostream << time_stamp.time.tv_sec << "."; 00227 if ( time_stamp.time.tv_nsec < 10 ) { 00228 ostream << "00000000"; 00229 } else if ( time_stamp.time.tv_nsec < 100 ) { 00230 ostream << "0000000"; 00231 } else if ( time_stamp.time.tv_nsec < 1000 ) { 00232 ostream << "000000"; 00233 } else if ( time_stamp.time.tv_nsec < 10000 ) { 00234 ostream << "00000"; 00235 } else if ( time_stamp.time.tv_nsec < 100000 ) { 00236 ostream << "0000"; 00237 } else if ( time_stamp.time.tv_nsec < 1000000 ) { 00238 ostream << "000"; 00239 } else if ( time_stamp.time.tv_nsec < 10000000 ) { 00240 ostream << "00"; 00241 } else if ( time_stamp.time.tv_nsec < 100000000 ) { 00242 ostream << "0"; 00243 } 00244 ostream << time_stamp.time.tv_nsec; 00245 return ostream; 00246 } 00247 00248 } // namespace ecl 00249 00250 #endif /* ECL_TIME_TIMESTAMP_BASE_HPP_ */ 00251 00252 00253