ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

介绍Linux下时间处理的相关操作(RTC、延时、闹钟、转换)

2021-11-23 21:02:15  阅读:241  来源: 互联网

标签:RTC int Linux tm 时间 闹钟 time include


一、系统时间设置

这篇文章主要介绍Linux下时间处理的相关函数与操作。
比如: 系统时间设置,读取、RTC时间设置,读取、时间单位转换、延时函数、闹钟信号等等。

Linux下存在两种时间: 1. 系统时间,2. RTC时间

系统时间是每次操作系统启动之后,从RTC驱动里读取进行设置的,一般只会在系统上电启动的时候自动(启动脚本)同步一次,后续用户也可以通过特定的命令再次同步;在系统界面上看到的时间就是系统时间;系统时间每次系统关机之后就会丢失,需要每次上电从RTC驱动里获取。

系统时间设置的方法如下:需要有管理员权限

[wbyq@wbyq linux_c]$ date -s "2020-10-12 9:28:20"
date: 无法设置日期: 不允许的操作
2020年 10月 12日 星期一 09:28:20 CST
[wbyq@wbyq linux_c]$ sudo date -s "2020-10-12 9:28:20"
[sudo] password for wbyq: 
2020年 10月 12日 星期一 09:28:20 CST
[wbyq@wbyq linux_c]$ 

RTC时间掉电不会停止运行,电源是后备电源单独供给的;可以一直运行,方便给系统提供准确的时间。

RTC时间读取与设置方法:需要有管理员权限

hwclock -r   显示RTC时间 (读取RTC时间显示)
hwclock -w   设置RTC时间 (将系统时间传递给RTC驱动,设置RTC的驱动时间)
hwclock -s   设置系统时间(将RTC时间读取出来设置给系统时间)

也可以通过代码实现:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/rtc.h>

/*
RTC_SET_TIME
RTC_RD_TIME
*/

struct rtc_time time;
int main(int argc,char **argv)
{
    if(argc!=2)
    {
        printf("./app /dev/rtcX\n");
        return 0;
    }
    //1.打开设备文件
    int fd=open(argv[1],2);
    if(fd<2)
    {
        printf("%s 设备文件打开失败.\n",argv[1]);
        return 0;
    }
    
    //2.获取RTC驱动的时间
    ioctl(fd,RTC_RD_TIME,&time);
    printf("应用层读取的时间: %d-%d-%d %d:%d:%d\n",
            time.tm_year+1900,
            time.tm_mon+1,
            time.tm_mday,
            time.tm_hour,
            time.tm_min,
            time.tm_sec);
    
    //3.设置RTC驱动的时间
    time.tm_year=2021-1900;
    time.tm_mon=10-1;
    time.tm_mday=1;
    time.tm_hour=11;
    time.tm_min=10;
    time.tm_sec=20;
    ioctl(fd,RTC_SET_TIME,&time);
    
    //4. 关闭驱动
    close(fd);
    return 0;
}

二、时间处理相关函数介绍(time.h)

#include <time.h>
struct tm {
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};

char *asctime(const struct tm *tm); //内部有一个全局空间存放转换的时间
char *asctime_r(const struct tm *tm, char *buf); //用户可以指定自己的空间
函数功能: 将tm时间结构体里的时间转为字符串格式返回(指针返回).

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
函数功能: 将秒单位的时间转为字符串格式返回.

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
函数功能: 将秒单位的时间转为格林威治时间返回---使用tm结构体。

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
函数功能: 将秒单位的时间转为本地时间返回.---使用tm结构体

time_t mktime(struct tm *tm);
函数功能: 将tm结构体时间转为秒单位返回.

time_t time(time_t *t);
函数功能:如果形参填NULL就表示获取当期系统的秒单位时间.

size_t strftime(char *s, size_t max, const char *format,
                       const struct tm *tm);
