linux获取时钟接口

概述

linux系统开发环境中,需要获取各种类型时钟,针对这些情况做了一个简单整理。
首先对时钟接口的需求大概包括有:
1:获取当前时钟,这个在内核中一般表现为UTC时钟(从1970.1.1 00:00:00到当前时间的秒及纳秒)
2:获取系统启动后单调时钟,即系统启动以来的秒和纳秒数
3:设置时钟
4:针对时钟不同结构体数据之间的转换

时区

时区分类:
UTC:
整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)GMT:
格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)CST:
中国标准时间 (China Standard Time)DST:
夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时钟拨快一小时,以提早日光的使用。(中国不使用)GMT + 8 = UTC + 8 = CST

linux系统时钟一般操作

读写时钟

//查看时钟
kongcb@tcu-Lenovo:~$ date
2022年 05月 17日 星期二 14:17:25 CST
kongcb@tcu-Lenovo:~$ date -R
Tue, 17 May 2022 14:17:30 +0800        //+0800表示东八区时区//设置时钟
kongcb@tcu-Lenovo:~$ sudo date -s "20220517 14:25"
[sudo] password for kongcb:
2022年 05月 17日 星期二 14:25:00 CST
kongcb@tcu-Lenovo:~$ date -R
Tue, 17 May 2022 14:25:08 +0800

时区操作

//获取时区
kongcb@tcu-Lenovo:~$ timedatectl
Local time: 二 2022-05-17 14:26:17 CST
Universal time: 二 2022-05-17 06:26:17 UTC   //看到UTC时间比CST晚8小时
Timezone: Asia/Chongqing (CST, +0800)  //时区CST,亚洲重庆
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a//设置时区
kongcb@tcu-Lenovo:~$ sudo timedatectl set-timezone Asia/Shanghai
kongcb@tcu-Lenovo:~$ timedatectl
Local time: 二 2022-05-17 14:31:43 CST
Universal time: 二 2022-05-17 06:31:43 UTC
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a    //查看时区
kongcb@tcu-Lenovo:~$ timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
........

同步网络时钟

//同步网络时钟
kongcb@tcu-Lenovo:~$ sudo apt-get install ntp
Reading package lists... Done
Building dependency tree
........//查看本地时钟和网路时钟差异
kongcb@tcu-Lenovo:~$ ntpdate -d cn.pool.ntp.org
17 May 14:39:36 ntpdate[12707]: ntpdate 4.2.6p5@1.2349-o Wed Oct  9 18:57:00 UTC 2013 (1)
Looking for host cn.pool.ntp.org and service ntp
host found : 111.230.189.174
transmit(111.230.189.174)
receive(111.230.189.174)
transmit(94.237.64.20)
.......
server 94.237.64.20, port 123
stratum 2, precision -25, leap 00, trust 000
refid [94.237.64.20], delay 0.12453, dispersion 0.00023
transmitted 4, in filter 4
reference time:    e62dbdc3.f523cdc3  Tue, May 17 2022 14:22:59.957
originate timestamp: e62dc0b0.235c07cd  Tue, May 17 2022 14:35:28.138
transmit timestamp:  e62dc1af.3064ec71  Tue, May 17 2022 14:39:43.189
filter delay:  0.12569  0.12532  0.12471  0.12453 0.00000  0.00000  0.00000  0.00000
filter offset: -255.100 -255.100 -255.100 -255.1000.000000 0.000000 0.000000 0.000000
delay 0.12453, dispersion 0.00023
offset -255.100391      //差别有255秒//同步网络时钟
kongcb@tcu-Lenovo:~$ sudo ntpdate -u cn.pool.ntp.org   //国内时钟服务器
17 May 14:38:23 ntpdate[12737]: adjust time server 202.118.1.81 offset 0.000369 sec
kongcb@tcu-Lenovo:~$ date
2022年 05月 17日 星期二 14:38:27 CST

ntp常用服务器

