0

我正在研究 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;

如果我使用 int 上述逻辑(*(unsigned __int64*)&endtime - *(unsigned __int64*)&startTime) / interval),我不会得到 7 吗?我如何解决它?

4

1 回答 1

0

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

检查此链接:FILETIME 结构

因此,对两个变量进行减法运算FILETIME可以得到以纳秒 (nS) 为单位的时间差。

将其除以 16(秒)不会给您想要的结果。

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

于 2014-07-29T06:59:37.953 回答