00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 00003 // -- BEGIN LICENSE BLOCK ---------------------------------------------- 00004 // This file is part of FZIs ic_workspace. 00005 // 00006 // This program is free software licensed under the LGPL 00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3). 00008 // You can find a copy of this license in LICENSE folder in the top 00009 // directory of the source code. 00010 // 00011 // © Copyright 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany 00012 // 00013 // -- END LICENSE BLOCK ------------------------------------------------ 00014 00015 //---------------------------------------------------------------------- 00023 //---------------------------------------------------------------------- 00024 #include "icl_core/TimeSpan.h" 00025 00026 #ifdef _IC_BUILDER_OPENSPLICEDDS_ 00027 # include "icl_core/iTimeSpan.h" 00028 #endif 00029 00030 namespace icl_core { 00031 00032 const TimeSpan TimeSpan::cZERO(0, 0); 00033 00034 TimeSpan::TimeSpan(int64_t sec, int32_t nsec) 00035 : TimeBase(sec, nsec) 00036 { } 00037 00038 TimeSpan::TimeSpan(const struct timespec& time_span) 00039 : TimeBase(time_span) 00040 { } 00041 00042 #ifdef _IC_BUILDER_OPENSPLICEDDS_ 00043 TimeSpan::TimeSpan(const iTimeSpan& time_span) 00044 : TimeBase(time_span.sec, time_span.nsec) 00045 { } 00046 00047 TimeSpan& TimeSpan::operator = (const iTimeSpan& time_span) 00048 { 00049 secs = time_span.sec; 00050 nsecs = time_span.nsec; 00051 return *this; 00052 } 00053 00054 TimeSpan::operator iTimeSpan () 00055 { 00056 iTimeSpan result; 00057 result.sec = secs; 00058 result.nsec = nsecs; 00059 return result; 00060 } 00061 #endif 00062 00063 TimeSpan& TimeSpan::fromSec(int64_t sec) 00064 { 00065 secs = sec; 00066 nsecs = 0; 00067 normalizeTime(); 00068 return *this; 00069 } 00070 00071 TimeSpan& TimeSpan::fromMSec(int64_t msec) 00072 { 00073 secs = msec / 1000; 00074 nsecs = int32_t((msec % 1000) * 1000000); 00075 normalizeTime(); 00076 return *this; 00077 } 00078 00079 TimeSpan& TimeSpan::fromUSec(int64_t usec) 00080 { 00081 secs = usec / 1000000; 00082 nsecs = int32_t((usec % 1000000) * 1000); 00083 normalizeTime(); 00084 return *this; 00085 } 00086 00087 TimeSpan TimeSpan::createFromSec(int64_t sec) 00088 { 00089 return TimeSpan().fromSec(sec); 00090 } 00091 00092 TimeSpan TimeSpan::createFromMSec(int64_t msec) 00093 { 00094 return TimeSpan().fromMSec(msec); 00095 } 00096 00097 TimeSpan TimeSpan::createFromUSec(int64_t usec) 00098 { 00099 return TimeSpan().fromUSec(usec); 00100 } 00101 00102 TimeSpan& TimeSpan::operator += (const TimeSpan& span) 00103 { 00104 secs += span.secs; 00105 nsecs += span.nsecs; 00106 normalizeTime(); 00107 return *this; 00108 } 00109 00110 TimeSpan& TimeSpan::operator -= (const TimeSpan& span) 00111 { 00112 secs -= span.secs; 00113 nsecs -= span.nsecs; 00114 normalizeTime(); 00115 return *this; 00116 } 00117 00118 bool TimeSpan::operator != (const TimeSpan& other) const 00119 { 00120 return TimeBase::operator != (other); 00121 } 00122 00123 bool TimeSpan::operator == (const TimeSpan& other) const 00124 { 00125 return TimeBase::operator == (other); 00126 } 00127 00128 bool TimeSpan::operator < (const TimeSpan& other) const 00129 { 00130 return TimeBase::operator < (other); 00131 } 00132 00133 bool TimeSpan::operator > (const TimeSpan& other) const 00134 { 00135 return TimeBase::operator > (other); 00136 } 00137 00138 bool TimeSpan::operator <= (const TimeSpan& other) const 00139 { 00140 return TimeBase::operator <= (other); 00141 } 00142 00143 bool TimeSpan::operator >= (const TimeSpan& other) const 00144 { 00145 return TimeBase::operator >= (other); 00146 } 00147 00148 int64_t TimeSpan::tsSec() const 00149 { 00150 return secs; 00151 } 00152 00153 int32_t TimeSpan::tsNSec() const 00154 { 00155 return nsecs; 00156 } 00157 00158 int32_t TimeSpan::tsUSec() const 00159 { 00160 return nsecs/1000; 00161 } 00162 00163 int64_t TimeSpan::toMSec() const 00164 { 00165 return secs * 1000 + nsecs / 1000000; 00166 } 00167 00168 int64_t TimeSpan::toUSec() const 00169 { 00170 return secs * 1000000 + nsecs / 1000; 00171 } 00172 00173 int64_t TimeSpan::toNSec() const 00174 { 00175 return 1000000000 * secs + nsecs; 00176 } 00177 00179 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 00180 00181 TimeSpan& TimeSpan::FromSec(int64_t sec) 00182 { 00183 return fromSec(sec); 00184 } 00185 00186 TimeSpan& TimeSpan::FromMSec(int64_t msec) 00187 { 00188 return fromMSec(msec); 00189 } 00190 00191 TimeSpan& TimeSpan::FromUSec(int64_t usec) 00192 { 00193 return fromUSec(usec); 00194 } 00195 00196 int64_t TimeSpan::TsSec() const 00197 { 00198 return tsSec(); 00199 } 00200 00201 int32_t TimeSpan::TsNSec() const 00202 { 00203 return tsNSec(); 00204 } 00205 00206 int32_t TimeSpan::TsUSec() const 00207 { 00208 return tsUSec(); 00209 } 00210 00211 int64_t TimeSpan::ToMSec() const 00212 { 00213 return toMSec(); 00214 } 00215 00216 int64_t TimeSpan::ToUSec() const 00217 { 00218 return toUSec(); 00219 } 00220 00221 int64_t TimeSpan::ToNSec() const 00222 { 00223 return toNSec(); 00224 } 00225 00226 #endif 00227 00228 00229 std::ostream& operator << (std::ostream& stream, const TimeSpan& time_span) 00230 { 00231 int64_t calc_secs = time_span.tsSec(); 00232 int64_t calc_nsec = time_span.tsNSec(); 00233 if (calc_secs < 0) 00234 { 00235 stream << "-"; 00236 calc_secs = -calc_secs; 00237 } 00238 if (calc_secs > 3600) 00239 { 00240 stream << calc_secs / 3600 << "h"; 00241 calc_secs = calc_secs % 3600; 00242 } 00243 if (calc_secs > 60) 00244 { 00245 stream << calc_secs / 60 << "m"; 00246 calc_secs=calc_secs % 60; 00247 } 00248 if (calc_secs > 0) 00249 { 00250 stream << calc_secs << "s"; 00251 } 00252 00253 if (calc_nsec / 1000000 * 1000000 == calc_nsec) 00254 { 00255 stream << calc_nsec / 1000000 << "ms"; 00256 } 00257 else if (calc_nsec / 1000 * 1000 == calc_nsec) 00258 { 00259 stream << calc_nsec << "us"; 00260 } 00261 else 00262 { 00263 stream << calc_nsec << "ns"; 00264 } 00265 00266 return stream; 00267 } 00268 00269 }