时间格式转换 从CString到SYSTEMTIME,再转换到time_t,以求得时间差

来源:互联网 发布:知其所以然的意思 编辑:程序博客网 时间:2024/04/30 01:05

//这里的 m_GetSampleStartDate,m_GetSampleEndDate为得到的时间为字符串型

int   nYear   = 0;

int   nMonth  = 0;
int   nDay    = 0;
int   nHour   = 0;
int   nSecond = 0;
int   nMinute = 0;
int   nYear1   = 0;
int   nMonth1  = 0;
int   nDay1   = 0;
int   nHour1   = 0;
int   nSecond1 = 0;
int   nMinute1 = 0;
SYSTEMTIME starttime,endtime;


_stscanf(m_GetSampleStartDate, _T("%d-%d-%d %d:%d:%d"), &nYear, &nMonth, &nDay, &nHour, &nMinute, &nSecond);


starttime.wYear   = nYear;
starttime.wMonth  = nMonth;
starttime.wDay    = nDay;
starttime.wHour   = nHour;
starttime.wSecond = nSecond;
starttime.wMinute = nMinute;
_stscanf(m_GetSampleEndDate, _T("%d-%d-%d %d:%d:%d"), &nYear1, &nMonth1, &nDay1, &nHour1, &nMinute1, &nSecond1);
endtime.wYear   = nYear1;
endtime.wMonth  = nMonth1;
endtime.wDay    = nDay1;
endtime.wHour   = nHour1;
endtime.wSecond = nSecond1;
endtime.wMinute = nMinute1;
time_t starttime_t = systime_to_timet(starttime);
time_t endtime_t = systime_to_timet(endtime);
int totals  =difftime(endtime_t,starttime_t);
int totalmin = totals/60;
CString strtotaltime;
strtotaltime.Format(_T("持续时间:%d min"),totalmin);

pStatic6->SetWindowText(strtotaltime);




定义一个转换函数

time_t CMyTime::systime_to_timet(const SYSTEMTIME& st)
{
struct tm gm = {st.wSecond, st.wMinute, st.wHour, st.wDay, st.wMonth-1, st.wYear-1900, st.wDayOfWeek, 0, 0};
return mktime(&gm);
}

0 0
原创粉丝点击