嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码。

Linux设置时间和日期的步骤:

1. 设置系统时间和日期;

2. 该系统的时间和日期,同步到硬件。

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <time.h>
#include <linux/rtc.h>
#include <linux/capability.h> int SetSysDateAndTime(const char *time_str);
void SetHWClockFromSysClock(int utc); static int rtc_xopen(const char **default_rtc, int flags);
static void write_rtc(time_t t, int utc); static const char *rtcname; int main(void)
{
const char time_str[] = "1989-11-22 11:22:33";
SetSysDateAndTime(time_str);
SetHWClockFromSysClock(0); system("reboot"); return 0;
} int SetSysDateAndTime(const char *time_str)
{
struct tm time_tm;
struct timeval time_tv;
time_t timep;
int ret; if(time_str == NULL)
{
fprintf(stderr, "time string args invalid!\n");
return -1;
} sscanf(time_str, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);
time_tm.tm_year -= 1900;
time_tm.tm_mon -= 1;
time_tm.tm_wday = 0;
time_tm.tm_yday = 0;
time_tm.tm_isdst = 0; timep = mktime(&time_tm);
time_tv.tv_sec = timep;
time_tv.tv_usec = 0; ret = settimeofday(&time_tv, NULL);
if(ret != 0)
{
fprintf(stderr, "settimeofday failed\n");
return -2;
} return 0;
} void SetHWClockFromSysClock(int utc)
{
struct timeval tv; gettimeofday(&tv, NULL);
//if (gettimeofday(&tv, NULL))
// bb_perror_msg_and_die("gettimeofday() failed");
write_rtc(tv.tv_sec, utc);
} static int rtc_xopen(const char **default_rtc, int flags)
{
int rtc; if (!*default_rtc) {
*default_rtc = "/dev/rtc";
rtc = open(*default_rtc, flags);
if (rtc >= 0)
return rtc;
*default_rtc = "/dev/rtc0";
rtc = open(*default_rtc, flags);
if (rtc >= 0)
return rtc;
*default_rtc = "/dev/misc/rtc";
} return open(*default_rtc, flags);
} static void write_rtc(time_t t, int utc)
{
#define RTC_SET_TIME _IOW('p', 0x0a, struct rtc_time) /* Set RTC time */ struct tm tm;
int rtc = rtc_xopen(&rtcname, O_WRONLY); tm = *(utc ? gmtime(&t) : localtime(&t));
tm.tm_isdst = 0; ioctl(rtc, RTC_SET_TIME, &tm); close(rtc);
}

Linux 设置系统时间和日期 API的更多相关文章

  1. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  2. linux设置系统时间

    设置系统时间 -         date命令:显示系统的时间,可以在直接输入"date"命令来查看系统的时间 -           date+%y/%m/%d -        ...

  3. linux设置系统时间和时区

    1.设置系统时间 date命令将日期设置为2014年6月18日 ----   date -s 06/18/14 将时间设置为14点20分50秒 ----   date -s 14:20:50 将时间设 ...

  4. linux设置系统时间与时区以及设置bios时间同步系统时间

    有装过Linux系统的人,可能都会有这样的经历,就是该机器安装windows系统时,时间正确,但是安装了linux系统后,尽管时区选择正确,也会发现系统时间不对.这是由于安装系统时采用了UTC,那么什 ...

  5. date---显示或设置系统时间与日期

    date命令可以用来显示或设定系统的日期与时间,格式设定为一个加号后接数个标记,其中可用的标记列表如下: 时间方面: %H : 小时(00..23) %M : 分钟(00..59) %p : 显示本地 ...

  6. linux设置系统时间与各种阻塞

    前阵子做了一个P2P的通信系统,发现开机的时候和中间运行的时候会莫名报错,这个问题找了好久,后来从日志中看出来,所有节点上阻塞的操作同时超时. 而在超时左右,有新节点自动加入系统. 在新节点加入系统的 ...

  7. linux 设置系统时间

    第一种: 服务器date时间不准: root@mdy-zabbix2:~# date Fri Sep 28 09:58:56 UTC 2018 实际是下午6点 第一步:执行tzselect 第二步: ...

  8. Linux 设置系统时间和时区2.Ubuntu

    查看当前时间状态 timedatectl status 设置时区 sudo dpkg-reconfigure tzdata Asia shanghai

  9. Linux 设置系统时间和时区1.Centos

随机推荐

  1. code forces Watermelon

    /* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...

  2. *[topcoder]LittleElephantAndIntervalsDiv1

    http://community.topcoder.com/stat?c=problem_statement&pm=12822&rd=15707 第一次用C++提交,艰辛.首先想到可以 ...

  3. Spring AOP实现方式三【附源码】

    注解AOP实现 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.aop; /** * 谈恋爱接口 * * @author Administrator ...

  4. C# SerialPort的简单使用

    SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现.由于线程实时读串口的效率不是十分高效 ...

  5. Case swapping

    Case swapping Description: Given a string, swap the case for each of the letters. e.g. CodEwArs --&g ...

  6. hdu4671Backup Plan

    http://acm.hdu.edu.cn/showproblem.php?pid=4671 这个高端的题意啊 看了N久啊 n>m时  直接第一列按顺序来 第二列为M+1 else  第一列顺序 ...

  7. bzoj1443

    首先要思考的问题肯定是,什么点可以是ans, 不难想到对图黑白染色,假如一个棋子不管怎么走,都只能走到和它同色的点上时,这就是一个ans 再考虑,每次棋子的移动都是黑白相间的 再考虑,黑白染色是可以构 ...

  8. c#基础这些你都看过吗?(一)-----仅供初学者使用

    1.注释(不写注释是流氓,名字瞎起是扯淡)‘///’一般用于注释函数,注释类.2.快捷键ctrl+k+d(有语法错误无法进行对齐)ctrl+j(快速弹出只能提示)shift+end,shift+hom ...

  9. BZOJ_1600_[Usaco2008_Oct]_建造栅栏_(动态规划)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1600 将长度为n的线段分成4段,长度为整数,围成面积>0的四边形,求方案数. 分析 首先 ...

  10. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.1

    Given any $k$-tupel of linearly independent vectors $X$ as above, there exists a $k$-tuple $Y$ biort ...