Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <vlCore/Time.hpp>
00033 #include <ctime>
00034 #include <cmath>
00035
00036 #if defined(VL_PLATFORM_LINUX) || defined(VL_PLATFORM_MACOSX)
00037 #include <sys/time.h>
00038 #include <unistd.h>
00039 #endif
00040
00041 using namespace vl;
00042
00043
00044
00045
00046 Time::Time()
00047 {
00048 VL_DEBUG_SET_OBJECT_NAME()
00049
00050 for(int i=0; i<VL_MAX_TIMERS; ++i)
00051 mStart[i] = -1;
00052
00053 #if defined(VL_PLATFORM_WINDOWS)
00054 SYSTEMTIME local_time;
00055 GetLocalTime(&local_time);
00056 mYear = local_time.wYear;
00057 mMonth = local_time.wMonth;
00058 mDayOfWeek = local_time.wDayOfWeek;
00059 mDayOfMonth = local_time.wDay;
00060 mHour = local_time.wHour;
00061 mMinute = local_time.wMinute;
00062 mSecond = local_time.wSecond;
00063 mMicrosecond = local_time.wMilliseconds * 1000;
00064 #elif defined(__GNUG__)
00065 time_t secs;
00066 ::time(&secs);
00067 tm* date = localtime( &secs );
00068
00069 mYear = date->tm_year + 1900;
00070 mMonth = date->tm_mon;
00071 mDayOfWeek = date->tm_wday;
00072 mDayOfMonth = date->tm_mday;
00073 mHour = date->tm_hour;
00074 mMinute = date->tm_min;
00075 mSecond = date->tm_sec;
00076
00077 struct timeval tv;
00078 gettimeofday( &tv, NULL );
00079 mMicrosecond = tv.tv_usec;
00080 #endif
00081 }
00082
00083 namespace vl
00084 {
00085 unsigned long long gStartTime = 0;
00086
00087 void initStartTime()
00088 {
00089 #if defined(VL_PLATFORM_WINDOWS)
00090 LARGE_INTEGER Frequency;
00091 LARGE_INTEGER PerformanceCount;
00092 BOOL has_timer = QueryPerformanceFrequency( &Frequency );
00093 if (has_timer)
00094 {
00095
00096 QueryPerformanceCounter( &PerformanceCount );
00097
00098 gStartTime = PerformanceCount.QuadPart;
00099 }
00100 else
00101 {
00102 gStartTime = GetTickCount();
00103 }
00104 #elif defined(__GNUG__)
00105 struct timeval tv;
00106 gettimeofday( &tv, NULL );
00107 gStartTime = (unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec;
00108 #endif
00109 }
00110 }
00111
00114 real Time::currentTime()
00115 {
00116 if (gStartTime == 0)
00117 initStartTime();
00118
00119 VL_CHECK(gStartTime);
00120
00121 #if defined(VL_PLATFORM_WINDOWS)
00122
00123 LARGE_INTEGER Frequency;
00124 LARGE_INTEGER PerformanceCount;
00125 BOOL has_timer = QueryPerformanceFrequency( &Frequency );
00126 if (has_timer)
00127 {
00128
00129 QueryPerformanceCounter( &PerformanceCount );
00130
00131 return (real)(PerformanceCount.QuadPart-gStartTime)/Frequency.QuadPart;
00132 }
00133 else
00134 {
00135 return (GetTickCount()-gStartTime) / 1000.0f;
00136 }
00137 #elif defined(__GNUG__)
00138 struct timeval tv;
00139 gettimeofday( &tv, NULL );
00140 return ((unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec - gStartTime) * 0.000001f;
00141 #endif
00142 }
00143
00144 void Time::sleep(unsigned int milliseconds)
00145 {
00146 #if defined(VL_PLATFORM_WINDOWS)
00147 Sleep(milliseconds);
00148 #elif defined(__GNUG__)
00149 usleep(milliseconds*1000);
00150 #endif
00151 }
00152