//国内
cn.pool.ntp.org  中国开源免费NTP服务器
ntp1.aliyun.com 阿里云NTP服务器
ntp2.aliyun.com 阿里云NTP服务器
time1.aliyun.com 阿里云NTP服务器
time2.aliyun.com 阿里云NTP服务器
//海外
pool.ntp.org 开源免费NTP服务器
time1.apple.com 苹果NTP服务器
time2.apple.com 苹果NTP服务器
time3.apple.com 苹果NTP服务器
time4.apple.com 苹果NTP服务器
time5.apple.com 苹果NTP服务器
time1.google.com 谷歌NTP服务器
time2.google.com 谷歌NTP服务器
time3.google.com 谷歌NTP服务器
time4.google.com 谷歌NTP服务器

linux应用程序时钟操作

接口类型

接口相关头文件
#include <time.h>
相关数据结构体定义:
timespec结构包括:
struct timespec {time_t tv_sec; /* 秒*/long tv_nsec; /* 纳秒*/
};struct tm {/** the number of seconds after the minute, normally in the range* 0 to 59, but can be up to 60 to allow for leap seconds*/int tm_sec;/* the number of minutes after the hour, in the range 0 to 59*/int tm_min;/* the number of hours past midnight, in the range 0 to 23 */int tm_hour;/* the day of the month, in the range 1 to 31 */int tm_mday;/* the number of months since January, in the range 0 to 11 */int tm_mon;/* the number of years since 1900 */long tm_year;/* the number of days since Sunday, in the range 0 to 6 */int tm_wday;/* the number of days since January 1, in the range 0 to 365 */int tm_yday;
};函数原型:
int clock_gettime(clockid_t clk_id, struct timespec *tp);
其中,cld_id类型有一下几种:
#define CLOCK_REALTIME          0 //可设定的系统级实时时钟,真实时间度量,可变更
#define CLOCK_MONOTONIC         1//不可设定的恒定态时钟,系统启动不会变,不会随时钟修改跳跃性变化
#define CLOCK_PROCESS_CPUTIME_ID    2//每进程CPU时间的时钟,测量调用进程消耗的用户和系统CPU时间
#define CLOCK_THREAD_CPUTIME_ID     3//每线程CPU时间的时钟,基本同上,不过是进程的时间
#define CLOCK_MONOTONIC_RAW         4//类似MONOTONIC,但不会收NTP影响
#define CLOCK_REALTIME_COARSE       5//同CLOCK_REALTIME,以较小代价获取低分辨率时间戳
#define CLOCK_MONOTONIC_COARSE      6//同CLOCK_MONOTONIC,以较小代价获取低分辨率时间戳
#define CLOCK_BOOTTIME              7//类似MONOTONIC,但系统suspend后依然会增加注意:对于CLOCK_MONOTONIC时钟,系统启动后置0,之后累加,但也并非不能修改,有几种情况会有修改:
1:时钟的clock-source修改了,比如时钟从高频时钟源转到低频的
2:timezone修改了,如从local time修改为UTC time
不过一般正常操作不会碰到这种情况了,且嵌入式设备系统一般不会配置ntp等等,实际上想操作也不行

获取时钟

struct timespec time;
//获取当前系统时钟秒数
int nRet = clock_gettime(CLOCK_REALTIME_COARSE, &time);
time_t ttSec =  time.tv_sec;//获取当前时钟毫秒数
int nRet = clock_gettime(CLOCK_REALTIME, &time);
long long llmSec =  time.tv_sec*1000 + time.tv_nsec/1000000;//获取启动一来的秒数
int nRet = clock_gettime(CLOCK_MONOTONIC_COARSE, &time);
time_t ttSecMon =  time.tv_sec;//获取启动一来的毫秒数
int nRet = clock_gettime(CLOCK_MONOTONIC, &time);
long long llmSecMonCnt =  time.tv_sec*1000 + time.tv_nsec/1000000;

设置时钟

