簡體   English   中英

文件時間到 __int64

[英]FILETIME to __int64

FILETIME結構轉換為__int64的正確方法是什么? 你能告訴我嗎?

我不認為您應該:“不要將指向FILETIME結構的指針ULARGE_INTEGER*轉換為ULARGE_INTEGER*__int64*值,因為它會導致 64 位 Windows 上的對齊錯誤。”

來源。

如果你真的想要它會是這樣的:

__int64 to_int64(FILETIME ft)
{
    return static_cast<__int64>(ft.dwHighDateTime) << 32 | ft.dwLowDateTime;
}

FILETIME ft = // ...
__int64 t = to_int64(ft);

但類似:

FILETIME ft = // ...
__int64 t = *reinterpet_cast<__int64*>(&ft);

不好。

無需使用按位 OR 恢復到神秘的構造。 Windows API 擁有執行此操作所需的一切。

unsigned __int64    convert( const FILETIME & ac_FileTime )
{
  ULARGE_INTEGER    lv_Large ;

  lv_Large.LowPart  = ac_FileTime.dwLowDateTime   ;
  lv_Large.HighPart = ac_FileTime.dwHighDateTime  ;

  return lv_Large.QuadPart ;
}  

或者,如果您想直接轉到 __int64。

__int64 convert_to_int64( const FILETIME & ac_FileTime )
{
  return static_cast< __int64 > ( convert( ac_FileTime ) ) ;
}

嘗試

(__int64(filetime.dwHighDateTime)<<32) | __int64(filetime.dwLowDateTime)

當然,您可以按如下*(FILETIME*)&int64Val 將 __int64 轉換為文件時間。 這將在 Visual C++ 下正常工作。

IE

__int64 createTime = 0;
__int64 accessTime = 0;
__int64 writeTime = 0;
GetFileTime( hFile, *(FILETIME*)&createTime, *(FILETIME*)&accessTime, *(FILETIME*)&writeTime );

你可以試試下面的代碼。 代碼來自鉻項目

template <class Dest, class Source>
inline Dest bit_cast(const Source& source) {
    Dest dest;
    memcpy(&dest, &source, sizeof(dest));
    return dest;
}

//FILETIME to __int64

__int64 FileTimeToMicroseconds(const FILETIME& ft) {
    return bit_cast<__int64, FILETIME>(ft) / 10;
}

void MicrosecondsToFileTime(__int64 us, FILETIME* ft) {
    *ft = bit_cast<FILETIME, __int64>(us * 10);
}

int _tmain(int argc, _TCHAR* argv[])
{
    __int64 nTmpUint64 = 13060762249644841;

    time_t unixtime;
    FILETIME nTmpFileTm;
    MicrosecondsToFileTime(nTmpUint64,&nTmpFileTm);

    return 0;
}

我有完全相同的問題,用谷歌搜索,然后來到這里。 但我也發現了一個有用的 Microsoft 支持頁面
https://support.microsoft.com/en-gb/help/188768/info-working-with-the-filetime-structure

它說:


使用文件時間執行算術

通常需要對文件時間執行簡單的算術運算。 例如,您可能需要知道某個文件何時存在 30 天。 要對文件時間執行算術運算,您需要將 FILETIME 轉換為四字(64 位整數),執行算術運算,然后將結果轉換回 FILETIME。

假設 ft 是包含文件創建時間的 FILETIME 結構,以下示例代碼將時間添加 30 天:

   ULONGLONG qwResult;

   // Copy the time into a quadword.
   qwResult = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;

   // Add 30 days.
   qwResult += 30 * _DAY;

   // Copy the result back into the FILETIME structure.
   ft.dwLowDateTime  = (DWORD) (qwResult & 0xFFFFFFFF );
   ft.dwHighDateTime = (DWORD) (qwResult >> 32 );

編輯:我意識到這只是確認了其他一些答案,但我認為值得添加澄清。

具有最佳性能的兩種方法(一種匯編指令)。 僅在 Visual C++ 2022 x64 中測試。

unsigned long long ull = *reinterpret_cast<_UNALIGNED unsigned long long*>(&ft)

缺點: _UNALIGNED宏(和__unaligned修飾符)是非標准的。 但我猜 Clang 也支持它。

memcpy(&ull, &ft, sizeof(ull))

缺點:只有開啟“Enable Intrinsic Functions”C++編譯器選項,這段代碼才會得到很好的優化。

其他答案有一些開銷。

(static_cast<unsigned long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime

生成按位運算。

ULARGE_INTEGER{ft.dwLowDateTime, ft.dwHighDateTime}.QuadPart

生成兩個 32 位操作,而不是一個 64 位操作。 盡管如此,還不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM