LINUX编程 设置系统时间

来源:互联网 发布:javascript什么意思 编辑:程序博客网 时间:2024/05/22 22:02

网上找了很多设置时间的方法,都不是很好,自己就动手写了一个。
根据输入字符串,转换为tm结构的日历结构体,再调用mktime转化为time_t .
设置后最好调用一下hwclock,写入RTC。系统是每隔11分钟才会写一次RTC,不然断电没法恢复。

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>int main( void )  {    struct tm my_tm;    char in_buffer[ 80 ] ="1993-11-30 18:40:44",         out_buffer[ 80 ];    time_t stime;    /* Convert the string to a struct tm. */    memset (&my_tm, 0, sizeof(struct tm));    strptime( in_buffer, "%F %X", &my_tm );    /* Convert the struct tm to a time_t (to fill in the     * missing fields). */    stime = mktime (&my_tm);   if( clock_settime( CLOCK_REALTIME, &stime) == -1 ) {       perror( "setclock" );       return EXIT_FAILURE;    }    return EXIT_SUCCESS;  }
0 0
原创粉丝点击