libH/UtilTime.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
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 // Defines / Type Defs
00039 
00041 // Construction
00043 
00047 UtilTime::UtilTime() {
00048         updateElapsedTimer();
00049 }
00050 
00054 UtilTime::~UtilTime() {
00055 }
00056 
00058 // Class Body
00060 
00066 unsigned long UtilTime::getTicks() {
00067         struct timeval tv;
00068         gettimeofday(&tv, NULL);
00069 
00070         /*
00071         * This will wrap around approximately every 4000 seconds, i.e.
00072         * just over an hour, which is more than enough.
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 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3