tags:

views:

55

answers:

3

Hello :)

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

+1  A: 

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.

Gabe
A: 

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.

adf88
Why is month not a usable unit? 2010-06-01 - 2010-02-01 = 4 months.
Billy ONeal
2010-01-30 + 1 month = ?
adf88
@adf88: 2010-01-30 + 1 month = March 1st
Billy ONeal
(2010-01-30 + 1 month) - 1 month = ?
adf88
(2010-01-30 + 1 month) - 1 month = 2010-01-30. If someone asks you to add a month, they mean 30 days. I don't think that's unreasonable. But in any case, I'm using an unsigned __int64, not SYSTEMTIME for internal representation of the difference.
Billy ONeal
+1  A: 

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();
}
Michael J