C++ 文件时间到\uuuu int64

C++ 文件时间到\uuuu int64,c++,int64,C++,Int64,将FILETIME结构转换为\uu int64的正确方法是什么?你能告诉我吗?我不认为你会这样做:“不要将指向FILETIME结构的指针投射到ULARGE\u INTEGER*或\uu int64*值,因为它会在64位窗口上导致对齐错误。” 如果你真的想要,它会是这样的: __int64 to_int64(FILETIME ft) { return static_cast<__int64>(ft.dwHighDateTime) << 32 | ft.dwLowD

FILETIME
结构转换为
\uu int64
的正确方法是什么?你能告诉我吗?

我不认为你会这样做:“不要将指向
FILETIME
结构的指针投射到
ULARGE\u INTEGER*
\uu int64*
值,因为它会在64位窗口上导致对齐错误。”

如果你真的想要,它会是这样的:

__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);
\uuuu int64到uu int64(文件时间英尺)
{
返回静态_cast(ft.dwHighDateTime)Try


<代码>(μIt64(FielTime.dWigDeTeTime:< P>)当然可以将一个SuxIn 64转换成文件时间,如下面的文件(文件时间*)和Itn64 Val.这将在Visual C++中工作得很好.


不需要使用按位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 ;
}  
或者,如果您想直接转到_uint64

__int64 convert_to_int64( const FILETIME & ac_FileTime )
{
  return static_cast< __int64 > ( convert( ac_FileTime ) ) ;
}
\uuuu int64将\u转换为\uint64(const FILETIME和ac\u FILETIME)
{
返回静态强制转换(转换(ac_文件时间));
}

您可以尝试下面的代码。该代码来自chromium项目

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;
}
模板
内联Dest位\u转换(常量源和源){
目的地;
memcpy(&dest,&source,sizeof(dest));
返回目的地;
}
//文件时间到\uuuu int64
__int64 FileTimeToMicroseconds(常量FILETIME和ft){
返回钻头(英尺)/10;
}
无效微秒时间(u int64 us,文件时间*ft){
*ft=钻头铸件(美制*10);
}
int _tmain(int argc,_TCHAR*argv[]
{
__int64 nTmpUint64=13060762249644841;
时间是唯一的时间;
文件时间nTmpFileTm;
微秒时间(nTmpUint64和nTmpFileTm);
返回0;
}

我也遇到了同样的问题,在谷歌上搜索,然后来到这里。但我在

它说:


使用文件时间执行算术

通常需要对文件时间执行简单的算术运算。例如,您可能需要知道文件的时间是30天。要对文件时间执行算术运算,您需要将文件时间转换为四字(64位整数),执行算术运算,然后将结果转换回文件时间

假设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 );
ULONGLONG qwResult;
//把时间复制成四字。
qwResult=((ULONGLONG)ft.dwHighDateTime)>32);


编辑:我意识到这只是证实了其他一些答案,但我认为值得补充澄清。

memcpy(,&FILETIME,8)如何?它安全吗?我会发现
memcpy
更难理解,但我确信它会工作。编译器可能会优化shift或更好。事实上,它更难理解,但迄今为止一直有效:)感谢这些天(从C++11开始)它可以简单地写成
ULARGEINTEGER{ft.dwLowDateTime,ft.dwHighDateTime}.QuadPart
。虽然可读性不高,但您不需要任何附加函数/宏。“不要将指向文件时结构的指针强制转换为ULARGE_INTEGER*或_int64*值,因为这可能会导致64位窗口上的对齐错误。”@MrTux:在链接中,它说你不能从文件时间结构中强制转换。这两种含义非常不同。基本上,文件时间可能是4字节对齐(甚至可能是2字节对齐)因此,强制转换到一个uuu int64会导致对齐问题,因为一个uu int64必须是8字节对齐的。但是在上面的函数中,我传入一个8字节对齐的uuu int64,windows写入其中。它不会因为指针是一个uu int64而更改指针的地址。这会破坏所有的东西。真的喜欢这个答案,因为它kes根据文档建议的真实含义提供完美的感觉;以及。感谢tor intelligent建议!
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;
}
   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 );