我写的一个给time_t赋值的小函数
我寫的一個給time_t賦值的小函數
//========================================
// 功能:將固定格式的時間轉化為 time_t 日歷時間
//??????? eg. 1970-01-01 00:00:00? = 0
//========================================
time_t GetTime_t(char* Buf)
{
??? int iYear=0,iMonth=0,iDay=0,
??????? iHour=0,iMinute=0,iSecond=0;
??? //獲得年,月,日
??? char *pTmp,szBuf[21]="";
??? _tcscpy(szBuf,Buf);
??? pTmp=_tcsstr(szBuf,"-");
??? char szYear[5]="",szMonth[3]="",szDay[3]="";
??? char* p=szBuf,*p1=szYear;
??? while (p != pTmp)
??? {??? *p1=*p;p++;p1++;
??? }
??? *pTmp=',';
??? pTmp=_tcsstr(szBuf,"-");
??? p1=szMonth;p++;
??? while (p != pTmp){
??????? *p1=*p;p++;p1++;
??? }
??? *pTmp=',';
??? pTmp=_tcsstr(szBuf," ");
??? p1=szDay;p++;
??? while (p != pTmp){
??????? *p1=*p;p++;p1++;
??? }
??? //獲得時間
??? char szHour[3]="",szMinute[3]="",szSecond[3]="";
??? *pTmp=',';
??? pTmp=_tcsstr(szBuf,":");
??? p1=szHour;p++;
??? while (p != pTmp){
??????? *p1=*p;p++;p1++;
??? }
???
??????? *pTmp=',';
??? pTmp=_tcsstr(szBuf,":");
??? p1=szMinute;p++;
??? while (p != pTmp){
??????? *p1=*p;p++;p1++;
??? }
??????? *pTmp=',';
??? pTmp=_tcsstr(szBuf,":");
??? p1=szSecond;p++;
??? while (p != pTmp && *p != '/0'){
??????? *p1=*p;p++;p1++;
??? }
??? iYear=atoi(szYear);iMonth=atoi(szMonth),iDay=atoi(szDay);
??? iHour=atoi(szHour),iMinute=atoi(szMinute),iSecond=atoi(szSecond);
??? struct tm t;
??? time_t t_of_day;
??? t.tm_year=iYear-1900;
??? t.tm_mon=iMonth-1;
??? t.tm_mday=iDay;
??? t.tm_hour=iHour;
??? t.tm_min=iMinute;
??? t.tm_sec=iSecond;
??? t.tm_isdst=0;
??? t_of_day=mktime(&t);
????
??? return t_of_day;
}
調用格式:
?time_t t;
??//t=GetTime_t("2010-01-28 16:41:31");
??t=GetTime_t("1970-01-01 01:00:00");
??cout<<"time: "<<t<<endl;
總結
以上是生活随笔為你收集整理的我写的一个给time_t赋值的小函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++ 日期 时间 time_t与s
- 下一篇: 使用VC编写VB使用DLL