PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Number of seconds between two times

Hi,

I'm trying to find a way to get the number of seconds, minutes, and hours between two specific times.

Examples:
How many seconds from now until midnight
How many seconds from the time and date you were born until now.
Or time difference between any two arbitrary date/times.

Anyway, I can get the current time, I can set a SYSTEMTIME to any date/time I want, but I can't seem to find a way to subtract one from the other and end up with seconds.

Anybody have any ideas? I figure I must be missing something easy.

Thanks,

Pam
I'm not sure, as I only know pretty basic C++ (some console stuff, OOP), but I think the way you'd do it would be to assign the times to variables and subtract them.

Code:

/*do whatever you need to do to get the time...
i think one way is to multiply the hours by 60 and
add those to the minutes, and then do the same with
minutes and add it to seconds. There's probably an
easier way, but I don't know of any*/

x = time1;
y = time2;
z = time1-time2

/*Then you can perform whatever conversions you
want, like changing it back into minutes and hours,
or whatever*/
UNIX has a system when all times are espressed in seconds since some special date in 1970, and I think you can get this is Windows as well.
Possibly by calling time() or another ANSI function?

Obviously in this case this system is easier to work with than a function which returns hours and seconds and stuff.

...though it does run out in 2037 or something (when all UNIX machines will die!)

update - just checked eVC, and there's no time function I must be thinking of Gnu GCC.
Oh well - I guess they stripped out all the UNIX stuff in Windows CE - makes sense.
Michael, the problem is how you get to time1 and time2 if they are spread apart by years. If it's time within one day, it's pretty easy to add the hours minutes seconds together. But over years or decades it's much more difficult to figure out.

Pam
It comes down to computing which year is a leap year (contains 366 days vs. 365). You already know how many days are in each month, and how many seconds are in a day. Do the math sistah!
Yeah, ok.

Visual Basic has a nice Date data type that you can subtract one from the other and get seconds. Can't imagine why MS didn't include the subtraction operator when they defined SYSTEMTIME or FILETIME.

Upon further reading I find this in MS help:

It is not recommended that you add and subtract values from the SYSTEMTIME structure to obtain relative times. Instead, you should

Convert the SYSTEMTIME structure to a FILETIME structure.
Copy the resulting FILETIME structure to a ULARGE_INTEGER structure.
Use normal 64-bit arithmetic on the ULARGE_INTEGER value.


hmmm normal 64-bit arithmetic. Ok, i'll try that.

Pam
Well, in the MSDN for EVC++, I found:

Code:
// example for CTime::operator +, -
CTime t1( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999
CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999
CTimeSpan ts = t2 - t1; // Subtract 2 CTimes
ASSERT( ts.GetTotalSeconds() == 86400L );
ASSERT( ( t1 + ts ) == t2 ); // Add a CTimeSpan to a CTime.
ASSERT( ( t2 - ts ) == t1 ); // Subtract a CTimeSpan from a CTime.


and

Code:
// example for CTimeSpan::GetTotalHours, CTimeSpan::GetTotalMinutes
// and CTimeSpan::GetTotalSeconds
CTimeSpan ts( 3, 1, 5, 12 ); // 3 days, 1 hour, 5 min, and 12 sec
ASSERT( ts.GetTotalHours() == 73 );
ASSERT( ts.GetTotalMinutes() == 4385 );
ASSERT( ts.GetTotalSeconds() == 263112 );


That should do you.

Cheers,

Ref.
Hi Pam,

Refreactor, as usual, is correct. However, I have had problems with CTime when I have tried in the past :(

I wrote a utility which calculates the number of days to an event called DaysToGo sometime ago, download the trial version from http://www.purplepecker.com to see if it is the sort of calculation you are after. If it is then the code is below


Code:

SYSTEMTIME systemtime;
FILETIME filetime;
int nDTG;

// change heading font
LOGFONT lf;
HBRUSH oldBrush;
HPEN oldPen;
HFONT hfont,oldhfont;
ULARGE_INTEGER ulDTG;
COLORREF crTextColor;

// work out how many days to go
GetLocalTime(&systemtime;);
SYSTEMTIME stEventDate;
FileTimeToSystemTime(&eEvents;.EventDate,&stEventDate;);
SystemTimeToFileTime(&systemtime;,&filetime;);
ULARGE_INTEGER a,b;
a.LowPart=eEvents.EventDate.dwLowDateTime;
a.HighPart=eEvents.EventDate.dwHighDateTime;
b.LowPart=filetime.dwLowDateTime;
b.HighPart=filetime.dwHighDateTime;

if(a.QuadPart>b.QuadPart)
{
ulDTG.QuadPart=a.QuadPart-b.QuadPart;
ulDTG.QuadPart/=10000000;
ulDTG.QuadPart=(ulDTG.QuadPart/(24*60*60))+1;
nDTG=(int)ulDTG.QuadPart;
}
if(a.QuadPart<b.QuadPart)
{
ulDTG.QuadPart=b.QuadPart-a.QuadPart;
ulDTG.QuadPart/=10000000;
ulDTG.QuadPart=(ulDTG.QuadPart/(24*60*60));
nDTG=(int)ulDTG.QuadPart;
nDTG=nDTG*-1;
eEvents.Priority=4;
}

// see if date is today
if(nDTG==0)
eEvents.Priority=0;
int nLenDate=0;
TCHAR strDate[256];
DWORD dwSize = 256;
FileTimeToSystemTime(&eEvents;.EventDate,&systemtime;);
nLenDate=GetDateFormat(0, 0, &systemtime;, NULL,strDate, dwSize);


I am not sure if this is the best way, it certainly is not easy but it works :)

I hope this helps
Bad
You should use COleDateTime instead of CTime. If you subtract two ColeDateTimes from one another, you get a COleDateTimeSpan, which exposes a GetTotalSeconds member to determine the total number of seconds spanning the two dates.

Dan East
While we're discussing CTime / COleDateTime, I'll throw out one little caveat that pertains to both. I have heard of many cases of individuals incorrectly using the GetCurrentTime member of those classes. If you'll notice, GetCurrentTime is a static function, which means that it cannot modify any data member of the class (because static members can be called even if the class has never be instantiated).

Code:

//Incorrect Usage:
COleDateTime date;
date.GetCurrentTime();
//At this point, it is often incorrectly assumed that
//date has been set to the current Date & Time.

//Correct usage:
COleDateTime date;
date=date.GetCurrentTime();

//Or:
COleDateTime date(COleDateTime::GetCurrentTime());


Dan East
Thanks Dan,

That would explain why I had problems with COleDateTime and had to resort to my (very long) method.

Speak soon
Bad