Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef KOGMO_TIME_HXX
00015 #define KOGMO_TIME_HXX
00016
00017 #include <inttypes.h>
00018 #include <iostream>
00019 #include <string>
00020
00021 #include "kogmo_time.h"
00022
00023 namespace KogniMobil
00024 {
00025
00026
00027 class Timestamp
00028 {
00029 protected:
00030 kogmo_timestamp_t ts;
00031
00032
00033 public:
00034 Timestamp (const kogmo_timestamp_t& t=0)
00035 {
00036 ts = t;
00037 }
00038
00039 Timestamp (std::string str)
00040 {
00041 ts = kogmo_timestamp_from_string (str.c_str());
00042 }
00043
00044 Timestamp& now (void)
00045 {
00046 ts = kogmo_timestamp_now();
00047 return *this;
00048 }
00049
00050 void settime(const kogmo_timestamp_t& t)
00051 {
00052 ts = t;
00053 }
00054
00055 static Timestamp getNow (void)
00056
00057
00058 {
00059 return Timestamp( kogmo_timestamp_now() );
00060 }
00061
00062 void settime(const char *str)
00063 {
00064 ts = kogmo_timestamp_from_string (str);
00065 }
00066
00067 void settime_secs(const unsigned long int& secs, const unsigned long int& nsecs = 0 )
00068 {
00069 ts = (kogmo_timestamp_t) secs * KOGMO_TIMESTAMP_TICKSPERSECOND
00070 + nsecs / KOGMO_TIMESTAMP_NANOSECONDSPERTICK;
00071 }
00072
00073 kogmo_timestamp_t timestamp (void) const
00074 {
00075 return ts;
00076 }
00077
00078 int64_t diffns (const Timestamp& End) const
00079 {
00080 return (End.ts-ts) * KOGMO_TIMESTAMP_NANOSECONDSPERTICK;
00081 }
00082
00083 double diffs (const Timestamp& End) const
00084 {
00085 return (double)(End.ts-ts) / KOGMO_TIMESTAMP_TICKSPERSECOND;
00086 }
00087
00088 void addns (const int64_t& ns)
00089 {
00090 ts += ns / KOGMO_TIMESTAMP_NANOSECONDSPERTICK;
00091 }
00092
00093 void adds (const double& secs)
00094 {
00095 ts += (kogmo_timestamp_t) (secs * KOGMO_TIMESTAMP_TICKSPERSECOND);
00096 }
00097
00098 Timestamp& operator+= (const double& secs)
00099 {
00100 ts += (kogmo_timestamp_t) (secs * KOGMO_TIMESTAMP_TICKSPERSECOND);
00101 return *this;
00102 }
00103
00104 Timestamp& operator-= (const double& secs)
00105 {
00106 ts -= (kogmo_timestamp_t) (secs * KOGMO_TIMESTAMP_TICKSPERSECOND);
00107 return *this;
00108 }
00109
00110
00111
00112 operator long long int ()
00113 {
00114 return ts;
00115 }
00116
00117 std::string string (void) const
00118 {
00119 kogmo_timestamp_string_t tstring;
00120 kogmo_timestamp_to_string (ts, tstring);
00121 return std::string(tstring);
00122 }
00123
00124 };
00125
00126 inline Timestamp
00127 operator+ (const Timestamp& Begin, const double& secs)
00128 {
00129 return Timestamp ( Begin.timestamp()
00130 + (kogmo_timestamp_t) (secs * KOGMO_TIMESTAMP_TICKSPERSECOND) );
00131 }
00132
00133 inline Timestamp
00134 operator- (const Timestamp& Begin, const double& secs)
00135 {
00136 return Timestamp ( Begin.timestamp()
00137 - (kogmo_timestamp_t) (secs * KOGMO_TIMESTAMP_TICKSPERSECOND) );
00138 }
00139
00140 inline double
00141 operator- (const Timestamp& End, const Timestamp& Begin)
00142 {
00143 return (double) ( End.timestamp() - Begin.timestamp() )
00144 / KOGMO_TIMESTAMP_TICKSPERSECOND;
00145 }
00146
00147
00148 inline bool operator< (const Timestamp& T1, const Timestamp& T2)
00149 { return T1.timestamp() < T2.timestamp(); }
00150 inline bool operator<= (const Timestamp& T1, const Timestamp& T2)
00151 { return T1.timestamp() <= T2.timestamp(); }
00152 inline bool operator> (const Timestamp& T1, const Timestamp& T2)
00153 { return T1.timestamp() > T2.timestamp(); }
00154 inline bool operator>= (const Timestamp& T1, const Timestamp& T2)
00155 { return T1.timestamp() >= T2.timestamp(); }
00156 inline bool operator== (const Timestamp& T1, const Timestamp& T2)
00157 { return T1.timestamp() == T2.timestamp(); }
00158 inline bool operator!= (const Timestamp& T1, const Timestamp& T2)
00159 { return T1.timestamp() != T2.timestamp(); }
00160
00161
00162 inline std::ostream& operator<< (std::ostream& out, const Timestamp& T)
00163 {
00164 kogmo_timestamp_string_t tstring;
00165 kogmo_timestamp_to_string (T.timestamp(), tstring);
00166 return out << tstring;
00167 }
00168
00169 }
00170 #endif
00171