获取间隔时间
2014-10-12 17:44
169 查看
头文件 DeltaTime.h
namespace CoreTools
{
CoreToolsExport int64_t GetTimeInMicroseconds();
CoreToolsExport double GetTimeInSeconds();
}
源文件 DeltaTime.cpp
#include "DeltaTime.h"
#ifdef __APPLE__
#include <sys/time.h>
namespace CoreTools
{
timeval g_Initial;
bool g_InitializedTime = false;
}
#else
#include <sys/timeb.h>
namespace CoreTools
{
long g_InitialSecond = 0;
long g_InitialMicrosecond = 0;
bool g_InitializedTime = false;
}
#endif // __APPLE__
int64_t CoreTools
::GetTimeInMicroseconds ()
{
#ifdef __APPLE__
if (!g_InitializedTime)
{
g_InitializedTime = true;
gettimeofday(&g_Initial, 0);
}
struct timeval currentTime;
gettimeofday(¤tTime, 0);
struct timeval deltaTime;
timersub(¤tTime, &g_Initial, &deltaTime);
return 1000000 * static_cast<int64_t>(deltaTime.tv_sec) + deltaTime.tv_usec;
#else
struct timeb currentTime;
if (!g_InitializedTime)
{
g_InitializedTime = true;
ftime(¤tTime);
g_InitialSecond = static_cast<long>(currentTime.time);
g_InitialMicrosecond = 1000 * currentTime.millitm;
}
ftime(¤tTime);
long currentSecond = static_cast<long>(currentTime.time);
long currentMicrosecond = 1000 * currentTime.millitm;
long deltaSecond = currentSecond - g_InitialSecond;
long deltaMicrosecond = currentMicrosecond - g_InitialMicrosecond;
if (deltaMicrosecond < 0)
{
deltaMicrosecond += 1000000;
--deltaSecond;
}
return 1000000 * static_cast<int64_t>(deltaSecond) + deltaMicrosecond;
#endif // __APPLE__
}
double CoreTools
::GetTimeInSeconds()
{
int64_t microseconds = GetTimeInMicroseconds();
return 1e-06 * microseconds;
}
头文件CustomTime.h
namespace CoreTools
{
class CoreToolsExport CustomTime
{
public:
typedef CustomTime ClassType;
public:
CustomTime();
#ifdef _DEBUG
bool IsValid() const;
#endif // _DEBUG
void ResetCurrentTime();
double GetElapsedTime();
double GetNowTime();
private:
void MarkTimeThisTick();
private:
double m_StartTime;
double m_CurrentTime;
double m_TimeLastTick;
};
}
源文件CustomTime.cpp
#include "CustomTime.h"
#include "DeltaTime.h"
#include "CoreTools/ClassInvariant/CoreToolsClassInvariant.h"
#include "CoreTools/Macro/ClassInvariantMacro.h"
CoreTools::CustomTime::CustomTime()
:m_StartTime(GetTimeInSeconds()),
m_CurrentTime(0.0),m_TimeLastTick(0.0)
{
SELF_CLASS_IS_VALID;
}
#ifdef _DEBUG
bool CoreTools::CustomTime
::IsValid() const
{
if(-0.0001 <= m_StartTime &&
-0.0001 <= m_CurrentTime &&
-0.0001 <= m_TimeLastTick)
return true;
else
return false;
}
#endif // _DEBUG
void CoreTools::CustomTime
::ResetCurrentTime()
{
CLASS_IS_VALID;
m_StartTime = GetTimeInSeconds();
m_CurrentTime = 0.0;
m_TimeLastTick = 0.0;
}
double CoreTools::CustomTime
::GetElapsedTime()
{
CLASS_IS_VALID;
MarkTimeThisTick();
return m_TimeLastTick;
}
double CoreTools::CustomTime
::GetNowTime()
{
CLASS_IS_VALID;
MarkTimeThisTick();
return m_CurrentTime;
}
// private
void CoreTools::CustomTime
::MarkTimeThisTick()
{
double newTime = GetTimeInSeconds() - m_StartTime;
m_TimeLastTick = newTime - m_CurrentTime;
m_CurrentTime = newTime;
if(m_TimeLastTick <= 0.0)
m_TimeLastTick = 0.0;
}
namespace CoreTools
{
CoreToolsExport int64_t GetTimeInMicroseconds();
CoreToolsExport double GetTimeInSeconds();
}
源文件 DeltaTime.cpp
#include "DeltaTime.h"
#ifdef __APPLE__
#include <sys/time.h>
namespace CoreTools
{
timeval g_Initial;
bool g_InitializedTime = false;
}
#else
#include <sys/timeb.h>
namespace CoreTools
{
long g_InitialSecond = 0;
long g_InitialMicrosecond = 0;
bool g_InitializedTime = false;
}
#endif // __APPLE__
int64_t CoreTools
::GetTimeInMicroseconds ()
{
#ifdef __APPLE__
if (!g_InitializedTime)
{
g_InitializedTime = true;
gettimeofday(&g_Initial, 0);
}
struct timeval currentTime;
gettimeofday(¤tTime, 0);
struct timeval deltaTime;
timersub(¤tTime, &g_Initial, &deltaTime);
return 1000000 * static_cast<int64_t>(deltaTime.tv_sec) + deltaTime.tv_usec;
#else
struct timeb currentTime;
if (!g_InitializedTime)
{
g_InitializedTime = true;
ftime(¤tTime);
g_InitialSecond = static_cast<long>(currentTime.time);
g_InitialMicrosecond = 1000 * currentTime.millitm;
}
ftime(¤tTime);
long currentSecond = static_cast<long>(currentTime.time);
long currentMicrosecond = 1000 * currentTime.millitm;
long deltaSecond = currentSecond - g_InitialSecond;
long deltaMicrosecond = currentMicrosecond - g_InitialMicrosecond;
if (deltaMicrosecond < 0)
{
deltaMicrosecond += 1000000;
--deltaSecond;
}
return 1000000 * static_cast<int64_t>(deltaSecond) + deltaMicrosecond;
#endif // __APPLE__
}
double CoreTools
::GetTimeInSeconds()
{
int64_t microseconds = GetTimeInMicroseconds();
return 1e-06 * microseconds;
}
头文件CustomTime.h
namespace CoreTools
{
class CoreToolsExport CustomTime
{
public:
typedef CustomTime ClassType;
public:
CustomTime();
#ifdef _DEBUG
bool IsValid() const;
#endif // _DEBUG
void ResetCurrentTime();
double GetElapsedTime();
double GetNowTime();
private:
void MarkTimeThisTick();
private:
double m_StartTime;
double m_CurrentTime;
double m_TimeLastTick;
};
}
源文件CustomTime.cpp
#include "CustomTime.h"
#include "DeltaTime.h"
#include "CoreTools/ClassInvariant/CoreToolsClassInvariant.h"
#include "CoreTools/Macro/ClassInvariantMacro.h"
CoreTools::CustomTime::CustomTime()
:m_StartTime(GetTimeInSeconds()),
m_CurrentTime(0.0),m_TimeLastTick(0.0)
{
SELF_CLASS_IS_VALID;
}
#ifdef _DEBUG
bool CoreTools::CustomTime
::IsValid() const
{
if(-0.0001 <= m_StartTime &&
-0.0001 <= m_CurrentTime &&
-0.0001 <= m_TimeLastTick)
return true;
else
return false;
}
#endif // _DEBUG
void CoreTools::CustomTime
::ResetCurrentTime()
{
CLASS_IS_VALID;
m_StartTime = GetTimeInSeconds();
m_CurrentTime = 0.0;
m_TimeLastTick = 0.0;
}
double CoreTools::CustomTime
::GetElapsedTime()
{
CLASS_IS_VALID;
MarkTimeThisTick();
return m_TimeLastTick;
}
double CoreTools::CustomTime
::GetNowTime()
{
CLASS_IS_VALID;
MarkTimeThisTick();
return m_CurrentTime;
}
// private
void CoreTools::CustomTime
::MarkTimeThisTick()
{
double newTime = GetTimeInSeconds() - m_StartTime;
m_TimeLastTick = newTime - m_CurrentTime;
m_CurrentTime = newTime;
if(m_TimeLastTick <= 0.0)
m_TimeLastTick = 0.0;
}
相关文章推荐
- 使用DateDiff方法来获取系统时间与指定时间的间隔数
- 获取精确毫秒的时间间隔
- ios NSTimeInterval获取时间间隔
- android 获取时间间隔
- JAVA 获取两个时间的间隔
- ios NSTimeInterval获取时间间隔
- 获取时间间隔
- 【Linux】shell: 获取时间间隔到毫秒、微秒级别
- 获取两个日期间隔时间的shell脚本代码
- java--打渔晒网问题(时间间隔的获取)
- php 时间处理函数--获取某个间隔前的日期
- C# 时间相减的计算方法(获取时间间隔)
- C/C++ 获取时间间隔的方法
- C++ 获取时间间隔
- 获取时间间隔
- C# 时间相减的计算方法(获取时间间隔)
- 获取时间间隔 iphon
- shell中获取两个日期的间隔时间
- Linux shell获取时间和时间间隔(ms级别)
- C# 时间相减的计算方法(获取时间间隔)