简体   繁体   中英

int64 time calculations in C++

I am working on timing related code in 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. How do I go that?

ceiling (Range/Interval) intervals.

For example start: 12:00:00:0000 and end is 12:01:52:00099. Interval is 16 seconds, then we have 7 intervals.

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 ? How do I fix it?

FILETIME struct contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

Check this link: FILETIME structure

Therefore doing a substraction of two FILETIME variables gives you the time difference in nano seconds (nS).

Dividing this by 16 (seconds) will not give you what you want.

Either divide by 16 * 10^9 or use difftime API (Link) that performs this math for you and returns the time difference in seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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