time,gettimeofday,clock_gettime
time()提供了秒级的精确度 1、头文件 <time.h>
2、函数原型
time_t time(time_t * timer)
函数返回从UTC1970-1-1 0:0:0开始到现在的秒数 用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。 #include <time.h>
#include <stdio.h>
int main(void)
{
time_t t;
t = time(NULL);
printf("The number of seconds since January 1, 1970 is %ld\n",t); return 0;
} #include <stdio.h>
#include <stddef.h>
#include <time.h>
int main(void)
{
time_t timer;//time_t就是long int 类型
struct tm *tblock;
timer = time(NULL);//这一句也可以改成time(&timer);
tblock = localtime(&timer);
printf("Local time is: %s\n",asctime(tblock)); return 0;
}
gettimeofday()提供了微秒级的精确度 1、头文件 <time.h>
2、函数原型
int gettimeofday(struct timeval *tv, struct timezone *tz); gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中(可用NULL)。
参数说明:
timeval结构定义为:
struct timeval
{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 结构定义为:
struct timezone
{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/ 返回值: 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
#include<stdio.h>
#include<sys/time.h>
int main(void)
{
struct timeval tv;
struct timezone tz; gettimeofday(&tv, &tz); printf("tv_sec; %d\n",tv.tv_sec);
printf("tv_usec; %d\n",tv.tv_usec); printf("tz_minuteswest; %d\n",tz.tz_minuteswest);
printf("tz_dsttime, %d\n",tz.tz_dsttime); return 0;
}
clock_gettime( ) 提供了纳秒级的精确度 1、头文件 <time.h>
2、编译&链接。在编译链接时需加上 -lrt ;因为在librt中实现了clock_gettime函数
3、函数原型
int clock_gettime(clockid_t clk_id, struct timespect *tp);
参数说明:
clockid_t clk_id 用于指定计时时钟的类型,有以下4种:
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
struct timespect *tp用来存储当前的时间,其结构如下:
struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
返回值。0成功,-1失败 #include<stdio.h>
#include<time.h>
int main()
{
struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts);
printf("CLOCK_REALTIME: %d, %d\n", ts.tv_sec, ts.tv_nsec); clock_gettime(CLOCK_MONOTONIC, &ts);//打印出来的时间跟 cat /proc/uptime 第一
个参数一样
printf("CLOCK_MONOTONIC: %d, %d\n", ts.tv_sec, ts.tv_nsec); clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d\n", ts.tv_sec, ts.tv_nsec); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
printf("CLOCK_THREAD_CPUTIME_ID: %d, %d\n", ts.tv_sec, ts.tv_nsec); printf("%d\n", time(NULL)); return 0;
}
/proc/uptime里面的两个数字分别表示:
the uptime of the system (seconds), and the amount of time spent in idle process (seconds).
把第一个数读出来,那就是从系统启动至今的时间,单位是秒
from : http://blog.csdn.net/sunlylorn/article/details/6313278
time,gettimeofday,clock_gettime的更多相关文章
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
		time()提供了秒级的精确度 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在的秒 ... 
- libevent源码分析(一)
		分析libevent的源代码,我的想法的是先分析各种结构体,struct event_base.struct event,然后是event_base_new函数.event_new函数.event_a ... 
- Libevent(2.1.8)中的事件结构和管理
		Libevent(2.1.8)中的事件结构体 这里的libevent版本为 2.1.8 . libevent中事件的结构体struct event,定义在event_struct.h 中, 这里我们简 ... 
- 【转载】Linux时间相关结构与函数
		1 时间的获取 在程序当中, 我们经常要输出系统当前的时间,比如日志文件中的每一个事件都要记录其产生时间.在 C 语言中获取当前时间的方法有以下几种,它们所获得的时间精度从秒级到纳秒,各有所不同. 表 ... 
- libevent之event_base
		event_base是libevent的事务处理框架,负责事件注册.删除等,属于Reactor模式中的Reactor. event_base结构体 event_base结构体定义于<event_ ... 
- 《Linux/Unix系统编程手册》 时间子系统
		Linux下操作系统编程有两本经典APUE即<Advanced Programming in the UNIX Environment>和TLPI<The Linux Program ... 
- Linux 应用层的时间编程【转】
		转自:https://blog.csdn.net/chinalj2009/article/details/21223681 浅析 Linux 中的时间编程和实现原理,第 1 部分: Linux 应用层 ... 
- Android反调试笔记
		1)代码执行时间检测 通过取系统时间,检测关键代码执行耗时,检测单步调试,类似函数有:time,gettimeofday,clock_gettime. 也可以直接使用汇编指令RDTSC读取,但测试AR ... 
- libevent学习笔记(参考libevent深度剖析)
		最近自学libevent事件驱动库,参考的资料为libevent2.2版本以及张亮提供的<Libevent源码深度剖析>, 参考资料: http://blog.csdn.net/spark ... 
随机推荐
- python3全栈开发-什么是粘包、粘包现象、如何解决粘包
			一.粘包现象 让我们基于tcp先制作一个远程执行命令的程序(1:执行错误命令 2:执行ls 3:执行ifconfig) 注意注意注意: res=subprocess.Popen(cmd.decode( ... 
- [Codeforces 606C]Sorting Railway Cars
			Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ... 
- [NOIp 2009]Hankson的趣味题
			Description Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课 ... 
- SRM 558 SurroundingGame
			题意: 给定一个网格,每个网格有选取代价和占据收益.每个点被占据,需要满足以下两个条件至少一个条件:1.被选取 2.邻近方格都被选取(有公共边被称为邻近) 不一定要占据所有方格,求最大收益. 输入 ... 
- [USACO09OPEN]滑雪课Ski Lessons
			题目描述 Farmer John wants to take Bessie skiing in Colorado. Sadly, Bessie is not really a very good sk ... 
- [JLOI2011]飞行路线
			Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ... 
- 使用word
			同样这也是生活中常用到的办公软件,word本质是一个排版软件,它与一般的编辑器不同的是,它将整个文本分成了一页一页的,当然这也是方便于打印文档. 使用word还是很容易的,一般来说需要注意以下几方面的 ... 
- hadoop hdfs 高可用
			单点故障: 如果某一个节点或服务出了问题,导致服务不可用 单点故障解决方式: 1.给容易出故障的地方安排备份 2.一主一备,要求同一时刻只能有一个对外提供服务 3.当active挂掉之后,standb ... 
- ionic3-ng4学习见闻--(多语言方案)
			1.安装ng2-translate npm install ng2-translate --save 2.app.moudle.ts 引入模块,在下方新增方法 import { TranslateMo ... 
- 作业02-Java基本语法与类库
			1. 本周学习总结 以几个关键词描述本周的学习内容.并将关键词之间的联系描述或绘制出来. 原则:少而精,自己写.即使不超过5行也可,但请一定不要简单的复制粘贴. 2. 书面作业 1. String-使 ... 
