#include <stdio.h>#include <time.h>//gcc time_test.c -o time_testint main(){time_t now;struct tm* tm_now;//返回自1970年1月1日以来持续时间的秒数time(&now);//转为本地时间tm_now = localtime(&now);printf("The number of seconds since January 1, 1970 is %d\n", now);printf("now datetime: %4d-%02d-%02d %02d:%02d:%02d\n",tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);//返回自1970年1月1日以来持续时间的秒数time_t time_now = mktime(tm_now);printf("The number of seconds since January 1, 1970 is %d\n", time_now);return 0;}
编译:gcc time_test.c -o time_test
测试:./time_test