分类: 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);
}