1. time()提供了秒级的精确度
  2. 1、头文件 <time.h>
  3. 2、函数原型
  4. time_t time(time_t * timer)
  5. 函数返回从TC1970-1-1 0:0:0开始到现在的秒数
  6. 用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。
  7. #include <time.h>
  8. #include <stdio.h>
  9. int main(void)
  10. {
  11. time_t t;
  12. t = time(NULL);
  13. printf("The number of seconds since January 1, 1970 is %ld",t);
  14. return 0;
  15. }
  16. #include <stdio.h>
  17. #include <stddef.h>
  18. #include <time.h>
  19. int main(void)
  20. {
  21. time_t timer;//time_t就是long int 类型
  22. struct tm *tblock;
  23. timer = time(NULL);//这一句也可以改成time(&timer);
  24. tblock = localtime(&timer);
  25. printf("Local time is: %s/n",asctime(tblock));
  26. return 0;
  27. }
  1. gettimeofday()提供了微秒级的精确度
  2. 1、头文件 <time.h>
  3. 2、函数原型
  4. int gettimeofday(struct timeval *tv, struct timezone *tz);
  5. gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中(可用NULL)。
  6. 参数说明:
  7. timeval结构定义为:
  8. struct timeval
  9. {
  10. long tv_sec; /*秒*/
  11. long tv_usec; /*微秒*/
  12. };
  13. timezone 结构定义为:
  14. struct timezone
  15. {
  16. int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
  17. int tz_dsttime; /*日光节约时间的状态*/
  18. };
  19. 上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
  20. DST_NONE /*不使用*/
  21. DST_USA /*美国*/
  22. DST_AUST /*澳洲*/
  23. DST_WET /*西欧*/
  24. DST_MET /*中欧*/
  25. DST_EET /*东欧*/
  26. DST_CAN /*加拿大*/
  27. DST_GB /*大不列颠*/
  28. DST_RUM /*罗马尼亚*/
  29. DST_TUR /*土耳其*/
  30. DST_AUSTALT /*澳洲(1986年以后)*/
  31. 返回值: 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
  32. #include<stdio.h>
  33. #include<time.h>
  34. int main(void)
  35. {
  36. struct timeval tv;
  37. struct timezone tz;
  38. gettimeofday (&tv , &tz);
  39. printf(“tv_sec; %d/n”, tv,.tv_sec) ;
  40. printf(“tv_usec; %d/n”,tv.tv_usec);
  41. printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest);
  42. printf(“tz_dsttime, %d/n”,tz.tz_dsttime);
  43. return 0;
  44. }
  1. clock_gettime( ) 提供了纳秒级的精确度
  2. 1、头文件 <time.h>
  3. 2、编译&链接。在编译链接时需加上 -lrt ;因为在librt中实现了clock_gettime函数
  4. 3、函数原型
  5. int clock_gettime(clockid_t clk_id, struct timespect *tp);
  6. 参数说明:
  7. clockid_t clk_id 用于指定计时时钟的类型,有以下4种:
  8. CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变
  9. CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
  10. CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
  11. CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
  12. struct timespect *tp用来存储当前的时间,其结构如下:
  13. struct timespec
  14. {
  15. time_t tv_sec; /* seconds */
  16. long tv_nsec; /* nanoseconds */
  17. };
  18. 返回值。0成功,-1失败
  19. #include<stdio.h>
  20. #include<time.h>
  21. int main()
  22. {
  23. struct timespec ts;
  24. clock_gettime(CLOCK_REALTIME, &ts);
  25. printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec);
  26. clock_gettime(CLOCK_MONOTONIC, &ts);//打印出来的时间跟 cat /proc/uptime 第一个参数一样
  27. printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec);
  28. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  29. printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);
  30. clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
  31. printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);
  32. printf("/n%d/n", time(NULL));
  33. return 0;
  34. }
  35. /proc/uptime里面的两个数字分别表示:
  36. the uptime of the system (seconds), and the amount of time spent in idle process (seconds).
  37. 把第一个数读出来,那就是从系统启动至今的时间,单位是秒
    1. _ftime()提供毫秒级的精确度
    2. 1、头文件 <sys/types.h> and <sys/timeb.h>
    3. 2、函数原型
    4. void _ftime(struct _timeb *timeptr);
    5. 参数说明:
    6. struct _timeb
    7. {
    8. time_t time;
    9. unsigned short millitm;
    10. short timezone;
    11. short dstflag;
    12. };
    13. #include <stdio.h>
    14. #include <sys/timeb.h>
    15. #include <time.h>
    16. void main( void )
    17. {
    18. struct _timeb timebuffer;
    19. char *timeline;
    20. _ftime( &timebuffer );
    21. timeline = ctime( & ( timebuffer.time ) );
    22. printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );
    23. }

linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime的更多相关文章

  1. linux下常用命令备忘

    转自:Linux 命令集锦 linux下查看监听端口对应的进程 # lsof -i:9000 # lsof -Pnl +M -i4 如果退格键变成了:"^h". 终端连接unix删 ...

  2. 20145239 Linux下常用的ls命令总结

    20145239 Linux下常用的ls命令总结 通过学习本周的教学视频和要求掌握的内容,发现ls命令被使用的次数非常多,但作为一个初学者,可能我只会ls或者顶多ls -l两种用法.但其实ls是一个非 ...

  3. Linux下常用压缩 解压命令和压缩比率对比

    常用的格式有:tar, tar.gz(tgz), tar.bz2, 不同方式,压缩和解压方式所耗CPU时间和压缩比率也差异也比较大. 1. tar只是打包动作,相当于归档处理,不做压缩:解压也一样,只 ...

  4. linux下常用关机命令

    linux下常用的关机命令有:shutdown.halt.poweroff.init:重启命令有:reboot.下面本文就主要介绍一些常用的关机命令以及各种关机命令之间的区别和具体用法. 首先来看一下 ...

  5. MongoDB在Linux下常用优化设置

    MongoDB在Linux下常用优化设置 以下是一些MongoDB推荐的常用优化设置.在生产环境下选取合适的参数值,例如预读值和默认文件描述符数目等,会对系统性能有很大的影响. 1.关闭数据库文件的 ...

  6. Linux下用C获取当前时间

    Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() {  ti ...

  7. linux下常用的截图、录屏工具

    录屏: 在linux下常用的录屏工具有5种,可以baidu或者google下喔,我选用的是recordMydesktop,使用非常方便,用时注意先把每秒桢数调高,否则效果必然很差. 在ubuntu下可 ...

  8. Linux下常用的ftp操作命令

    Linux下常用的ftp操作命令 =========== 完美的分割线 ============= 1.登陆ftp服务器 ftp [IP] [PORT] # 登陆ftp服务器,本机登陆可以不写IP 实 ...

  9. Linux下常用压缩 解压命令与压缩比率对比

    常用的格式有:tar, tar.gz(tgz), tar.bz2, 不同方式,压缩和解压方式所耗CPU时间和压缩比率也差异也比较大. 1. tar只是打包动作,相当于归档处理,不做压缩:解压也一样,只 ...

随机推荐

  1. selenium+python 自动化

    <a class="big_images_new" target="_blank" href="http://photo.xcar.com.cn ...

  2. 对Largest函数的测试

    题目:查找list[]中的最大值:int Largest(int list[], int length); int Largest(int list[], int length) { int i,ma ...

  3. php----函数大全

    字符串函数 数组函数 数学函数

  4. Pytorch相关内容

    ---恢复内容开始--- Pytorch中文官方文档:https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn P ...

  5. C#编程之神奇程序找数

    C#编程之神奇程序找数 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分 ...

  6. AngularJs 学习 (二)

    紧接着第一部分: 推荐阅读: http://adrianmejia.com/blog/2014/10/03/mean-stack-tutorial-mongodb-expressjs-angularj ...

  7. MySql点点滴滴(一)之可视化工具介绍

    以下的文章主要介绍的是10个可以简化开发过程的MySQL工具,其中包括MySQL Workbench.phpMyAdmin.Aqua Data Studio,以及SQLyog与MYSQL Front等 ...

  8. vue 实战语法错误导致的问题记录

    1. 在组件中引入mapActions import mapActions  from 'vuex'   而不是  getActions 2.引入 mutations-type.js import   ...

  9. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  10. Beta阶段DAY3

    一.提供当天站立式会议照片一张 二.每个人的工作 1.讨论项目每个成员的昨天进展 刘阳航:尝试改进UI,美化界面. 林庭亦:调整难度设置. 郑子熙:尝试改进UI,美化界面. 陈文俊:调整难度设置. 2 ...