C/C++ - SYSTEMTIME 값 기준으로 특정 시간이 지났는지를 판단하는 함수
C#으로 구현하면 이런 식인데,
static void Main(string[] args)
{
DateTime old = DateTime.Now;
Thread.Sleep(1000);
if ((DateTime.Now - old).TotalMilliseconds > 1000)
{
Console.Write("Hello");
}
Console.WriteLine("World!");
}
Windows 환경의 C/C++에서 SYSTEMTIME으로 저 작업을 하려니 코드가 좀 복잡해지는데, ^^ 제 경우에는 stackoverflow의 어느 코드인가를 참고해서 다음과 같이 만들어봤습니다.
bool IsElapsedTime(SYSTEMTIME* oldTime, SYSTEMTIME* currentTime, ULONGLONG milliSeconds)
{
union TimeUnit {
ULARGE_INTEGER li;
FILETIME ft;
};
TimeUnit oldUnit, newUnit;
SystemTimeToFileTime(oldTime, &oldUnit.ft);
SystemTimeToFileTime(currentTime, &newUnit.ft);
// Add in the seconds
oldUnit.li.QuadPart += (milliSeconds * 10000); // 1 milli == 1000000 nano
// 1 milli == 10000 * 100 nano
return oldUnit.li.QuadPart <= newUnit.li.QuadPart;
}
SYSTEMTIME을 FileTime으로 바꾸면,
SYSTEMTIME structure
; https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
FILETIME structure
; https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
QuadPart에 1이 있는 경우 그 값이 100 나노초가 되므로 밀리 초를 나타내기 위해 10,000을 곱하는 식으로 해결하면 됩니다.
어쨌든 사용법은, 요런 식으로 할 수 있습니다.
if (DateTimeExtension::IsElapsedTime(&oldTime, ¤tTime, 500) == true)
{
// 500ms 지난 후 해야 할 작업 수행
}
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]