函数功能:  将tm结构体的时间按照指定的格式转成字符串返回.
const char *format 格式有以下格式可以填:
%H 小时(以 00-23 来表示)
%M 分钟(以 00-59 来表示)
%S 秒(以本地的惯用法来表示)
%Y 年份(以四位数来表示)
%m 月份(以 01-12 来表示)
%d 日期(以 01-31 来表示)。

image.png

时间获取与转换示例:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc,char **argv)
{
    /*1.获取本地的秒单位时间*/
    time_t sec_time=time(NULL);
    printf("当前系统的总秒数:%d\n",sec_time);
    /*2. 将秒单位时间转为字符串返回*/
    char time_buff[100];
    ctime_r(&sec_time,time_buff);
    printf("字符串格式时间(系统默认):%s\n",time_buff);
    /*3. 将秒单位时间转为tm结构体返回*/
    struct tm tm_time;
    gmtime_r(&sec_time,&tm_time);
    printf("国际时间: %d-%d-%d %d:%d:%d\n",tm_time.tm_year+1900,
                                 tm_time.tm_mon+1,
                                 tm_time.tm_mday,
                                 tm_time.tm_hour,
                                 tm_time.tm_min,
                                 tm_time.tm_sec);

    localtime_r(&sec_time,&tm_time);
    printf("本地时间: %d-%d-%d %d:%d:%d\n",tm_time.tm_year+1900,
                                 tm_time.tm_mon+1,
                                 tm_time.tm_mday,
                                 tm_time.tm_hour,
                                 tm_time.tm_min,
                                 tm_time.tm_sec);
    /*4. 将tm结构体时间转为秒单位返回.*/
    printf("总秒数:%d\n",mktime(&tm_time));

    /*5. 将tm结构体时间格式按照指定格式转为字符串*/
    strftime(time_buff,sizeof(time_buff),"%Y_%m_%d_%H_%M_%S.mp4",&tm_time);
    printf("time_buff=%s\n",time_buff);

    return 0;
}

三、常用的一些延时函数

#include <unistd.h>
unsigned int sleep(unsigned int seconds);
函数功能: 秒单位的延时函数.

int usleep(useconds_t usec);
函数功能: 微秒单位的延时函数.

#include <time.h>
int nanosleep(const struct timespec *req, struct timespec *rem);
函数功能: 秒+纳秒的延时函数.

struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds */
};

以上的函数都是可中断的延时函数。 
比如: 延时10秒,有可能10秒钟还没有到达,它可以被其他信号终止.

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int main(int argc,char **argv)
{
    printf("1234\n");
    //sleep(5);
    //usleep(1000*1000);

    struct timespec req={5,1000}; //将要延时的时间
    struct timespec rem; //保存是延时结束剩余的时间
    nanosleep(&req,&rem);

    printf("5678\n");
    return 0;
}

四、系统定时器信号: 闹钟信号

函数原型介绍:

#include <unistd.h>
unsigned int alarm(unsigned int seconds);
闹钟超时之后会产生SIGALRM闹钟信号。

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
函数功能: 捕获进程收到的信号.
函数参数:
int signum 要捕获的信号
sighandler_t handler  捕获信号之后调用的处理函数

示例代码:

例子代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

void sighandler_func(int sig)
{
    printf("闹钟时间到达.\n");
    //定义一个闹钟
    alarm(1); //重复定时
}

int main(int argc,char **argv)
{
    //声明要捕获的信号
    signal(SIGALRM,sighandler_func);
    //定义一个闹钟
    alarm(1);
    while(1)
    {

    }
    return 0;
}

运行效果:

[wbyq@wbyq linux_c]$ gcc app.c 
[wbyq@wbyq linux_c]$ ./a.out 
闹钟时间到达.
闹钟时间到达.
闹钟时间到达.
闹钟时间到达.
闹钟时间到达.

标签:RTC,int,Linux,tm,时间,闹钟,time,include
来源: https://blog.csdn.net/xiaolong1126626497/article/details/121464320

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有