1. 结构基本定义

1.1 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 );

1.2 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[64];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[64];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 0;
}
#include <time.h>// 返回自系统开机以来的毫秒数(tick)
unsigned long GetTickCount()
{struct timespec ts;clock_gettime(CLOCK_MONOTONIC, &ts);return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}int main()
{struct timespec time1 = { 0, 0 };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 0;
}

Linux高精度struct timespec和struct timeval相关推荐

  1. struct timespec 和 struct timeval

    struct timespec 和 struct timeval time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * ...

  2. Linux高精度struct timespec 和 struct timeval

    struct timespec now_ts;clock_gettime(CLOCK_MONOTONIC, &now_ts);printf("os tick times, %ld s ...

  3. Linux时间相关知识小结:struct timeval、timespec、gettimeofday、time、localtime....

    前言 我们在linux平台进行开发时,时间相关的操作基本上都会遇到,本文就对常用的时间相关的结构体.接口进行分析小结. 常见类型.结构体定义 timespec 原型 struct timespec { ...

  4. struct timeval 和 struct timespec 应用小结

    在基于linux的C编程中,经常会看到 struct timeval和struct timespec 这两个跟时间有关的结构体,有时候会容易混淆,先看下这两个结构体的定义,以linux-2.6.35为 ...

  5. 结构体struct timeval 和 struct timespec的定义

    结构体struct timeval 和 struct timespec的定义均在头文件<sys/time.h>中,具体定义如下: struct timeval {long tv_sec; ...

  6. struct timeval结构体 以及 gettimeofday()函数、struct timespec结构体

    struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Seconds. */ __ ...

  7. struct timeval 和 struct timespec

    1. 使用timeval,微秒级(1s = 1 000ms = 1 000 000us = 1 000 000 000ns) struct timeval {long tv_sec; //second ...

  8. linux '$^t' 时间,Linux C时间函数 time_t struct tm

    Linux C时间函数 time_t struct tm #include 关于时间的类型: time_t long型,表示从1970年1月1日到现在经过的秒数. struct tm { int tm ...

  9. linux内核那些事之struct page

    struct page page(页)是linux内核管理物理内存的最小单位,内核将整个物理内存按照页对齐方式划分成千上万个页进行管理,内核为了管理这些页将每个页抽象成struct page结构管理每 ...

最新文章

  1. 基于ARMv8的固件系统体系结构
  2. 局部邻域搜索-爬山法,模拟退火,禁忌,迭代局部搜索,变邻域局部搜索的简单阐释
  3. shell 脚本 进行sqlite3初始化
  4. svn添加钩子hook
  5. css 超出隐藏滚动条_css 之内容溢出滚动,隐藏滚动条(解决火狐浏览隐藏不了滚动条问题)...
  6. go sync.WaitGroup源码分析
  7. 双向特征融合的数据自适应SAR图像舰船目标检测模型
  8. vim粘贴乱码的原因
  9. 灵云语音识别(ASR)实现实时识别
  10. 那些必须要知道的Javascript
  11. 【Mac技巧】怎样隐藏电脑Dock栏
  12. 【读书笔记】《软件方法(上)业务建模和需求》(第2版)习题答案
  13. java api在jdk哪里,jdk api文档在哪里
  14. 【matlab】设置中文版帮助
  15. jQuery实现button按钮提交表单
  16. 比特率和波特率的区别
  17. 微信公众号和服务器的关系,微信公众号、订阅号、服务号之间的关系和区别
  18. linux命令行连接蓝牙音箱,有些Linux发行版用蓝牙连接天猫精灵和小爱音箱没声音...
  19. linux sftp put 文件夹,使用sftp命令上传文件夹方法
  20. 金蝶kis修改服务器,金蝶kis 修改服务器地址

热门文章

  1. PostgreSQL数据库备份和恢复
  2. PHP-Opcache优化
  3. 《Web编程技术》学习笔记(二)
  4. 解决pip时错误:PermissionError: [Errno 13] Permission denied
  5. 树莓派:访问Bitlocker To Go加密过的磁盘
  6. 产生低峰均功率比(low-PAPR)序列
  7. Python---turtle模块---美国国旗的绘画
  8. python可视化工具之matplotlib(1)基本图表
  9. 故障排查 ❀ 网页搜索出现的“404”
  10. css 清空ios端_CSS 修改 IOS 默认按钮样式