如何将Cstring转换为ColeDateTimeSpan? [英] How to convert Cstring to ColeDateTimeSpan ?
本文介绍了如何将Cstring转换为ColeDateTimeSpan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
COleDateTimeSpan dtOleDuration;
CString m_sDuration;
这里我有两个不同类型的变量现在我想将CString转换为ColeDateTimeSpan
此持续时间介于批次/节点的startdate和enddate之间
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
推荐答案
您需要转换字符串为整数并使用该整数来设置 ColeDateTimeSpan
变量,根据其定义。
You need to convert the string to an integer and use that integer to set the ColeDateTimeSpan
variable, according to its definition.
根据上面的评论你似乎有一个时间跨度字符串格式为hh:mm:ss。然后你可以这样执行转换:
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);
}
这篇关于如何将Cstring转换为ColeDateTimeSpan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文