Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34758
  • 博文数量: 22
  • 博客积分: 301
  • 博客等级: 二等列兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-12 15:30
文章分类

全部博文(22)

文章存档

2019年(4)

2014年(1)

2013年(7)

2011年(8)

2010年(2)

分类: C/C++

2013-12-15 18:48:02


#include "time.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main(int argc, char *argv[])
{
    struct tm t;
    time_t t_of_day;
    char szBuf[15];
    char *ptr ;
    char szYear[5];
    char szMon[3];
    char szDay[3];
    char szHour[3];
    char szMin[3];
    char szSec[3];
   
    if (argc !=2 )
    {
        printf("Usage:%s timestring(eg:20130515181138)\n", argv[0]);
        return (1);   
    }
   
    if (argv == NULL )
    {
        printf("Usage:%s timestring(eg:20130515181138)\n", argv[0]);
        return (1);   
    }
   
    memset(szBuf, 0, 15);
   
    strncpy(szBuf,argv[1],14);
    if ( strlen(szBuf) != 14 )
    {
       printf("Timestring error.\n");
       printf("Usage:%s timestring(eg:20130515181138)\n", argv[0]);
       return  1; 
    }
    // printf("time szBuf: %s\n", szBuf);
   
    ptr=szBuf;
   
    memset(szYear, '\0', 5);
    memset(szMon, '\0', 3);
    memset(szDay, '\0', 3);
    memset(szHour, '\0', 3);
    memset(szMin, '\0', 3);
    memset(szSec, '\0', 3);
    strncpy(szYear,ptr,4);
    strncpy(szMon,ptr+4,2);
    strncpy(szDay,ptr+6,2);
    strncpy(szHour,ptr+8,2);
    strncpy(szMin,ptr+10,2);
    strncpy(szSec,ptr+12,2);
   
    // printf("time %s-%s-%s %s:%s:%s\n", szYear,szMon, szDay,szHour,szMin,szSec);
   
    t.tm_year=atoi(szYear)-1900;
    t.tm_mon=atoi(szMon)-1;
    t.tm_mday=atoi(szDay);
    t.tm_hour=atoi(szHour);
    t.tm_min=atoi(szMin);
    t.tm_sec=atoi(szSec);
    t.tm_isdst=0;
    t_of_day=mktime(&t);
    // printf("time is %d \n", t_of_day);
    // printf(ctime(&t_of_day));
    
    printf("%d\n", t_of_day);
    return (0);
}

阅读(2182) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~