00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "UtilTime.hpp"
00030 #include "../libH/Debug.hpp"
00031 #include <sys/time.h>
00032 #include <math.h>
00033
00034 using namespace H;
00035
00037
00039
00041
00043
00047 UtilTime::UtilTime() {
00048 updateElapsedTimer();
00049 }
00050
00054 UtilTime::~UtilTime() {
00055 }
00056
00058
00060
00066 unsigned long UtilTime::getTicks() {
00067 struct timeval tv;
00068 gettimeofday(&tv, NULL);
00069
00070
00071
00072
00073
00074 return tv.tv_sec * 1000000 + tv.tv_usec;
00075 }
00076
00082 unsigned long UtilTime::getElapsedTime() {
00083 return getTicks() - mLastUpdate;
00084 }
00085
00093 int UtilTime::nanoSleep(int nanoSecs) {
00094 struct timespec sleepTime;
00095 struct timespec sleepRet;
00096
00097 sleepTime.tv_sec = 0;
00098 sleepTime.tv_nsec = nanoSecs;
00099 sleepRet.tv_sec = 0;
00100 sleepRet.tv_nsec = 0;
00101
00102 nanosleep(&sleepTime, &sleepRet);
00103
00104 return sleepRet.tv_nsec;
00105 }
00106
00113 int UtilTime::nanoSleepSecs(int nanoSecs) {
00114 struct timespec sleepTime;
00115 struct timespec sleepRet;
00116
00117 sleepTime.tv_sec = nanoSecs;
00118 sleepTime.tv_nsec = 0;
00119 sleepRet.tv_sec = 0;
00120 sleepRet.tv_nsec = 0;
00121
00122 nanosleep(&sleepTime, &sleepRet);
00123
00124 return sleepRet.tv_nsec;
00125 }
00126
00133 void UtilTime::sleep(float Seconds) {
00134 struct timespec sleepTime;
00135 struct timespec sleepRet;
00136
00137 int IntSecs = (int) floor(Seconds);
00138 float Remainder = Seconds - float(IntSecs);
00139
00140 sleepTime.tv_sec = IntSecs;
00141 sleepTime.tv_nsec = (long) (Remainder * 1000000000);
00142
00143 nanosleep(&sleepTime, &sleepRet);
00144 }
00145
00149 void UtilTime::updateElapsedTimer() {
00150 mLastUpdate = getTicks();
00151 }