Reputation: 106530
I'm writing a simple wrapper around the Win32 FILETIME structure. boost::datetime
has most of what I want, except I need whatever date type I end up using to interpolate with Windows APIs without issues.
To that end, I've decided to write my own things for doing this -- most of the operations aren't all that complicated. I'm implementing the TimeSpan - like type at this point, but I'm unsure how I'd implement FileTimeToSystemTime. I could just use the system's built-in FileTimeToSystemTime function, except FileTimeToSystemTime cannot handle negative dates -- I need to be able to represent something like "-12 seconds".
How should something like this be implemented?
Billy3
Upvotes: 5
Views: 1848
Reputation: 4442
I see bad design here. Time span, difference between two times, is the same when measuring with system time and with file time too. W32 FileTimeToSystemTime is right about not accepting negative values because it has no sense. Period of 2 seconds is a period of 2 seconds, no matter which time zone you used.
//EDIT: Second problem. SYSTEMTIME is somehow able to represent time span, but it would be erroneous. I.e. month is not usable unit when measuring time spans.
Upvotes: 0
Reputation: 7939
Windows SYSTEMTIME and FILETIME data types are intended to represent a particular date and time. They are not really suitable to represent time differences. Time differences are better of as a simple integer representing the number of between two SYSTEMTIMEs or FILETIMEs. might be seconds, or something smaller if you need more precision.
If you need to display a difference to users, simple division and modulus can be used to compute the components.
std::string PrintTimeDiff(int nSecDiff)
{
std::ostringstream os;
if (nSecDiff<0)
{
os << "-";
nSecDiff= -nSecDiff;
}
int nSeconds = nSecDiff % (24*60*60);
nSecDiff /= 60;
int nMinutes = nSecDiff % (24*60)
nSecDiff /= 60;
int nHours = nSecDiff % 24;
int nDays = nSecDiff / 24;
os << nDays << " Days " << nHours << ":" << nMinutes << ":" << nSeconds;
return os .str();
}
Upvotes: 2
Reputation: 86718
Assuming you didn't have a problem with the structure all having unsigned components, you could take any negative timespans, make them positive, call FileTimeToSystemTime, and then (if the original input was negative) pick out components to make negative.
Upvotes: 1