linux高精度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 ;
- }
- #include <time.h>
- // 返回自系统开机以来的毫秒数(tick)
- unsigned long GetTickCount()
- {
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (ts.tv_sec * + ts.tv_nsec / );
- }
- int main()
- {
- struct timespec time1 = { , };
- clock_gettime(CLOCK_REALTIME, &time1);
- printf("CLOCK_REALTIME: %d, %d\n", time1.tv_sec, time1.tv_nsec);
- clock_gettime(CLOCK_MONOTONIC, &time1);
- printf("CLOCK_MONOTONIC: %d, %d\n", time1.tv_sec, time1.tv_nsec);
- clock_gettime(CLOCK_MONOTONIC_RAW, &time1);
- printf("CLOCK_MONOTONIC_RAW: %d, %d\n", time1.tv_sec, time1.tv_nsec);
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
- printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d\n", time1.tv_sec,
- time1.tv_nsec);
- clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time1);
- printf("CLOCK_THREAD_CPUTIME_ID: %d, %d\n", time1.tv_sec,
- time1.tv_nsec);
- printf("\n%d\n", time(NULL));
- printf("tick count in ms: %ul\n", GetTickCount());
- return ;
- }
linux高精度struct timespec 和 struct timeval的更多相关文章
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- (linux)struct inode 和 struct file
转自:http://www.cnblogs.com/QJohnson/archive/2011/06/24/2089414.html 1.struct inode──字符设备驱动相关的重要结构介绍 内 ...
- struct inode 和 struct file
1.struct inode──字符设备驱动相关的重要结构介绍 内核中用inode结构表示具体的文件,而用file结构表示打开的文件描述符.Linux2.6.27内核中,inode结构体具体定义如下: ...
- struct timeval 和 struct timespec
struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...
- Linux内核中的双向链表struct list_head
一.双向链表list_head Linux内核驱动开发会经常用到Linux内核中经典的双向链表list_head,以及它的拓展接口和宏定义:list_add.list_add_tail.list_de ...
- 转:struct sockaddr与struct sockaddr_in ,struct sockaddr_un的区别和联系
在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:typedef unsigned short sa_family_t; ...
- linux 高精度定时器例子
//author:DriverMonkey //phone:13410905075 //mail:bookworepeng@Hotmail.com //qq:196568501 #include &l ...
- struct msghdr和struct cmsghdr【转载】
理解struct msghdr当我第一次看到他时,他看上去似乎是一个需要创建的巨大的结构.但是不要怕.其结构定义如下:struct msghdr { void *msg_name ...
- 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...
随机推荐
- Android倒计时功能的实现
Android中的倒计时的功能(也能够直接使用CountDownTimer这个类直接实现,相关此Demo可查看我的博客).參考了网上写的非常好的一个倒计时Demo: watermark/2/text/ ...
- 分析iOS Crash文件,使用命令符号化iOS Crash文件
TBMainClient.ipa改名为TBMainClient.zip并解压得到TBMainClient.app 然后将TBMainClient.app TBMainClient.app.d ...
- 丑闻第三季 /全集Scandal迅雷下载
丑闻 第三季 Scandal Season 3 (2013)本季看点:在经典美剧<老友记第一季>中饰演菲比的女星莉莎·库卓,即将加盟描写华府危机公关的ABC剧集<丑闻>(Sca ...
- Android应用icon和闪屏splash的尺寸
icon (尺寸为px) 目录 尺寸 (width * height) drawable 72 x 72 drawable-hdpi 72 x 72 drawable-ldpi 36 x 36 dra ...
- Java多线程中join方法的理解
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. t.join( ...
- 虚拟私有云(Virtual Private Cloud,专有网络)配置方式总结
虚拟私有云 虚拟私有云(Virtual Private Cloud)是用户在云上申请的隔离的.私密的虚拟网络环境.用户可以自由配置VPC内的IP地址段.子网.安全组等子服务,也可以申请弹性带宽和弹性公 ...
- Installing Hyperledger Fabric v1.1 on Ubuntu 16.04 — Part I
There is an entire library of Blockchain APIs which you can select according to the needs that suffi ...
- QT国际化(中英转换)
转载:https://blog.csdn.net/u012528526/article/details/54707233 QT国际化(中英转换) 我们都知道在安卓中,想做国际化很简单,只需要建立对应的 ...
- Nutch1.7学习笔记:基本环境搭建及使用
Nutch1.7学习笔记:基本环境搭建及使用 作者:雨水,时间:2013-10-31博客地址:http://blog.csdn.net/gobitan 说明:Nutch有两个主版本1.x和2.x,它们 ...
- pthread_once重塑singleton模式
单件模式是非线程安全的: // Single threaded version class Foo { private Helper helper = null; public Helper getH ...