Skip to content

Commit

Permalink
Merge branch 'NifTK-45-time-stamp-windows'
Browse files Browse the repository at this point in the history
  • Loading branch information
tokjun committed Aug 21, 2014
2 parents 54df50d + c4c5ac3 commit 76e2033
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 114 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if(COMMAND cmake_policy)
# OpenIGTlink version number.
SET(OpenIGTLink_VERSION_MAJOR "1")
SET(OpenIGTLink_VERSION_MINOR "11")
SET(OpenIGTLink_VERSION_PATCH "0")
SET(OpenIGTLink_VERSION_PATCH "1")

option(OpenIGTLink_PROTOCOL_VERSION_2 "Build Library for Protocol Version 2" ON)

Expand Down
65 changes: 64 additions & 1 deletion Source/igtlOSUtil.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#include "igtlOSUtil.h"

#if defined(_WIN32) && !defined(__CYGWIN__)
#include <ctime>
#include <windows.h>
#else
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#endif
Expand All @@ -26,6 +28,7 @@
namespace igtl
{

//-----------------------------------------------------------------------------
void Sleep(int milliseconds)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
Expand All @@ -48,7 +51,7 @@ void Sleep(int milliseconds)
}



//-----------------------------------------------------------------------------
// strnlen(), if not defined
#ifndef OpenIGTLink_HAVE_STRNLEN
size_t Strnlen(const char* s, size_t maxlen)
Expand All @@ -65,5 +68,65 @@ size_t Strnlen(const char* s, size_t maxlen)
}
#endif


//-----------------------------------------------------------------------------
#if defined(_WIN32) && !defined(__CYGWIN__)

igtlUint64 GetHectoNanotime()
{
FILETIME fileTime;
GetSystemTimeAsFileTime(&fileTime);

ULONGLONG hectoNanoSeconds = fileTime.dwHighDateTime;
hectoNanoSeconds <<= 32;
hectoNanoSeconds |= fileTime.dwLowDateTime;

return hectoNanoSeconds;
}
#endif


//-----------------------------------------------------------------------------
igtlUint64 GetTimeUTC()
{
#if defined(_WIN32) && !defined(__CYGWIN__)

// This call returns the time elapsed since the Windows epoch (January 1, 1601) in hectonanoseconds
igtlUint64 curTime = GetHectoNanotime();

// Need to convert this to Unix time (UTC, epoch January 1, 1970), so subtract 11644473600 seconds
// (369 years, 89 of which are leap years = 134774 days)
curTime -= 116444736000000000ui64; // Offset in hectonanosec.

// Convert to nanosecs.
curTime *= 100;

#else

struct timeval tval;

// Gets the system time.
gettimeofday( &tval, 0 );

igtlUint64 nanoSeconds = tval.tv_usec * 1e3;
igtlUint64 seconds = tval.tv_sec *1e9;
igtlUint64 curTime = seconds + nanoSeconds;


#endif // defined(_WIN32) && !defined(__CYGWIN__)

return curTime;
}


//-----------------------------------------------------------------------------
void IGTLCommon_EXPORT GetTimeUTC(igtlInt32 &second, igtlInt32 &nanosecond)
{
igtlUint64 curTime = GetTimeUTC();
igtlUint64 sec = curTime / 1e9;
second = static_cast<igtlInt32>(sec);
nanosecond = static_cast<igtlInt32>(curTime - (sec * 1e9));
}

//-----------------------------------------------------------------------------
} // end namespace
20 changes: 20 additions & 0 deletions Source/igtlOSUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

#include <cstring>

#include "igtlTypes.h"
#include "igtlWin32Header.h"