//设置当前时钟
#include <time.h>int stime(time_t *t) //time_t *t是UTC时间,1970-1-1 00:00:00到设定时间的秒数
//如何将年月日时分秒的数据转化为秒呢,有相关接口//如下两接口将struct tm转换为1970-1-1 00:00:00到指定时间秒数time_t timelocal(struct tm *tm);        //转换为local时区的秒数time_t timegm(struct tm *tm);        //转换为UTC时区的秒数//如下接口将time_t转换为tmchar *asctime(const struct tm *tm);//tm解析为字符串,如"Wed Jun 30 21:49:08 2022\n"char *asctime_r(const struct tm *tm, char *buf);char *ctime(const time_t *timep);//转换成字符串形态返回。已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 :08 2022\n”char *ctime_r(const time_t *timep, char *buf);//如下接口将秒转换为tm时间(UTC时间)struct tm *gmtime(const time_t *timep);  //time_t表示的秒转换为tm时间-utcstruct tm *gmtime_r(const time_t *timep, struct tm *result);
//如下接口将秒转换为tm时间(本地时间)struct tm *localtime(const time_t *timep);//time_t表示的秒转换为tm时间-local
//如下接口将struct tm时间转换为秒数time_t mktime(struct tm *tm);//struct tm表示的时间,转换为1970-1-1 00:00:00到时间的秒

实例代码

#include <time.h>
#include <stdio.h>
#include <stdlib.h>int main()
{struct timespec tstmtime,tsMonTime;struct tm *pCurTime;time_t ttSec, ttSecMonCnt, timet;long lmSecCnt;int nRet = 0;//获取当前时间nRet = clock_gettime(CLOCK_REALTIME, &tstmtime);ttSec =  tstmtime.tv_sec;lmSecCnt =  tstmtime.tv_sec*1000 + tstmtime.tv_nsec/1000000;printf("currtime-time-spec: second:%d, mSec:%ld \n", (int )ttSec, lmSecCnt);//获取系统启动一来的秒nRet = clock_gettime(CLOCK_MONOTONIC_COARSE, &tsMonTime);ttSecMonCnt =  tsMonTime.tv_sec;printf("monotonic second :%d \n", (int )ttSecMonCnt);//将time_t格式转为struct tm格式pCurTime = localtime(&ttSec);printf("localtime:%d-%d-%d %02d:%02d:%02d \n", pCurTime->tm_year+1900, pCurTime->tm_mon+1, pCurTime->tm_mday,pCurTime->tm_hour,pCurTime->tm_min,pCurTime->tm_sec);pCurTime = gmtime(&ttSec);printf("gmtime:%d-%d-%d %02d:%02d:%02d \n", pCurTime->tm_year+1900, pCurTime->tm_mon+1, pCurTime->tm_mday,pCurTime->tm_hour,pCurTime->tm_min,pCurTime->tm_sec);timet = mktime(pCurTime);printf("mktime-utc:get time_t:%d \n", (int )timet);pCurTime = localtime(&ttSec);timet = mktime(pCurTime);printf("mktime-location:get time_t:%d \n", (int )timet);getchar();
}

执行结果

kongcb@tcu-Lenovo:/home/clou-tcu/kongchengbo/test-code/time$ gcc time-test.c -o timetest
kongcb@tcu-Lenovo:/home/clou-tcu/kongchengbo/test-code/time$ ./timetest
currtime-time-spec: second:1652783615, mSec:-778793352
monotonic second :443800
localtime:2022-5-17 18:33:35
gmtime:2022-5-17 10:33:35
mktime-utc:get time_t:1652754815
mktime-location:get time_t:1652783615
如上可知:
1:CLOCK_MONOTONIC获取启动依赖的秒数
2:本地时间(我们是北京时间)和UTC时间差8小时
3:不同接口操作location time和UTC time

