linux c 日期 转化成 秒

来源:互联网 发布:mysql 修复表 编辑:程序博客网 时间:2024/06/10 03:32
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>


main()
{
        time_t timep;
        char datetime[200] = {0};
        struct tm *p = (struct tm *)malloc(sizeof(struct tm));
        memset(p, 0, sizeof(struct tm));


        printf("1328029261 \n2012-02-01 01:01:01\n");


        time(&timep);
        p=localtime(&timep);


        p->tm_year = 2012-1900;
        p->tm_mon = 1;
        p->tm_mday= 1;
        p->tm_hour= 1;
        p->tm_min= 1;
        p->tm_sec = 1;


        timep = mktime(p);
        strftime(datetime, 200, "%Y-%m-%d %H:%M:%S", p);


        printf("0: mktime():\t%d\n", timep);
        printf("0: strftime():\t%s\n", datetime);


}