namespace igtl
{

Expand All @@ -34,6 +36,24 @@ namespace igtl
{ return strnlen(s, maxlen); }
#endif

#if defined(_WIN32) && !defined(__CYGWIN__)

/**
* \brief Gets the time since Windows epoch (January 1, 1601) in hectonanoseconds.
*/
igtlUint64 IGTLCommon_EXPORT GetHectoNanotime();

#endif

/**
* \brief Gets the system time in nanoseconds since the Unix Epoch.
*/
igtlUint64 IGTLCommon_EXPORT GetTimeUTC();

/**
* \brief Gets the system time in seconds and nanoseconds since the Unix Epoch.
*/
void IGTLCommon_EXPORT GetTimeUTC(igtlInt32 &second, igtlInt32 &nanosecond);
}

#endif // __igltOSUtil_h
Expand Down
117 changes: 22 additions & 95 deletions Source/igtlTimeStamp.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,127 +47,46 @@
#include <string.h>

#include "igtl_util.h"
#include "igtlOSUtil.h"

namespace igtl
{

//-----------------------------------------------------------------------------
TimeStamp::TimeStamp(): Object()
{
#if defined(WIN32) || defined(_WIN32)

//LARGE_INTEGER frequency;
//::QueryPerformanceFrequency(&frequency);
//
//this->m_WinFrequency =
// static_cast< FrequencyType >( (__int64)frequency.QuadPart );
//
//SYSTEMTIME st1;
//SYSTEMTIME st2;
//FILETIME ft1;
//FILETIME ft2;
//
//::memset( &st1, 0, sizeof( st1 ) );
//::memset( &st2, 0, sizeof( st2 ) );
//
//st1.wYear = 1601;
//st1.wMonth = 1;
//st1.wDay = 1;
//
//st2.wYear = 1970;
//st2.wMonth = 1;
//st2.wDay = 1;
//
//::SystemTimeToFileTime(&st1, &ft1);
//::SystemTimeToFileTime(&st2, &ft2);
//
//LARGE_INTEGER ui1;
//LARGE_INTEGER ui2;
//
//memcpy( &ui1, &ft1, sizeof( ui1 ) );
//memcpy( &ui2, &ft2, sizeof( ui2 ) );
//
//this->m_WinDifference =
// static_cast< TimeStampType >( ui2.QuadPart - ui1.QuadPart) /
// static_cast< TimeStampType >( 1e7 );
//
//FILETIME currentTime;
//LARGE_INTEGER intTime;
//LARGE_INTEGER tick;
//
//::GetSystemTimeAsFileTime( &currentTime );
//::QueryPerformanceCounter( &tick );
//
//memcpy( &intTime, &currentTime, sizeof( intTime ) );
//
//this->m_WinOrigin =
// static_cast< TimeStampType >( intTime.QuadPart ) /
// static_cast< TimeStampType >( 1e7 );
//
//this->m_WinOrigin -=
// static_cast< TimeStampType >( (__int64)tick.QuadPart ) /
// this->m_WinFrequency;
//
//this->m_WinOrigin += this->m_WinDifference;
//
//this->m_Frequency = static_cast<igtlInt32>( m_WinFrequency );

this->m_WinTimeOrigin = time( NULL );
this->m_WinClockOrigin = clock();
this->m_Frequency = 1000000;

#else

this->m_Frequency = 1000000;

#endif // defined(WIN32) || defined(_WIN32)
// See issue #43. Timestamp should be initialised, but
// the user should grab the time using GetTime() at the point they need it.
this->m_Second = 0;
this->m_Nanosecond = 0;
}


//-----------------------------------------------------------------------------
TimeStamp::~TimeStamp()
{
}


//-----------------------------------------------------------------------------
void TimeStamp::GetTime()
{
#if defined(WIN32) || defined(_WIN32)

//LARGE_INTEGER tick;
//
//::QueryPerformanceCounter( &tick );
//
//TimeStampType value =
// static_cast< TimeStampType >( (__int64)tick.QuadPart ) /
// this->m_WinFrequency;
//
//value += this->m_WinOrigin;
//
//double second = floor(value);

clock_t c1 = clock();
this->m_Second = this->m_WinTimeOrigin + ( c1 - this->m_WinClockOrigin ) / CLOCKS_PER_SEC;
this->m_Nanosecond = (c1 - this->m_WinClockOrigin ) % CLOCKS_PER_SEC * ( 1e9 / CLOCKS_PER_SEC );

#else

struct timeval tval;

::gettimeofday( &tval, 0 );

this->m_Second = tval.tv_sec;
this->m_Nanosecond = tval.tv_usec * 1000; /* convert from micro to nano */

#endif // defined(WIN32) || defined(_WIN32)

igtl::GetTimeUTC(this->m_Second, this->m_Nanosecond);
}


//-----------------------------------------------------------------------------
void TimeStamp::SetTime(double tm)
{
double second = floor(tm);
this->m_Second = static_cast<igtlInt32>(second);
this->m_Nanosecond = static_cast<igtlInt32>((tm - second)*1e9);
}


//-----------------------------------------------------------------------------
void TimeStamp::SetTime(igtlUint32 second, igtlUint32 nanosecond)
{
if (nanosecond < 1e9)
Expand All @@ -178,6 +97,7 @@ void TimeStamp::SetTime(igtlUint32 second, igtlUint32 nanosecond)
}


//-----------------------------------------------------------------------------
void TimeStamp::SetTime(igtlUint64 tm)
{
// Export from 64-bit fixed-point expression used in OpenIGTLink
Expand All @@ -198,6 +118,7 @@ void TimeStamp::SetTimeInNanoseconds(igtlUint64 tm)
}


//-----------------------------------------------------------------------------
double TimeStamp::GetTimeStamp()
{
double tm;
Expand All @@ -207,13 +128,16 @@ double TimeStamp::GetTimeStamp()
return tm;
}


//-----------------------------------------------------------------------------
void TimeStamp::GetTimeStamp(igtlUint32* second, igtlUint32* nanosecond)
{
*second = this->m_Second;
*nanosecond = this->m_Nanosecond;
}


//-----------------------------------------------------------------------------
igtlUint64 TimeStamp::GetTimeStampUint64()
{
// Export as 64-bit fixed-point expression used in OpenIGTLink
Expand All @@ -235,6 +159,8 @@ igtlUint64 TimeStamp::GetTimeStampInNanoseconds() const
return tmp;
}


//-----------------------------------------------------------------------------
void TimeStamp::PrintSelf( std::ostream& os) const
{
Superclass::PrintSelf(os);
Expand All @@ -249,6 +175,7 @@ void TimeStamp::PrintSelf( std::ostream& os) const
<< this->m_Nanosecond << std::endl;
}

}
//-----------------------------------------------------------------------------
} // end namespace


