This commit is contained in:
Igor Pavlov
2022-06-23 11:43:16 +01:00
committed by Kornel
parent c3529a41f5
commit ec44a8a070
1248 changed files with 15242 additions and 2443 deletions

220
CPP/Windows/TimeUtils.cpp Normal file → Executable file
View File

@@ -22,7 +22,7 @@ static const UInt64 kUnixTimeOffset =
(UInt64)60 * 60 * 24 * (89 + 365 * (kUnixTimeStartYear - kFileTimeStartYear));
static const UInt64 kNumSecondsInFileTime = (UInt64)(Int64)-1 / kNumTimeQuantumsInSecond;
bool DosTimeToFileTime(UInt32 dosTime, FILETIME &ft) throw()
bool DosTime_To_FileTime(UInt32 dosTime, FILETIME &ft) throw()
{
#if defined(_WIN32) && !defined(UNDER_CE)
return BOOLToBool(::DosDateTimeToFileTime((UInt16)(dosTime >> 16), (UInt16)(dosTime & 0xFFFF), &ft));
@@ -43,7 +43,7 @@ bool DosTimeToFileTime(UInt32 dosTime, FILETIME &ft) throw()
static const UInt32 kHighDosTime = 0xFF9FBF7D;
static const UInt32 kLowDosTime = 0x210000;
bool FileTimeToDosTime(const FILETIME &ft, UInt32 &dosTime) throw()
bool FileTime_To_DosTime(const FILETIME &ft, UInt32 &dosTime) throw()
{
#if defined(_WIN32) && !defined(UNDER_CE)
@@ -121,49 +121,86 @@ bool FileTimeToDosTime(const FILETIME &ft, UInt32 &dosTime) throw()
return true;
}
UInt64 UnixTimeToFileTime64(UInt32 unixTime) throw()
bool UtcFileTime_To_LocalDosTime(const FILETIME &utc, UInt32 &dosTime) throw()
{
FILETIME loc = { 0, 0 };
const UInt64 u1 = FILETIME_To_UInt64(utc);
const UInt64 kDelta = ((UInt64)1 << 41); // it's larger than quantums in 1 sec.
if (u1 >= kDelta)
{
if (!FileTimeToLocalFileTime(&utc, &loc))
loc = utc;
else
{
const UInt64 u2 = FILETIME_To_UInt64(loc);
const UInt64 delta = u1 < u2 ? (u2 - u1) : (u1 - u2);
if (delta > kDelta) // if FileTimeToLocalFileTime() overflow, we use UTC time
loc = utc;
}
}
return FileTime_To_DosTime(loc, dosTime);
}
UInt64 UnixTime_To_FileTime64(UInt32 unixTime) throw()
{
return (kUnixTimeOffset + (UInt64)unixTime) * kNumTimeQuantumsInSecond;
}
void UnixTimeToFileTime(UInt32 unixTime, FILETIME &ft) throw()
void UnixTime_To_FileTime(UInt32 unixTime, FILETIME &ft) throw()
{
UInt64 v = UnixTimeToFileTime64(unixTime);
const UInt64 v = UnixTime_To_FileTime64(unixTime);
ft.dwLowDateTime = (DWORD)v;
ft.dwHighDateTime = (DWORD)(v >> 32);
}
UInt64 UnixTime64ToFileTime64(Int64 unixTime) throw()
UInt64 UnixTime64_To_FileTime64(Int64 unixTime) throw()
{
return (UInt64)((Int64)kUnixTimeOffset + unixTime) * kNumTimeQuantumsInSecond;
}
bool UnixTime64ToFileTime(Int64 unixTime, FILETIME &ft) throw()
bool UnixTime64_To_FileTime64(Int64 unixTime, UInt64 &fileTime) throw()
{
if (unixTime > (Int64)(kNumSecondsInFileTime - kUnixTimeOffset))
{
ft.dwLowDateTime = ft.dwHighDateTime = (UInt32)(Int32)-1;
fileTime = (UInt64)(Int64)-1;
return false;
}
Int64 v = (Int64)kUnixTimeOffset + unixTime;
if (v < 0)
if (unixTime < -(Int64)kUnixTimeOffset)
{
ft.dwLowDateTime = ft.dwHighDateTime = 0;
fileTime = 0;
return false;
}
UInt64 v2 = (UInt64)v * kNumTimeQuantumsInSecond;
ft.dwLowDateTime = (DWORD)v2;
ft.dwHighDateTime = (DWORD)(v2 >> 32);
fileTime = UnixTime64_To_FileTime64(unixTime);
return true;
}
Int64 FileTimeToUnixTime64(const FILETIME &ft) throw()
bool UnixTime64_To_FileTime(Int64 unixTime, FILETIME &ft) throw()
{
UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
UInt64 v;
const bool res = UnixTime64_To_FileTime64(unixTime, v);
ft.dwLowDateTime = (DWORD)v;
ft.dwHighDateTime = (DWORD)(v >> 32);
return res;
}
Int64 FileTime_To_UnixTime64(const FILETIME &ft) throw()
{
const UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
return (Int64)(winTime / kNumTimeQuantumsInSecond) - (Int64)kUnixTimeOffset;
}
bool FileTimeToUnixTime(const FILETIME &ft, UInt32 &unixTime) throw()
Int64 FileTime_To_UnixTime64_and_Quantums(const FILETIME &ft, UInt32 &quantums) throw()
{
const UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
quantums = (UInt32)(winTime % kNumTimeQuantumsInSecond);
return (Int64)(winTime / kNumTimeQuantumsInSecond) - (Int64)kUnixTimeOffset;
}
bool FileTime_To_UnixTime(const FILETIME &ft, UInt32 &unixTime) throw()
{
UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
winTime /= kNumTimeQuantumsInSecond;
@@ -173,9 +210,9 @@ bool FileTimeToUnixTime(const FILETIME &ft, UInt32 &unixTime) throw()
return false;
}
winTime -= kUnixTimeOffset;
if (winTime > 0xFFFFFFFF)
if (winTime > (UInt32)0xFFFFFFFF)
{
unixTime = 0xFFFFFFFF;
unixTime = (UInt32)0xFFFFFFFF;
return false;
}
unixTime = (UInt32)winTime;
@@ -202,12 +239,13 @@ bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day,
return true;
}
void GetCurUtcFileTime(FILETIME &ft) throw()
void GetCurUtc_FiTime(CFiTime &ft) throw()
{
#ifdef _WIN32
// Both variants provide same low resolution on WinXP: about 15 ms.
// But GetSystemTimeAsFileTime is much faster.
#ifdef _WIN32
#ifdef UNDER_CE
SYSTEMTIME st;
GetSystemTime(&st);
@@ -216,8 +254,22 @@ void GetCurUtcFileTime(FILETIME &ft) throw()
GetSystemTimeAsFileTime(&ft);
#endif
#else
#else
FiTime_Clear(ft);
struct timeval now;
if (gettimeofday(&now, 0 ) == 0)
{
ft.tv_sec = now.tv_sec;
ft.tv_nsec = now.tv_usec * 1000;
}
#endif
}
#ifndef _WIN32
void GetCurUtcFileTime(FILETIME &ft) throw()
{
UInt64 v = 0;
struct timeval now;
if (gettimeofday(&now, 0 ) == 0)
@@ -227,8 +279,126 @@ void GetCurUtcFileTime(FILETIME &ft) throw()
}
ft.dwLowDateTime = (DWORD)v;
ft.dwHighDateTime = (DWORD)(v >> 32);
#endif
}
#endif
}}
#ifdef _WIN32
/*
void FiTime_Normalize_With_Prec(CFiTime &ft, unsigned prec)
{
if (prec == k_PropVar_TimePrec_0
|| prec == k_PropVar_TimePrec_HighPrec
|| prec >= k_PropVar_TimePrec_100ns)
return;
UInt64 v = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
int numDigits = (int)prec - (int)k_PropVar_TimePrec_Base;
UInt32 d;
if (prec == k_PropVar_TimePrec_DOS)
{
// we round up as windows DosDateTimeToFileTime()
v += NWindows::NTime::kNumTimeQuantumsInSecond * 2 - 1;
d = NWindows::NTime::kNumTimeQuantumsInSecond * 2;
}
else
{
if (prec == k_PropVar_TimePrec_Unix)
numDigits = 0;
else if (numDigits < 0)
return;
d = 1;
for (unsigned k = numDigits; k < 7; k++)
d *= 10;
}
v /= d;
v *= d;
ft.dwLowDateTime = (DWORD)v;
ft.dwHighDateTime = (DWORD)(v >> 32);
}
*/
#else
/*
void FiTime_Normalize_With_Prec(CFiTime &ft, unsigned prec)
{
if (prec >= k_PropVar_TimePrec_1ns
|| prec == k_PropVar_TimePrec_HighPrec)
return;
int numDigits = (int)prec - (int)k_PropVar_TimePrec_Base;
UInt32 d;
if (prec == k_PropVar_TimePrec_Unix ||
prec == (int)k_PropVar_TimePrec_Base)
{
ft.tv_nsec = 0;
return;
}
if (prec == k_PropVar_TimePrec_DOS)
{
// we round up as windows DosDateTimeToFileTime()
const unsigned sec1 = (ft.tv_sec & 1);
if (ft.tv_nsec == 0 && sec1 == 0)
return;
ft.tv_nsec = 0;
ft.tv_sec += 2 - sec1;
return;
}
{
if (prec == k_PropVar_TimePrec_0
|| numDigits < 0)
numDigits = 7;
d = 1;
for (unsigned k = numDigits; k < 9; k++)
d *= 10;
ft.tv_nsec /= d;
ft.tv_nsec *= d;
}
}
*/
int Compare_FiTime(const CFiTime *a1, const CFiTime *a2)
{
if (a1->tv_sec < a2->tv_sec) return -1;
if (a1->tv_sec > a2->tv_sec) return 1;
if (a1->tv_nsec < a2->tv_nsec) return -1;
if (a1->tv_nsec > a2->tv_nsec) return 1;
return 0;
}
bool FILETIME_To_timespec(const FILETIME &ft, timespec &ts)
{
UInt32 quantums;
const Int64 sec = NWindows::NTime::FileTime_To_UnixTime64_and_Quantums(ft, quantums);
// time_t is long
const time_t sec2 = (time_t)sec;
if (sec2 == sec)
{
ts.tv_sec = sec2;
ts.tv_nsec = (long)(quantums * 100);
return true;
}
return false;
}
void FiTime_To_FILETIME_ns100(const CFiTime &ts, FILETIME &ft, unsigned &ns100)
{
const UInt64 v = NWindows::NTime::UnixTime64_To_FileTime64(ts.tv_sec) + ((UInt64)ts.tv_nsec / 100);
ns100 = (unsigned)((UInt64)ts.tv_nsec % 100);
ft.dwLowDateTime = (DWORD)v;
ft.dwHighDateTime = (DWORD)(v >> 32);
}
void FiTime_To_FILETIME(const CFiTime &ts, FILETIME &ft)
{
const UInt64 v = NWindows::NTime::UnixTime64_To_FileTime64(ts.tv_sec) + ((UInt64)ts.tv_nsec / 100);
ft.dwLowDateTime = (DWORD)v;
ft.dwHighDateTime = (DWORD)(v >> 32);
}
#endif