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地址,激活接 ...
随机推荐
- Ubuntu下安装Nginx,PHP5(及PHP-FPM),MySQL
.简介: Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱.虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很多 ...
- 使用air进行移动app开发常见功能和问题(一)
1. 获取最近联系人 思路:侦听Geolocation的update事件,获取经度和纬度信息,再把坐标信息上传至服务器,服务器比较坐标信息算出距离,返回最近位置的若干个人. update时间在2种情 ...
- 读取XML帮助类
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ...
- php面试常用算法
这些都是真实的IT公司招聘PHP程序员的面试题,这些都是简单的基本算法.包括:冒泡算法.快速排序算法.二分查找算法.顺序算法. 冒泡排序,对象可以是一个数组 01 function bubble_so ...
- Android(java)学习笔记134:Handler用法总结 和 秒表案例
一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...
- linux中shell编程
shell编程 1 echo -e 识别\转义符 \a \b \t \n \x十六进制 \0八进制 等等 #!/bin/bash echo -e "hello world" 执行脚 ...
- Wireshark 过滤条件
做应用识别这一块经常要对应用产生的数据流量进行分析. 抓包采用wireshark,提取特征时,要对session进行过滤,找到关键的stream,这里总结了wireshark过滤的基本语法,供自己以后 ...
- sscanf、strsep
#include <stdio.h> #include <string.h> int main() { char token[] ="abdzxbcdefgh&quo ...
- web项目设计与开发——DBHelper3
本次学习的内容为根据DBHelper对数据库里的数据进行增删改查 具体内容为: 一.编写程序 1.创建工程——Mangage 2.在src目录下创建五个包,分别为DAO,DBHelper,Ent ...
- 【原】NGUI中的UIAnchor脚本功能
UIAnchor的功能是把对象锚定在屏幕的边缘(左上,左中,左下,上,中,下,右上,右中,右下),或缩放物体使其匹配屏幕的尺寸. 在1.90版本后,拉长(缩放)的功能被放到UIStretch中,UIA ...