在C ++中进行int64时间计算

时间:2012-06-18 09:59:01

标签: c++

我正在使用C ++编写与时序相关的代码。

FILETIME ftTime;
GetSystemTimeAsFileTime(&ftTime);

struct DateTime
{
    unsigned int dwLowDateTime;
    unsigned int dwHighDateTime;
};



DateTime myTime;
myTime.dwHighDateTime = (unsigned int)ftTime.dwHighDateTime;
myTime.dwLowDateTime  = (unsigned int)ftTime.dwLowDateTime;

ussigned __int64 AsUInt64_t

unsigned __int64 myInt64Time = *(unsigned __int64*)&myTime;

// I have Getstring format from date time some thing like this below

Format() {

SYSTEMTIME  systemTime;
FileTimeToSystemTime(&fileTime, &systemTime);

const char*         formatString = "%04d-%02d-%02d %02d:%02d:%02d.%03d";

// I have few variable declartions to be used for snprintf which is not mentioned here.

apiResult = _snprintf_s(pBuffer, sizeOfBuffer, count, formatString, systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute,                         systemTime.wSecond, systemTime.wMilliseconds);

}

DateTime startTime = now(); // some how I am getting latest time here.
sleep(5s); // sleeping for 5 seconds.
DateTime endTime = now(); // i.e., after 5 seconds.

std::cout << "\nOn Start Time:" << startTime.Format() << std::endl;
std::cout << "On End Tme:    " << endTime.Format() << std::endl;

std::cout << "On Start Read " << *(unsigned __int64*)&startTime << std::endl;
std::cout << "On End Read " << *(unsigned __int64*)&endtime<< std::endl;

///////////////////

以上输出如下:

On Start Time:2012-06-18 09:45:03.180
On End Time  :2012-06-18 09:45:08.183

// Int Int64 I am getting as below

On Start Read : 129844863031802858
On End Read   : 129844863081832935

我的问题是我想根据给定的间隔将边界划分为相等的间隔,例如每个间隔为2秒。我该怎么做?

天花板(范围/间隔)间隔。

例如,开始时间:12:00:00:0000,结束时间为12:01:52:00099。间隔是16秒,然后我们有7个间隔。

unsigned __int64 interval =16;

如果我(*(unsigned __int64*)&endtime - *(unsigned __int64*)&startTime) / interval),如果我在逻辑上方使用int,那么我没有得到7?我该如何解决?

1 个答案:

答案 0 :(得分:0)

FILETIME struct包含一个64位值,表示自1601年1月1日(UTC)以来100纳秒间隔的数量。

点击此链接:FILETIME structure

因此,对两个FILETIME变量进行减法可以得出纳秒(nS)的时差。

将此除以16(秒)将无法满足您的需求。

除以16 * 10 ^ 9或使用为您执行此数学运算的difftime API (Link),并以秒为单位返回时差。