一、时间类型

linux中编程通常需要用到时间变量,和相关的时间操作函数。常用的时间类型有:

time_t 、struct  timeval、struct   timespec、struct   tm

在用到相关的类型和函数时,需要加上头文件:#include <time.h>

  • time_t: 存储从1970年到现在经过了多少。格式为long int。UTC时间。
  • struct  timeval:提供秒和微秒单位,最高精度是微秒
/* A time value that is accurate to the nearestmicrosecond but also has a range of years.  */
struct timeval{__time_t tv_sec;     /* Seconds.  */__suseconds_t tv_usec;   /* Microseconds.  */};
# endif /* struct timeval */
  • struct   timespec:提供秒和纳秒单位,最高精度是纳秒
struct timespec{__time_t tv_sec;     /* Seconds.  */__syscall_slong_t tv_nsec;   /* Nanoseconds.  */};
  • struct   tm:详细时间的结构体
struct tm
{int tm_sec;            /* Seconds. [0-60] (1 leap second) */int tm_min;            /* Minutes. [0-59] */int tm_hour;           /* Hours.   [0-23] */int tm_mday;           /* Day.     [1-31] */int tm_mon;            /* Month.   [0-11] */int tm_year;           /* Year - 1900.  */int tm_wday;         /* Day of week. [0-6] */int tm_yday;            /* Days in year.[0-365] */int tm_isdst;         /* DST.     [-1/0/1]*/# ifdef   __USE_MISClong int tm_gmtoff;       /* Seconds east of UTC.  */const char *tm_zone;     /* Timezone abbreviation.  */
# elselong int __tm_gmtoff;     /* Seconds east of UTC.  */const char *__tm_zone;   /* Timezone abbreviation.  */
# endif
};

二、时间函数

char *asctime(const struct tm* timeptr);
//将结构中的信息转换为真实世界的UTC时间,以字符串的形式显示char *ctime(const time_t *timep);
//将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不同double difftime(time_t time1, time_t time2);
//返回两个时间相差的秒数int gettimeofday(struct timeval *tv, struct timezone *tz);
//返回当前距离1970年的秒数和微秒数,后面的tz是时区,一般不用struct tm* gmtime(const time_t *timep);
//将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针stuct tm* localtime(const time_t *timep);
//和gmtime类似,但是它是经过时区转换的时间。time_t mktime(struct tm* timeptr);
//将struct tm 结构的时间转换为从1970年至今的秒数time_t time(time_t *t);
//取得从1970年1月1日至今的秒数
//eg,totalsec = time(NULL) or time(&totalsec);int clock_gettime(clockid_t clk_id, struct timespec* tp);
/*可以根据需要,获取不同要求的精确时间
参数
clk_id : 检索和设置的clk_id指定的时钟时间。
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,
中间时刻如果系统时间被用户改成其他,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间*///eg. 获得从开机到现在的时间:clock_gettime(CLOCK_MONOTONIC,&tspec);

三、示例

1、设置系统时间:

