Re: Problem with 64-bit time
As requested, below is a sample program.
I understand the problem better now. The sample program will demonstrate
that the problem does not exist in the _timeb structure but in the way the
sprintf family of functions work. Without the _USE_32BIT_TIME_T defined, the
time attribute is of type __time64_t. When sprintf reads the first %d
placeholder, it only reads the lower 32 bits of the __time64_t. The next %u
placeholder reads the upper 32 bits of the __time64_t which at this time is
zero. The real problem (for me) is that the code I'm working with really
needs to read a 64-bit integer so that sprintf can calculate the correct
offset into the variable argument buffer. Quickly I found a reference to the
%I64x format specifier however it isn't exactly what I want since I want an
integer not a hex version of the integer. Is there a better candidate format
specifier for 64-bit integers?
Thanks,
Mike
//Referenced Declarations
#include "stdafx.h"
// ...from c++ standard library
#include <iostream>
using std::cout;
using std::endl;
// ...from c-runtime library
#include "sys\timeb.h"
//Note, to see equivalent behavior between the two results set the
preprocessor
//macro _USE_32BIT_TIME_T in the project properties.
int _tmain(int argc, _TCHAR* argv[])
{
_timeb oTime;
_ftime(&oTime);
cout << "Using istream to read time values" << endl;
cout << "oTime.time: " << oTime.time << " (sec)\n"
<< "oTime.millitm: " << oTime.millitm << " (milli-sec)\n"
<< "oTime.timezone: " << oTime.timezone << " (minutes)\n"
<< "oTime.dstflag: " << oTime.dstflag << endl;
cout << endl;
cout << "Using stdio to read time values" << endl;
char sBuf[256] = { 0 };
_snprintf_s(sBuf, sizeof(sBuf) - 1, _TRUNCATE,
"oTime.time: %I64x (sec)\n"
"oTime.millitm: %u (milli-sec)\n"
"oTime.timezone: %d (minutes)\n"
"oTime.dstflag: %d\n",
oTime.time, oTime.millitm, oTime.timezone, oTime.dstflag);
cout << sBuf << endl;
return 0;
}