简体   繁体   English

C ++中的int64时间计算

[英]int64 time calculations in C++

I am working on timing related code in 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;

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

For above output is coming as below. 对于上面的输出如下。

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

My question is I want to divide the bounds in to equal interval according to given interval for example each interval of 2 seconds. 我的问题是我想根据给定的间隔将边界划分为相等的间隔,例如每2秒间隔。 How do I go that? 我该怎么办?

ceiling (Range/Interval) intervals. 上限(范围/间隔)间隔。

For example start: 12:00:00:0000 and end is 12:01:52:00099. 例如开始:12:00:00:0000,结束是12:01:52:00099。 Interval is 16 seconds, then we have 7 intervals. 间隔是16秒,那么我们有7个间隔。

unsigned __int64 interval =16;

If I use int above logic if I do (*(unsigned __int64*)&endtime - *(unsigned __int64*)&startTime) / interval) , I am not getting 7 ? 如果我在(*(unsigned __int64*)&endtime - *(unsigned __int64*)&startTime) / interval)使用上述逻辑,则我没有得到7? How do I fix it? 我如何解决它?

FILETIME struct contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC). FILETIME结构包含一个64位值,表示自1601年1月1日(UTC)起100纳秒间隔的数量。

Check this link: FILETIME structure 检查此链接: FILETIME结构

Therefore doing a substraction of two FILETIME variables gives you the time difference in nano seconds (nS). 因此,对两个FILETIME变量进行减法FILETIME得到以纳秒(nS)为单位的时间差。

Dividing this by 16 (seconds) will not give you what you want. 将其除以16(秒)不会得到您想要的。

Either divide by 16 * 10^9 or use difftime API (Link) that performs this math for you and returns the time difference in seconds. 除以16 * 10 ^ 9或使用difftime API (链接)为您执行此数学运算并返回以秒为单位的时差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM