From 2959598108418ebebe84c76c274d58eda0eab5e8 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 30 Jul 2011 11:14:02 +0300 Subject: [PATCH] Add conversions from timeval/FILETIME to RawTime --- source/time/rawtime.cpp | 38 ++++++++++++++++++++++++++++++++++- source/time/rawtime_private.h | 10 ++++++--- source/time/utils.cpp | 21 ++----------------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/source/time/rawtime.cpp b/source/time/rawtime.cpp index 5c21a68..a299246 100644 --- a/source/time/rawtime.cpp +++ b/source/time/rawtime.cpp @@ -1,10 +1,41 @@ #include "rawtime.h" #include "rawtime_private.h" +namespace { + +#ifdef WIN32 +/// Returns the unixtime epoch as filetime +Msp::Time::RawTime get_epoch() +{ + SYSTEMTIME st; + st.wYear = 1970; + st.wMonth = 1; + st.wDay = 1; + st.wHour = 0; + st.wMinute = 0; + st.wSecond = 0; + st.wMilliseconds = 0; + + FILETIME ft; + SystemTimeToFileTime(&st, &ft); + return (ft.dwLowDateTime+(static_cast(ft.dwHighDateTime)<<32))/10; +} +#endif + +} + + namespace Msp { namespace Time { -#ifndef WIN32 +#ifdef WIN32 +RawTime filetime_to_rawtime(const FILETIME &ft) +{ + static RawTime epoch = get_epoch(); + + return (ft.dwLowDateTime+(static_cast(ft.dwHighDateTime)<<32))/10-epoch; +} +#else timeval rawtime_to_timeval(RawTime raw) { timeval tv; @@ -20,6 +51,11 @@ timespec rawtime_to_timespec(RawTime raw) ts.tv_nsec = (raw%1000000)*1000; return ts; } + +RawTime timeval_to_rawtime(const timeval &tv) +{ + return tv.tv_sec*1000000LL+tv.tv_usec; +} #endif } // namespace Time diff --git a/source/time/rawtime_private.h b/source/time/rawtime_private.h index 566d5a6..846a712 100644 --- a/source/time/rawtime_private.h +++ b/source/time/rawtime_private.h @@ -1,7 +1,9 @@ #ifndef MSP_TIME_RAWTIME_PRIVATE_H_ #define MSP_TIME_RAWTIME_PRIVATE_H_ -#ifndef WIN32 +#ifdef WIN32 +#include +#else #include #endif #include "rawtime.h" @@ -9,10 +11,12 @@ namespace Msp { namespace Time { -#ifndef WIN32 -// Internal conversion utilities, not intended for public use +#ifdef WIN32 +RawTime filetime_to_rawtime(const FILETIME &); +#else timeval rawtime_to_timeval(RawTime); timespec rawtime_to_timespec(RawTime); +RawTime timeval_to_rawtime(const timeval &); #endif } // namespace Time diff --git a/source/time/utils.cpp b/source/time/utils.cpp index b59a8af..8b57695 100644 --- a/source/time/utils.cpp +++ b/source/time/utils.cpp @@ -23,28 +23,11 @@ TimeStamp now() #ifndef WIN32 timeval tv; gettimeofday(&tv, 0); - return TimeStamp(tv.tv_sec*1000000LL+tv.tv_usec); + return TimeStamp(timeval_to_rawtime(tv)); #else - static RawTime epoch = 0; - if(!epoch) - { - SYSTEMTIME st; - st.wYear = 1970; - st.wMonth = 1; - st.wDay = 1; - st.wHour = 0; - st.wMinute = 0; - st.wSecond = 0; - st.wMilliseconds = 0; - - FILETIME ft; - SystemTimeToFileTime(&st, &ft); - epoch = (ft.dwLowDateTime+(static_cast(ft.dwHighDateTime)<<32))/10; - } - FILETIME ft; GetSystemTimeAsFileTime(&ft); - return TimeStamp((ft.dwLowDateTime+(static_cast(ft.dwHighDateTime)<<32))/10-epoch); + return TimeStamp(filetime_to_rawtime(ft)); #endif } -- 2.45.2