本文主要探讨在linux系统中处理时间的相关函数和操作,包括系统时间设置、rtc时间设置、时间单位转换、延时函数以及闹钟信号等。
在Linux系统中存在两种时间类型:系统时间和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时间读取出来设置给系统时间)
通过代码也可以实现RTC时间的设置:
#include <stdio.h>
#include <sys>
#include <sys>
#include <fcntl.h>
#include <sys>
#include <linux>
/*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</linux></sys></fcntl.h></sys></sys></stdio.h>接下来介绍与时间处理相关的函数,这些函数主要位于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 来表示)。
</time.h>
下面是时间获取与转换的示例代码:
#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;
}</time.h></stdlib.h></stdio.h>常用的延时函数包括:
#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秒钟还没有到达,它可以被其他信号终止.</time.h></unistd.h>以下是延时函数的示例代码:
#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;
}</unistd.h></time.h></stdlib.h></stdio.h>最后,介绍系统定时器信号中的闹钟信号函数:
#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 捕获信号之后调用的处理函数</signal.h></unistd.h>
以下是闹钟信号的示例代码:
例子代码:
#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;
}</signal.h></unistd.h></time.h></stdlib.h></stdio.h>运行效果如下:
[wbyq@wbyq linux_c]$ gcc app.c [wbyq@wbyq linux_c]$ ./a.out 闹钟时间到达. 闹钟时间到达. 闹钟时间到达. 闹钟时间到达. 闹钟时间到达.
以上就是介绍Linux下时间处理的相关操作(RTC、延时、闹钟、转换)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号