struct timespec 和 struct timeval
time()提供了秒级的精确度 .
1、头文件 <time.h>
2、函数原型
time_t time(time_t * timer)
函数返回从TC1970-1-1 0:0:0开始到现在的秒数
用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。
如果需要更高的时间精确度,就需要struct timespec 和 struct timeval来处理:
一、struct timespec 定义:
typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};
#endif
struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间
struct tm *localtime(const time_t *clock); //线程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
二、struct timeval 定义:
struct timeval {
time_t tv_sec; // seconds
long tv_usec; // microseconds
};
struct timezone{
int tz_minuteswest; //miniutes west of Greenwich
int tz_dsttime; //type of DST correction
};
struct timeval有两个成员,一个是秒,一个是微秒, 所以最高精确度是微秒。
一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间
三、 对照样例:
#include<stdio.h>
#include<time.h>
#include<sys/time.h> void nowtime_ns()
{
printf("---------------------------struct timespec---------------------------------------\n");
printf("[time(NULL)] : %ld\n", time(NULL));
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n", ts.tv_sec, ts.tv_nsec); struct tm t;
char date_time[];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));
printf("clock_gettime : date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
}
void nowtime_us()
{
printf("---------------------------struct timeval----------------------------------------\n");
printf("[time(NULL)] : %ld\n", time(NULL));
struct timeval us;
gettimeofday(&us,NULL);
printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec); struct tm t;
char date_time[];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));
printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
} int main(int argc, char* argv[])
{
nowtime_ns();
printf("\n");
nowtime_us();
printf("\n");
return ;
}
nowtime.cpp
执行结果:
$tt
---------------------------struct timespec---------------------------------------
[time(NULL)] : 1400233995
clock_gettime : tv_sec=1400233995, tv_nsec=828222000
clock_gettime : date_time=2014-05-16 17:53:15, tv_nsec=828222000
---------------------------struct timeval----------------------------------------
[time(NULL)] : 1400233995
gettimeofday: tv_sec=1400233995, tv_usec=828342
gettimeofday: date_time=2014-05-16 17:53:15, tv_usec=828342
over
struct timespec 和 struct timeval的更多相关文章
- linux高精度struct timespec 和 struct timeval
一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...
- struct inode 和 struct file
1.struct inode──字符设备驱动相关的重要结构介绍 内核中用inode结构表示具体的文件,而用file结构表示打开的文件描述符.Linux2.6.27内核中,inode结构体具体定义如下: ...
- (linux)struct inode 和 struct file
转自:http://www.cnblogs.com/QJohnson/archive/2011/06/24/2089414.html 1.struct inode──字符设备驱动相关的重要结构介绍 内 ...
- struct timeval 和 struct timespec
struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...
- [转载]彻底弄清struct和typedef struct
struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...
- struct和typedef struct彻底明白了
struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...
- struct和typedef struct
转自:http://www.cnblogs.com/qyaizs/articles/2039101.html struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++ ...
- struct 与 typedef struct
1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等). 在编程中使用typede ...
- struct ifconf和struct ifreq,获取网线插入状态
这两天看用C获取当前网口的插入网线状态的程序,遇见了这两个不熟悉的结构体,看了头文件中的说明和详细. struct ifreq 这个结构定义在include/net/if.h,用来配置ip地址,激活接 ...
随机推荐
- JNI总结(一)
JNI 是 Java平台中的一个强大特性.应用程序能够通过 JNI把 C/C++代码集成进 Java程序中.通过 JNI.开发人员在利用 Java平台强大功能的同一时候,又不必放弃对原有代码的投资.由 ...
- MyEclipse设置默认的目光格式
首先,选择菜单 windows-->preference Java-->Code Style-->Code Templates code-->new Java files 然后 ...
- android的项目文件介绍
1.res目录存放Android的各种资源文件,比如layout存放布局文件main.xml,values存放各种xml格式的资源文件,字符串资源strings.xml,颜色资源文件:colors.x ...
- cocos2d-x引擎实现$1Unistroke Recognizer手势识别
$1 Unistroke(单笔画) Recognizer官网 http://depts.washington.edu/aimgroup/proj/dollar/ (在官网还有多笔画的识别库) 代码下载 ...
- oracle中对LONG列进行查询
SQL> CREATE TABLE T_LONG (ID NUMBER, LONG_COL LONG); 表已创建. SQL> INSERT INTO T_LONG VALUES (1, ...
- IIS7下w3wp.exe进程CPU100%问题解决办法
IIS下经常会出现w3wp.exe进程的CPU使用率达到100%的情况,在IIS7出现之前,要想确定问题所在,可以通过WinDbg来调试分析,但整个过程对技术水平要求非常高,可以参考http:// ...
- php使用mysql_query查询超大结果集超内存的解决方法
再使用mysql_query查询超大结果集的时候会出现超出内存限制的致命错误,这是因为mysql_query采用的是查询全部结果然后把结果集全部缓存到内存中的方式. mysql的查询还提供了另外一种查 ...
- c语言面试题之sizeof
c语言面试题之sizeof */--> c语言面试题之sizeof Table of Contents 1. sizeof 1 sizeof sizeof是c语言中判断数据类型或者表达式的长度符 ...
- 目前电脑的硬件尺寸参数,计划弄个小一些的ATX机箱
显卡:讯景R9 370x 尺寸:234×115×39mm 主板:技嘉GA-970A-DS3P 尺寸: 30.5X21.5 cm
- c# 远程监控(2) 摄像头调研及模拟
经过N多调研,最终选择了OpenCV(Emgu CV) ** 至于DirectShow, OpenCV等等其他大家可以百度,在这里我就不再赘述 环境:vs2010 vs2012 vs2013均可 Op ...