Getting Time Differences
With a program I'm working on now I get the time in one of three different ways:
Fri, 31 Dec 1999 23:59:59 GMT
Friday, 31-Dec-99 23:59:59 GMT
Fri Dec 31 23:59:59 1999
I need to calculate the difference between the time given and the current time. I know there are functions to convert a time_t structure to a string, is there any way to work backwards? I figure if I can get it down to a tm struct or a time_t, I'll be able to use the difftime(?) function to get the difference.
So what I'm asking is, is there a function I can call that converts a string like the above into one of these structures, or will I have to write my own?
Use COleDateTime MFC class.
You can get the current time as follows:
COleDateTime today = COleDateTime::GetCurrentTime();
strTime.Format("%d %d %d", today.GetMonth(), today.GetDay(), today.GetYear());
Something like that.
or you can also use CTime, but I think you're much better off with COleDateTime (see MSDN).
Thanks Mullah, but I can already get the current time (although I bookmarked the MSDN reference for later use, thanks for that).
My problem lies in converting a string into a time structure, and COleDateTime doesn't seem to be able to do that :( Any suggestions?
for converting strings into some other things you have atoi (convert a string to an integer), atol (convert to a long), atof (convert to a double) C functions. See what you can do with these functions. You can pass CString objects to such functions and get the value from the CString and use it.
Yeah I know I can write it all myself, I'm about to write a function that uses istringstream and other standard stuff to convert the numbers and store them in a struct tm, but I had hoped there might be a function like this:
struct tm GetTime(string, string);
And I could say
struct tm MyTm;
MyTm = GetTime("%h, %m, %s", oldTime);
Where the first string is the format of the string, and the second is the string to be converted. I hoped someone might have done this before, but oh well, looks like I'll have to do it myself.