struct timeval tv;
tv.tv_sec = (time_t)gps_utctime_sec;
tv.tv_usec = 0;
if(settimeofday (&tv, NULL) < 0){  MSG("[ GPS ]:Set system datatime error,may need root permissions!\n");
}
else{MSG("[ GPS ]:GPS valid,update system time!\n");

2、读取当前时间tick

/*! get the current system time tick, second unit */
static uint32_t getCurrSysTick(void)
{time_t timep;time (&timep);return timep;//seconds from 1970-1-1:0:0:0
}

3、读取当前时间字符串:Fri Dec 20 14:44:18 2019

static char* getCurrSysTime(void)
{time_t timep;time (&timep);return asctime(localtime(&timep));
}

4、设置延时

static void delay_ms(unsigned int dms)
{struct timespec sleeper, dummy ;sleeper.tv_sec  = (time_t)(dms / 1000) ;sleeper.tv_nsec = (long)(dms % 1000) * 1000000 ;nanosleep (&sleeper, &dummy);//delay 1ms
}

5、获取当前时间tm

struct tm* t_tm;
t_tm = localtime((time_t *)&meas_gps_utctime.tv_sec);
/* test gps */
MSG("today is %4d-%02d-%02d %02d:%02d:%02d\n",t_tm->tm_year+1900,t_tm->tm_mon+1,t_tm->tm_mday,t_tm->tm_hour,t_tm->tm_min,t_tm->tm_sec);  

Linux时间转换图:

参考:

  1. c++ 时间类型详解 time_t
  2. linux应用time和timezone
  3. linux下的clock_gettime()获取时间函数

Linux tm time_t timeval timespec以及与时间相关函数用法相关推荐

  1. [c/c++][Linux] 时间戳转换为tm,time_t,timeval

    ref https://blog.csdn.net/GreenTeemo/article/details/102586265 说明 时间戳为2020/03/25 11:09:42 .761197,其中 ...

  2. C++性能之战(0)--Linux时间相关函数总结

    0. 写在最前面 本文持续更新地址:https://haoqchen.site/2019/12/17/linux-time-summary/ 最近写程序涉及到时间相关的,包括当前时间呀,进程运行的时间 ...

  3. 计算可由time_t数据类型表示的最近时间。如果超出了这一时间将会如何?

    计算可由time_t数据类型表示的最近时间.如果超出了这一时间将会如何? C++11标准规定long类型最少占32位,在我的计算机上,系统使用long int来实现time_t,实际使用64位来表示l ...

  4. linux内核和cpu指令集,Linux之父:Intel别浪费时间在AVX512这类指令集、多堆核心才是正道...

    原标题:Linux之父:Intel别浪费时间在AVX512这类指令集.多堆核心才是正道 在最近一次邮件交流中,Linux之父Linus Torvalds对Intel的处理器战略表达意见. 他谈到&qu ...

  5. linux时间同步到win7,mac与win7时间不同步怎么办_mac与win7时间不准如何解决

    由于工作或者学习的需要,mac电脑安装完win7双系统之后,有的用户发现mac系统和win7系统的时间不同步,虽然有其它可以看的时间设备,但是在使用电脑是可能要选择最方便的方式,因此有什么方法能够解决 ...

  6. linux系统时钟使用utc,如何设置时间,时区和同步系统时钟使用timedatectl命令

    该timedatectl命令是RHEL / CentOS 7和Fedora 21+根据发行,这是作为systemd系统和服务经理,用于基于sysvinit的后台程序的Linux发行版使用旧的传统dat ...

  7. Linux系统下查看和修改 系统时间和硬件时间

    Linux系统下查看和修改 系统时间和硬件时间 1.具体命令使用如下: 查看当前系统时间 [root@littlelawson hbase-1.4.0]# date Mon Jun 18 10:36: ...

  8. linux cat时间段,Linux Cat命令及使用详解时间

    Linux Cat命令及使用详解时间 Red Hat Linux 有一个工具程序,它能够帮助你保留简短列表,将这些列表收集起来,甚至向你透漏一点你的系统信息.这个工具叫做Linux Cat,它是con ...

  9. QT在linux环境下读取和设置系统时间

    QT在linux环境下读取和设置系统时间 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:Fedora12 开发软件:QT 读取系统时间 ...

最新文章

  1. js array 删除指定元素_数组--学习笔记(数据结构数组 /js数组)
  2. Spark MLlib之使用Breeze操作矩阵向量
  3. FPGA开发要懂得使用硬件分析仪调试——ILA
  4. Andriod --- JetPack (二):LifeCycle 的诞生
  5. RobotFramework自动化框架—数据驱动测试
  6. 自然哲学的数学原理_物理起源点,牛顿《自然哲学的数学原理》
  7. Oracle 日期加减运算
  8. 数据库镜像怎么还原数据库_如何创建数据库镜像
  9. 北航院系和数字的对应关系
  10. 996 的程序员,只能云养娃吗?
  11. android 混淆字符串,android 代码混淆
  12. 阿里巴巴 研发工程师Java暑期实习一面
  13. iphone屏幕圆角插件_苹果iPhone6s也能分屏?越狱新插件助你实现
  14. linux环境sphinx搭建,Sphinx安装配置应用
  15. 【Android】制作一个简易的画板
  16. 【外卖cps源码分享】支持美团饿了么
  17. 单GDB调试RISC-V CPU 多核时,continue 命令行为解析
  18. 剑指XX游戏(六) - 轻松搞定面试中的红黑树问题
  19. vmware虚拟机卸载清理工具 1.3 最新版
  20. ROS学习笔记(十)——ROS试用练习(一)

热门文章

  1. 什么从什么写短句_从什么到从什么造句
  2. 现在mfc的现状如何_天玑云客:微信代运营现在什么现状?如何挑选合适的代运营公司?...
  3. 计算机语言乍么设置,电脑如何设置语言
  4. 开源cms - 资料收集
  5. Proteus仿真单片机:PIC18单片机的仿真
  6. Exynos4412 Uboot 的使用与烧写
  7. 微信小程序打开pdf文件;uni-app下载打开pdf文件;uni-app微信小程序下载打开pdf文件预览;
  8. Nginx下配置小绿锁https
  9. Taro+react开发(77):taro项目目录介绍
  10. 前端学习(2916):事件绑定