本文介绍了c ++ \将FILETIME转换为秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将FILETIME转换为秒?我需要比较两个FILETIME对象。

How can I convert FILETIME to seconds? I need to compare two FILETIME objects..

我发现,
但似乎它不做的伎俩...

I found this,but seems like it doesn't do the trick...

 ULARGE_INTEGER ull;
    ull.LowPart = lastWriteTimeLow1;
    ull.HighPart = lastWriteTimeHigh1;
    time_t lastModified =  ull.QuadPart / 10000000ULL - 11644473600ULL;

    ULARGE_INTEGER xxx;
    xxx.LowPart = currentTimeLow1;
    xxx.HighPart = currentTimeHigh1;
    time_t current =  xxx.QuadPart / 10000000ULL - 11644473600ULL;

    unsigned long SecondsInterval = current - lastModified;

    if (SecondsInterval > RequiredSecondsFromNow)
        return true;

    return false;

我比较2 FILETIME和预期差异10秒,它给了我〜7000 ...
这是一个很好的方法来提取秒数?

I compared to 2 FILETIME and expected diff of 10 seconds and it gave me ~7000...Is that a good way to extract number of seconds?

推荐答案

你给的代码似乎是正确的,它转换一个FILETIME到UNIX时间戳(显然丢失精度,因为FILETIME的理论分辨率为100纳秒)。您确定您比较的FILETIME确实只有10秒的差异吗?

The code you give seems correct, it converts a FILETIME to a UNIX timestamp (obviously losing precision, as FILETIME has a theoretical resolution of 100 nanoseconds). Are you sure that the FILETIMEs you compare indeed have only 10 seconds of difference?

我在一些软件中使用了非常类似的代码:

I actually use a very similar code in some software:

double time_d()
{
  FILETIME ft;
  GetSystemTimeAsFileTime(&ft);
  __int64* val = (__int64*) &ft;
  return static_cast<double>(*val) / 10000000.0 - 11644473600.0;   // epoch is Jan. 1, 1601: 134774 days to Jan. 1, 1970
}

这会返回一个类似于UNIX的时间戳(以秒为单位,自1970年以来),以秒为单位的解析度。

This returns a UNIX-like timestamp (in seconds since 1970) with sub-second resolution.

这篇关于c ++ \将FILETIME转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 09:49