天下

记录修行的印记

date2tm

 

#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;
}

 

posted on 2012-08-22 17:39 天下 阅读(271) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论