From 61ee7ce92d4de51a54d3e1135245b43fe7ba9c99 Mon Sep 17 00:00:00 2001 From: Amar Takhar Date: Thu, 3 Jun 2010 01:09:41 +0000 Subject: [PATCH] Fill in time_log() with code from http://www.suacommunity.com/dictionary/gettimeofday-entry.php <-thanks! Originally committed to SVN as r4409. --- aegisub/libaegisub/windows/util.cpp | 45 ++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/aegisub/libaegisub/windows/util.cpp b/aegisub/libaegisub/windows/util.cpp index 1da2329e0..1e422d040 100644 --- a/aegisub/libaegisub/windows/util.cpp +++ b/aegisub/libaegisub/windows/util.cpp @@ -24,9 +24,14 @@ #include #include + +#include +#include + #endif -#include +//#include +#include "libaegisub/types.h" #include "libaegisub/util.h" #include "libaegisub/util_win.h" @@ -70,6 +75,44 @@ std::string ErrorString(DWORD error) { return str; } +/// @brief Get seconds and microseconds. +/// @param tv[out] timeval struct +/// This code is from http://www.suacommunity.com/dictionary/gettimeofday-entry.php +void time_log(timeval &tv) { +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + + // Define a structure to receive the current Windows filetime + FILETIME ft; + + // Initialize the present time to 0 and the timezone to UTC + unsigned __int64 tmpres = 0; + static int tzflag = 0; + + GetSystemTimeAsFileTime(&ft); + + // The GetSystemTimeAsFileTime returns the number of 100 nanosecond + // intervals since Jan 1, 1601 in a structure. Copy the high bits to + // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits. + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + // Convert to microseconds by dividing by 10 + tmpres /= 10; + + // The Unix epoch starts on Jan 1 1970. Need to subtract the difference + // in seconds from Jan 1 1601. + tmpres -= DELTA_EPOCH_IN_MICROSECS; + + // Finally change microseconds to seconds and place in the seconds value. + // The modulus picks up the microseconds. + tv.tv_sec = (long)(tmpres / 1000000UL); + tv.tv_usec = (long)(tmpres % 1000000UL); +} } // namespace io } // namespace agi