Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting CString to CTime

Status
Not open for further replies.

Larree

Programmer
Dec 31, 2002
20
SG
Dear all,

I have declared a CTime object as CTime Timing. I believe this is timing in seconds. I later convert it to a CString object using Timing.format("%H:%M:%S") This gives the timing in terms of Hours : Minutes : Seconds

I then store it in a daatbase. I wish to extract this CString object later and convert it to CTime for mathematical operations.

What is the conversion code? Please advice.....Thanks
 
Well If your using ADO, and you should, you receive the database value for a field as a VARIANT (_variant_t) type. Then you construct your COleDateTime (not CTime) object using the _variant_t and the DATE member of the VARIANT

-pete
 
Hi Pete,

Actually I have already used ODBC for most part of the program so it is not possible for me to switch to ADO now. I actually receive a CTime object value. Then switch to CString to store in my database since I make use of the AddString() function to add to an array I constructed.Then I display the results. However I need to calcultae the difference between a start time and an end time so I need to switch back to CTime to use CTimeSpan for calculation.So is there any way to switch from CString to CTime? I use Timing.format("%H:%M:%S") to switch from CTime to CString in the first place.Please advice....thanks
 
Larree,

Wow, did i mess this thread up! I re-read your initial post just now… I don’t know what I was doing.

First make sure CTime meets your requirements. Given that it’s upper limit is the year 2038 it may not. If possible I suggest migrating to COleDateTime.

If for some reason you cannot use COleDateTime you will have to parse the string your self into integer values representing the Hours, minutes and second values in the string and use them to construct your CTime object.

If you can use the COleDateTime object then it will parse the string for you like this:

Code:
CSstring buf = "14:23:44";
COleDateTime dt;
dt.ParseDateTime(buf, VAR_TIMEVALUEONLY );
cout << (LPCTSTR)dt.Format(&quot;%H:%M:%S&quot;) << endl;;

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top