#include "stdafx.h"
//param const char* pszDate "2012-12-31 11:23:45"
struct tm* date2tm(const char* pszDate)
{
static struct tm tmInput;
int index = 0;
int offset= 0;
char buf[20];
if (pszDate==NULL || (strlen(pszDate)!=19))
{
//printf("param error at %p \r\n",pszDate);
return NULL;
}
//printf("pszDate:%s \r\n",pszDate);
tmInput.tm_isdst=0;
//year
index = 0;
offset = 4;
memcpy(buf,pszDate+index,offset);
buf[offset] = 0;
tmInput.tm_year = atoi(buf) - 1900;
//month
index = 5;
offset = 2;
memcpy(buf,pszDate+index,offset);
buf[offset] = 0;
tmInput.tm_mon = atoi(buf)-1;
//day
index = 8;
offset = 2;
memcpy(buf,pszDate+index,offset);
buf[offset] = 0;
tmInput.tm_mday = atoi(buf);
//hour
index = 11;
offset = 2;
memcpy(buf,pszDate+index,offset);
buf[offset] = 0;
tmInput.tm_hour = atoi(buf);
//minute
index = 14;
offset = 2;
memcpy(buf,pszDate+index,offset);
buf[offset] = 0;
tmInput.tm_min = atoi(buf);
//second
index = 17;
offset = 2;
memcpy(buf,pszDate+index,offset);
buf[offset] = 0;
tmInput.tm_sec = atoi(buf);
return &tmInput;
}
int _tmain(int argc, _TCHAR* argv[])
{
char szDate[20] = "2012-12-31 11:23:45";
char szBuf[20];
struct tm* ptm_date = date2tm(szDate);
strftime(szBuf,20,"%Y-%m-%d %H:%M:%S",ptm_date);
int nret = strcmp(szDate,szBuf);
//localtime => mktime
//gmtime ==> _mkgmtime,timegm
#ifdef WIN32
time_t date = _mkgmtime(ptm_date);
#else
time_t date = timegm(ptm_date);
#endif
return 0;
}