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 //---------------------------------------------------------------------- 00022 //---------------------------------------------------------------------- 00023 #include "TimeBase.h" 00024 00025 #include <limits> 00026 00027 #include "os_lxrt.h" 00028 #include "os_time.h" 00029 00030 #ifdef _SYSTEM_LXRT_33_ 00031 # include <rtai_lxrt.h> 00032 # include <rtai_posix.h> 00033 #endif 00034 00035 namespace icl_core { 00036 00037 TimeBase& TimeBase::operator += (const TimeBase& span) 00038 { 00039 secs += span.secs; 00040 nsecs += span.nsecs; 00041 normalizeTime(); 00042 return *this; 00043 } 00044 00045 TimeBase& TimeBase::operator -= (const TimeBase& span) 00046 { 00047 secs -= span.secs; 00048 nsecs -= span.nsecs; 00049 normalizeTime(); 00050 return *this; 00051 } 00052 00053 bool TimeBase::operator != (const TimeBase& other) const 00054 { 00055 return (secs != other.secs) || (nsecs != other.nsecs); 00056 } 00057 00058 bool TimeBase::operator == (const TimeBase& other) const 00059 { 00060 return (secs == other.secs) && (nsecs == other.nsecs); 00061 } 00062 00063 bool TimeBase::operator < (const TimeBase& other) const 00064 { 00065 return (secs == other.secs) ? (nsecs < other.nsecs) : (secs < other.secs); 00066 } 00067 00068 bool TimeBase::operator > (const TimeBase& other) const 00069 { 00070 return (secs == other.secs) ? (nsecs > other.nsecs) : (secs > other.secs); 00071 } 00072 00073 bool TimeBase::operator <= (const TimeBase& other) const 00074 { 00075 return (secs == other.secs) ? (nsecs <= other.nsecs) : (secs < other.secs); 00076 } 00077 00078 bool TimeBase::operator >= (const TimeBase& other) const 00079 { 00080 return (secs == other.secs) ? (nsecs >= other.nsecs) : (secs > other.secs); 00081 } 00082 00083 void TimeBase::normalizeTime() 00084 { 00085 while (((secs >= 0) && (nsecs >= 1000000000)) || 00086 ((secs <= 0) && (nsecs <= -1000000000)) || 00087 ((secs > 0) && (nsecs < 0)) || 00088 ((secs < 0) && (nsecs > 0))) 00089 { 00090 if ((secs >= 0) && (nsecs >= 1000000000)) 00091 { 00092 nsecs -= 1000000000; 00093 ++secs; 00094 } 00095 else if ((secs <= 0) && (nsecs <= -1000000000)) 00096 { 00097 nsecs += 1000000000; 00098 --secs; 00099 } 00100 else if ((secs > 0) && (nsecs < 0)) 00101 { 00102 nsecs += 1000000000; 00103 --secs; 00104 } 00105 else if ((secs < 0) && (nsecs > 0)) 00106 { 00107 nsecs -= 1000000000; 00108 ++secs; 00109 } 00110 } 00111 } 00112 00113 struct timespec TimeBase::timespec() const 00114 { 00115 struct timespec time; 00116 time.tv_sec = time_t(secs); 00117 time.tv_nsec = long(nsecs); 00118 return time; 00119 } 00120 00121 struct timespec TimeBase::systemTimespec() const 00122 { 00123 #ifdef _SYSTEM_LXRT_33_ 00124 if (os::isThisLxrtTask()) 00125 { 00126 struct timespec time = timespec(); 00127 struct timespec global_now; 00128 os::gettimeofday(&global_now); 00129 00130 RTIME rtime_rtai_now = rt_get_time(); 00131 RTIME rtime_global_now = timespec2count(&global_now); 00132 RTIME rtime_time = timespec2count(&time); 00133 00134 count2timespec(rtime_time + rtime_rtai_now - rtime_global_now, &time); 00135 00136 return time; 00137 } 00138 else 00139 { 00140 #endif 00141 return timespec(); 00142 #ifdef _SYSTEM_LXRT_33_ 00143 } 00144 #endif 00145 } 00146 00147 #ifdef _SYSTEM_DARWIN_ 00148 mach_timespec_t TimeBase::machTimespec() const 00149 { 00150 mach_timespec_t time; 00151 time.tv_sec = static_cast<unsigned int>(secs); 00152 time.tv_nsec = static_cast<clock_res_t>(nsecs); 00153 return time; 00154 } 00155 #endif 00156 00158 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 00159 00160 void TimeBase::NormalizeTime() 00161 { 00162 normalizeTime(); 00163 } 00164 00165 int64_t TimeBase::Days() const 00166 { 00167 return days(); 00168 } 00169 00170 int64_t TimeBase::Hours() const 00171 { 00172 return hours(); 00173 } 00174 00175 int64_t TimeBase::Minutes() const 00176 { 00177 return minutes(); 00178 } 00179 00180 int64_t TimeBase::Seconds() const 00181 { 00182 return seconds(); 00183 } 00184 00185 int32_t TimeBase::MilliSeconds() const 00186 { 00187 return milliSeconds(); 00188 } 00189 00190 int32_t TimeBase::MicroSeconds() const 00191 { 00192 return microSeconds(); 00193 } 00194 00195 int32_t TimeBase::NanoSeconds() const 00196 { 00197 return nanoSeconds(); 00198 } 00199 00200 int64_t TimeBase::TbSec() const 00201 { 00202 return tbSec(); 00203 } 00204 00205 int32_t TimeBase::TbNSec() const 00206 { 00207 return tbNSec(); 00208 } 00209 00210 struct timespec TimeBase::Timespec() const 00211 { 00212 return timespec(); 00213 } 00214 00215 struct timespec TimeBase::SystemTimespec() const 00216 { 00217 return systemTimespec(); 00218 } 00219 00220 #ifdef _SYSTEM_DARWIN_ 00221 00224 mach_timespec_t TimeBase::MachTimespec() const 00225 { 00226 return machTimespec(); 00227 } 00228 #endif 00229 00230 #endif 00231 00232 TimeBase TimeBase::maxTime() 00233 { 00234 // TODO: Fix this in a better way! 00235 #undef max 00236 return TimeBase(std::numeric_limits<int64_t>::max(), 999999999); 00237 } 00238 00239 TimeBase::TimeBase(int64_t secs, int32_t nsecs) 00240 : secs(secs), 00241 nsecs(nsecs) 00242 { 00243 normalizeTime(); 00244 } 00245 00246 TimeBase::TimeBase(const struct timespec& time) 00247 : secs(time.tv_sec), 00248 nsecs(time.tv_nsec) 00249 { 00250 normalizeTime(); 00251 } 00252 00253 void TimeBase::fromTimespec(const struct timespec& time) 00254 { 00255 secs = time.tv_sec; 00256 nsecs = time.tv_nsec; 00257 normalizeTime(); 00258 } 00259 00261 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 00262 00263 TimeBase TimeBase::MaxTime() 00264 { 00265 return maxTime(); 00266 } 00267 00268 void TimeBase::FromTimespec(const struct timespec& time) 00269 { 00270 fromTimespec(time); 00271 } 00272 00273 #endif 00274 00275 00276 }