// author: hjjdebug
 // date: 2024年 03月 01日 星期五 15:29:32 CST
 // description: 获取秒值, 把秒值转换为时分秒, 及把时分秒转换成秒值
 // 使用就这么简单.
 // 有时候程序输出需要打印时间信息, 就可以这么干了.
 ///
 #include <stdio.h>
 #include <time.h>
 int main()
 {
     long sec=time(NULL);  //获取秒值
     printf("now time:%ld\n",sec);
     struct tm tm;
     localtime_r(&sec,&tm); //转换到struct tm
     printf("%4d-%02d-%02d %02d:%02d:%02d\n",\
             tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,\
            tm.tm_hour,tm.tm_min,tm.tm_sec);   //打印年月日时分秒
     long conv_time=mktime(&tm);  //由年月日时分秒转换到秒值,实际是经过一系列非线性运算
     printf("conv time:%ld\n",conv_time); //打印秒值,可判断与原始值一致
     return 0;
 }
 执行结果:
 ./localtime
 now time:1709278291
 2024-03-01 15:31:31
 conv time:1709278291
精确到ms或者us也可以,那就要用gettimeofday()函数来获取时间了.