20 changes: 3 additions & 17 deletions Source/igtlTimeStamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class IGTLCommon_EXPORT TimeStamp : public Object
igtlNewMacro(Self);

/// Gets the frequency of a clock.
igtlGetConstMacro(Frequency, igtlUint32);
igtlGetConstMacro(Frequency, igtlInt32);

/// Gets the second part of the time stamp.
igtlGetConstMacro(Second, igtlUint32);
igtlGetConstMacro(Second, igtlInt32);

/// Gets the fraction of second part of the time stamp.
igtlGetConstMacro(Nanosecond, igtlUint32);
igtlGetConstMacro(Nanosecond, igtlInt32);

/// Gets the current time from the system's clock and save it as a time stamp.
void GetTime();
Expand Down Expand Up @@ -98,20 +98,6 @@ class IGTLCommon_EXPORT TimeStamp : public Object
/// Nano-second part of the time stamp
igtlInt32 m_Nanosecond;


#if defined(WIN32) || defined(_WIN32)
//typedef double TimeStampType;
//typedef double FrequencyType;
//
//FrequencyType m_WinFrequency;
//TimeStampType m_WinDifference;
//TimeStampType m_WinOrigin;

time_t m_WinTimeOrigin;
clock_t m_WinClockOrigin;

#endif

};

} // end of namespace igtl
Expand Down

0 comments on commit 76e2033

Please sign in to comment.