Click here to Skip to main content
15,507,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
COleDateTimeSpan dtOleDuration;
CString m_sDuration;

here iam having two variables which is of different types now i want convert CString to ColeDateTimeSpan

this duration is between startdate and enddate of a batch / node
Posted
Comments
Jochen Arndt 25-Feb-15 10:39am    
This depends on the format of the string (you should add an example to your question). The solution is simple if it is already a time span in the format [h]hh:mm:ss.

But if the time span consists of two date time strings it can be difficult. Then you have to split the string into the two parts and convert them to COleDateTime which can then be used to calculate the time span. This conversion depends on the used format and may fail if different formats are used.
Vamsi Krishna 26-Feb-15 1:43am    
COleDateTimeSpan dtOleDuration;
CString m_sDuration;

m_sDuration= 36:22;
dtOleDuration=..............???????

How to convert this to Duration..........

write now iam calculating it by getting the difference between start date and end date like this

COleDateTime m_dtStopDate,m_dtStDate;


m_dtStDate.ParseDateTime(m_sStDate);
m_dtStopDate.ParseDateTime(m_sEndDate);

dtOleDuration=m_dtStopDate-m_dtStDate;


You need to convert the string to an integer and use that integer to set the ColeDateTimeSpan variable, according to its definition.
 
Share this answer
 
According to the above comments you seem to have a time span string in the format hh:mm:ss. Then you can perform the conversion like this:
// Convert time span string to COleDateTimeSpan
// Format must be [[hh:]mm:]ss[.msec]
COleDateTimeSpan SpanFromString(const CString& strSpan) const
{
	long lDays = 0;
	long lHours = 0;
	int nMins = 0;
	int nSecs = 0;
	LPCTSTR s1 = strSpan.GetString();
	LPCTSTR s2 = _tcschr(s1, _T(':'));
	LPCTSTR s3 = s2 ? _tcschr(s2 + 1, _T(':')) : NULL;
	// hh:mm:ss
	if (s3)
	{
		nSecs = _tstoi(s3 + 1);
		nMins = _tstoi(s2 + 1);
		lHours = _tstol(s1);
	}
	// mm:ss
	else if (s2)
	{
		nSecs = _tstoi(s2 + 1);
		nMins = _tstoi(s1);
	}
	// ss or empty string
	else
		nSecs = _tstoi(s1);
	if (lHours >= 24)
	{
		lDays = lHours / 24;
		lHours %= 24;
	}
	return COleDateTimeSpan(lDays, (int)lHours, nMins, nSecs);
}
 
Share this answer
 
Comments
Vamsi Krishna 26-Feb-15 9:59am    
i don't need to calculate all these stuff already i'm having the value in CString type...
just need to convert it to COleDateTimeSpan type..!!!!!
Jochen Arndt 26-Feb-15 10:18am    
That is done by my code. The above function converts a CString containing something like "36:22" (interpreting it as minutes and seconds here) to COleDateTimeSpan. If that is not what you want you should state more precisely what you need.

From your comment I assumed that calling the function this way is what you want:
m_sDuration = "36:22";
dtOleDuration = SpanFromString(m_sDuration);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900