linux时钟操作接口相关推荐

  1. Linux音频驱动之五:UDA1341芯片操作接口

    本文是基于mini2440开发板Linux版本号是linux-2.6.32.2的学习笔记 一. uda1341的硬件信息 CPU通过L3接口控制uda1341芯片,分别是: L3DATA: 数据线 L ...

  2. Linux信号 五 信号挂起与信号掩码操作接口集

    A signal may be blocked, which means that it will not be delivered until it is later unblocked. Betw ...

  3. 嵌入式linux开发,对pcf8563时钟操作报错:rtc-pcf8563 0-0051: low voltage detected, date/time is not reliable.

    一.问题描述. 嵌入式linux开发,对pcf8563时钟操作报错如下: [root@ms ~]# hwclock -s rtc-pcf8563 0-0051: low voltage detecte ...

  4. Linux时钟校对(一):时间和时区的操作

    时间和时区的操作 1.概述 2.两种时钟 2.1.硬件时钟 2.2.系统时钟 2.3 硬件时钟与系统时钟间的同步 3.修改时区 3.1 检查当前时区 3.2 更改时区 4.总结 1.概述 大家好,我是 ...

  5. 汇总:Linux下文件操作接口

    写在前面: 文件描述符: 对于内核而言,所有打开的文件都通过文件描述符引用.文件描述符是一个非负整数.当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符.当读或写一个文件时,使用ope ...

  6. Linux 时钟精度 与 PostgreSQL auto_explain (explain timing 时钟开销估算)

    标签 PostgreSQL , auto_explain , pg_test_timing , 时钟 , tsc , hpet , acpi , acpi_pm , Linux 背景 我们在诊断SQL ...

  7. 如何提高linux的时钟精度,Linux时钟精度提高有什么办法?

    2 动态高精度时钟设计和实现 动态高精度时钟设计方案借鉴了KURT-Linux思想,但与其不同的是提供一个与标准Linux核心时钟并行的具有精密刻度的实时时钟,并与原核心时钟区别开.采用X86体系CP ...

  8. Linux时钟管理clk

    硬件资源越来越庞大和复杂,内核的另一个挑战就是要便捷的管理这些资源.同时,面对如此之多的平台不同的CPU,管理机制需要统一适用,这就需要对资源的管理抽象到更加通用的层次.CPU中各个模块都需要时钟驱动 ...

  9. Linux 时钟管理

    Linux 时钟管理 时钟管理是操作系统不可或缺的一个重要组成部分.无论是应用程序还是内核本身都在大量使用各种各样的timer.本文介绍了Linux操作系统曾经使用和现在正在使用的时钟管理机制,着重阐 ...

最新文章

  1. linux 文件操作 编程,Linux系统编程------------文件操作(基础)
  2. vue父组件传值给字组件
  3. 剑指Offer_17_树的子结构
  4. MySQL读写分离一主多从实现
  5. linux下安装nginx,php,mysql
  6. 虚拟现实技术利用计算机,虚拟现实技术在计算机专业教学中的应用思考
  7. laravel 先排序后分组怎么写_希尔排序算法拆解解析
  8. 用AJAX编写一个简单的相册
  9. egg连接oracle,egg插件sequelize:表自连接
  10. 编写高质量javascript代码的基本要点
  11. linux mysql 消失_Linux下修改mysql的root密码后数据库消失怎么处理
  12. 安卓bin固件文件编辑器_摄像头恶意固件制作指南
  13. 新版php卡盟排行榜网站源码
  14. 【eNSP 华为模拟器】了解一下交换机原理和基础命令
  15. Pyinstaller 打包exe运行时找不到源码,函数 错误 OSError: could not get source code
  16. 《东周列国志》第二十九回 晋惠公大诛群臣 管夷吾病榻论相
  17. 举例说明 频分多址FDMA、时分多址TDMA、码分多址CDMA、空分多址SDMA的异同
  18. 008 怎么取消隐藏文件扩展名
  19. python分析出nba球员的位置_虎扑热帖|Python数据分析|NBA的球星们喜欢在哪个位置出手...
  20. C#使用 MailKit获取邮件中的附件(QQ邮箱/163网易邮箱)

热门文章

  1. Improving Sample Efficiency In Model-Free Reinforcement Learning From Images 论文翻译
  2. 3C认证所需要的条件和流程
  3. 从申请文件获受理到挂牌交易 科创板首批25家公司最快98天完成上市
  4. 科创板|微芯生物8月12日上市 将成科创板第28家上市公司
  5. Macbook pro开盖唤醒时连不上wifi解决方案
  6. npm version使用方法
  7. 基于JavaWEB+MYSQL的美食网站设计与实现
  8. 51单片机 独立键盘模块
  9. 平面设计师学python_设计师学习编程意义大吗?
  10. 计算机硬件拆解,拆解看硬件_台电一体电脑_主板评测-中关村在线