Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of writing time into RTC clock in Linux application layer system

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the method of writing Linux application layer system time into RTC clock". In daily operation, I believe that many people have doubts about the method of writing Linux application layer system time into RTC clock. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Linux application layer system time writing RTC clock method". Next, please follow the editor to study!

First, write time

1. Preliminary knowledge:

A 、 mktime

Header file: # include

Function: time_t mktime (struct tm * timeptr)

Function description: mktime () is used to convert the tm structure data referred to by timeptr into the number of seconds that have elapsed since 00:00:00 on January 1, 1970.

Return value: returns the number of seconds elapsed. Return-1 when an error occurs.

B 、 settimeofday

Header file: # include

# include

Function: int settimeofday (const struct timeval * tv,const struct timezone * tz)

Function description: settimeofday () sets the current time to the structure information referred to by tv, and the local time zone information to the structure referred to by tz.

Return value: only root permission can use this function to modify the time. Success returns 0, failure returns-1, and the error code is stored in errno.

2. Practice:

Writing time can be completed by using mktime and settimeofday together.

3. The code is as follows:

# include # include struct my_timeval {_ _ time_t tv_sec; _ _ suseconds_t tv_usec;} / * function name: System_SetTime* function: write system time * usage: char* dt = "2016-04-15 21:00:00"; System_SetTime (dt) * * / int System_SetTime (char* dt) {struct rtc_time tm;struct tm _ tm;struct my_timeval tv; time_t timep Sscanf (dt, "% d-%d-%d% d:%d:%d", & tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec); _ tm.tm_sec = tm.tm_sec; _ tm.tm_min = tm.tm_min; _ tm.tm_hour = tm.tm_hour; _ tm.tm_mday = tm.tm_mday _ tm.tm_mon = tm.tm_mon-1; _ tm.tm_year = tm.tm_year-1900; timep = mktime (& _ tm); tv.tv_sec = timep; tv.tv_usec = 0bot if (settimeofday (& tv, (struct timezone *) 0)

< 0) {printf("Set system datetime error!\n");return -1; } return 0;}void main(void){char *dt = "2016-4-15 21:00:00"; System_SetTime(dt);} 4、测试结果:

Second, preservation time

As can be seen from the above test results, the system time can be written normally. At first I thought it would be all right, but I found that it wouldn't work. Because once I restart the development board, the system time will return to the original time. Come to think of it, we only wrote the system time and did not synchronize the system time to the hardware time, so that the hardware time for each restart of the system does not change. The system time CST = UTC + 8 obtained after startup is still the system time obtained. So how to synchronize the system time we set to hardware time? We know that in the terminal, the system time can be synchronized to the hardware time through hwclock-systohc. How can it be realized in the application layer? I don't know if there is any other good solution, but the solution I came up with is to create a child process in the application layer and call the script file in the child process. The instruction in the script is hwclock-systohc. This completes the synchronization. Of course, if there is a simpler and more appropriate way, you are welcome to guide and communicate. Having said so much, let's get back to business.

1. Preliminary knowledge:

A. Fork creates a child process with the following code:

/ * * function: create child process fork () test * time: 2016-4-15 * author: Jack Cui**/#include # include int main (void) {pid_t fpid; / / fpid represents the value returned by the fork function int count=0; fpid=fork () If (fpid

< 0) //创建子进程失败printf("error\n"); else if (fpid == 0) { printf("I am the child process,my process id is %d\n",getpid()); count++; }else { printf("I am the parent process,my process id is %d\n",getpid()); count++; } printf("count = %d\n",count);return 0;} b、fork测试程序结果显示:

C, execve () application layer call script file:

Header file: # include

Function: int execve (const char * filename, char * const argv [], char * const envp [])

Function description: execve () is used to execute the file path represented by the parameter filename string, the second parameter is passed to the execution file using the array pointer, and the last parameter is the new array of environment variables passed to the execution file.

Return value: if the execution is successful, the function will not return, if the execution fails, it will directly return-1. The reason for the failure is stored in errno.

D, execve () test code:

/ * * function: test execve* time: 2016-4-15 * author: Jack Cui**/#include / / perror#include / / EXIT_SUCCESS EXIT_FAILURE#include / / execvevoid main (void) {char * args [] = {"/ home/nfsroot/hwclock.sh" NULL} If (- 1 = (execve ("/ home/nfsroot/hwclock.sh", args,NULL)) {perror ("execve"); exit (EXIT_FAILURE);} exit (EXIT_SUCCESS);}

E. Content of the script:

F, execve test results:

You can see that execve is working normally, and we can synchronize the system time to hardware time by changing the content of the script to hwclock-systohc.

3. The overall code is as follows:

/ * function: Linux application layer system time writing method of RTC clock * time: 2016-4-15 * author: Jack Cui** * / # include # include struct my_timeval {_ _ time_t tv_sec _ _ suseconds_t tv_usec;}; / * function name: System_SetTime* function: write system time * usage: char* dt = "2016-04-15 21:00:00" System_SetTime (dt); * * / int System_SetTime (char* dt) {struct rtc_time tm;struct tm _ tm;struct my_timeval tv; time_t timep Sscanf (dt, "% d-%d-%d% d:%d:%d", & tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec); _ tm.tm_sec = tm.tm_sec; _ tm.tm_min = tm.tm_min; _ tm.tm_hour = tm.tm_hour; _ tm.tm_mday = tm.tm_mday _ tm.tm_mon = tm.tm_mon-1; _ tm.tm_year = tm.tm_year-1900; timep = mktime (& _ tm); tv.tv_sec = timep; tv.tv_usec = 0 settimeofday if (& tv, (struct timezone *) 0) < 0) {printf ("Set system datetime error!\ n"); return-1;} return 0 } void main (void) {char * dt = "2016-4-15 21:00:00"; pid_t fpid; / / fpid represents the value returned by the fork function fpid=fork (); if (fpid < 0) / / create child process failed printf ("error\ n"); else if (fpid= = 0) {char * args [] = {"/ home/nfsroot/hwclock.sh", NULL} If (- 1 = (execve ("/ home/nfsroot/hwclock.sh", args,NULL)) {perror ("execve"); exit (EXIT_FAILURE);} exit (EXIT_SUCCESS);} else {System_SetTime (dt);} return 0;}

4. The final results show that:

1. Content of the script:

2. Test results:

In this way, we restart the development board, the system time will not change, the setting is successful ~!

At this point, the study on "the method of writing Linux application layer system time to RTC clock" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report