apue2nd书中 没有对于linux平台下时间函数的例子,仅仅是介绍了一下api,不太方便我们使用,这里找了一些例子,
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
int main()
{
time_t timep;
time (&timep);
printf("%d/n",timep);
}
该例子直接打印出来 绝对时间,也就是 从 1970.1.1到现在的秒数
<sys/time.h> 和 <sys/timeb.h>中有一些更加精细的 时间结构体
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
int main()
{
struct timeb tp;
struct tm * tm;
ftime (&tp);
tm = localtime (&( tp.time ));
printf("%02d:%02d:%02d:%03d/n", (tm->tm_hour), (tm->tm_min), (tm->tm_sec),(tp.millitm ));
}
本文提供了两个简单的C语言程序示例,演示如何在Linux环境下使用时间函数。第一个示例展示了如何获取从1970年1月1日至今的秒数;第二个示例则通过使用<sys/time.h>和<sys/timeb.h>头文件中的函数来获取更精确的时间信息。
4288

被折叠的 条评论
为什么被折叠?



