00001 // PTF - Psychological Test Framework 00002 // http://ptf.sourceforge.net 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 #include <ptf/core/timeinterval.hpp> 00019 #include <sstream> 00020 00021 // bring stuff into scope 00022 using std::stringstream; 00023 00027 TimeInterval::TimeInterval(const timespec a, const timespec b) { 00028 interval[0] = a; 00029 interval[1] = b; 00030 } 00031 00035 TimeInterval::TimeInterval() { 00036 interval[0].tv_sec = 0; interval[0].tv_nsec = 0; 00037 interval[1].tv_sec = 0; interval[1].tv_nsec = 0; 00038 } 00039 00045 void TimeInterval::setInterval(const timespec a, const timespec b) { 00046 interval[0] = a; 00047 interval[1] = b; 00048 } 00049 00055 void TimeInterval::getInterval(timespec& a, timespec& b) const { 00056 a = interval[0]; 00057 b = interval[1]; 00058 } 00059 00063 void TimeInterval::measureA() { 00064 Clock::getSingleton().getClock(interval[0]); 00065 } 00066 00070 void TimeInterval::measureB() { 00071 Clock::getSingleton().getClock(interval[1]); 00072 } 00073 00078 timespec TimeInterval::getDifference() const { 00079 timespec delta; 00080 long s = interval[1].tv_sec - interval[0].tv_sec; 00081 long ns = interval[1].tv_nsec - interval[0].tv_nsec; 00082 if (ns < 0) { 00083 s -= 1; 00084 ns = 1000000000 + ns; 00085 } 00086 delta.tv_sec = s; 00087 delta.tv_nsec = ns; 00088 return delta; 00089 } 00090 00096 int TimeInterval::compareDifference(const timespec& a) const { 00097 timespec delta = getDifference(); 00098 if (a.tv_sec < delta.tv_sec) return -1; 00099 if (a.tv_sec > delta.tv_sec) return 1; 00100 // a.tv_sec == delta.tv_sec at this point 00101 if (a.tv_nsec < delta.tv_nsec) return -1; 00102 if (a.tv_nsec > delta.tv_nsec) return 1; 00103 // a.tv_nsec == delta.tv_nsec at this point 00104 return 0; 00105 } 00106 00111 int TimeInterval::compareTimespecs(const timespec& a, const timespec& b) { 00112 if (a.tv_sec < b.tv_sec) return -1; 00113 if (a.tv_sec > b.tv_sec) return 1; 00114 // a.tv_sec == delta.tv_sec at this point 00115 if (a.tv_nsec < b.tv_nsec) return -1; 00116 if (a.tv_nsec > b.tv_nsec) return 1; 00117 // a.tv_nsec == delta.tv_nsec at this point 00118 return 0; 00119 } 00120 00125 timespec TimeInterval::addTimespecs(const timespec& a, const timespec& b) { 00126 timespec result; 00127 result.tv_sec = (a.tv_sec + b.tv_sec) + (a.tv_nsec + b.tv_nsec) / 1000000000; 00128 result.tv_nsec = (a.tv_nsec + b.tv_nsec) % 1000000000; 00129 return result; 00130 } 00131 00136 timespec TimeInterval::subTimespecs(const timespec& a, const timespec& b) { 00137 timespec result; 00138 result.tv_sec = a.tv_sec - b.tv_sec; 00139 if (a.tv_nsec < b.tv_nsec) { 00140 result.tv_sec = result.tv_sec - 1; 00141 result.tv_nsec = 1000000000 + a.tv_nsec - b.tv_nsec; 00142 } 00143 else { 00144 result.tv_nsec = a.tv_nsec - b.tv_nsec; 00145 } 00146 return result; 00147 } 00148 00154 string TimeInterval::timespecToString(const timespec& a) { 00155 stringstream temp; 00156 temp << a.tv_sec << " " << a.tv_nsec; 00157 return temp.str(); 00158 } 00159 00164 string TimeInterval::toString() const { 00165 string temp = "[" + timespecToString(interval[0]) + " | " 00166 + timespecToString(interval[1]) + "]"; 00167 return temp; 00168 }