嵌入式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. [状压dp]经典TSP

    0出发 每个顶点经过一次 回到0 最小花费. O($n^2 \times 2^n$) 记忆化搜索: // s: 已经访问过的节点状态 v: 出发位置 int dfs(int s, int v) { ) ...

  2. *[topcoder]LittleElephantAndIntervalsDiv1

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

  3. *[topcoder]LittleElephantAndString

    http://community.topcoder.com/stat?c=problem_statement&pm=12854&rd=15709 这道题DIV1 250的,还有点意思. ...

  4. plsql exist和in 的区别

    <![endif]--> <![endif]--> 发现公司同事很喜欢用exists 和in 做子查询关联,我觉得很有必要研究下 两者的区别,供参考和备忘 /* (这段信息来自 ...

  5. OTG

    OTG技术就是在没有Host的情况下,实现设备间的数据传送.例如数码相机直接连接到打印机上,通过OTG技术,连接两台设备间的USB口,将拍出的相片立即打印出来:也可以将数码照相机中的数据,通过OTG发 ...

  6. 【HDOJ】1669 Jamie's Contact Groups

    二分+二分图多重匹配. /* 1669 */ #include <iostream> #include <string> #include <map> #inclu ...

  7. 应付分配集 Distribution Sets

    (N) AP > Setup > Invoice > Distribution Sets (定义分配集) You can use a Distribution Set to auto ...

  8. poi大数据导入解决方法

    This one comes up quite a lot, but often the reason isn't what you might initially think. So, the fi ...

  9. 定制属于自己的自动化安装的linux系统镜像

    使用软件和平台 1.基于平台:                  Vmware workstation 8.0 2.基于系统镜像:               rhel-server-5.8-i386 ...

  10. WeiXinMPSDK-微信C# SDK

    微信C# SDK 微信公众号:Senparc.Weixin.MP.dll 微信企业号:Senparc.Weixin.QY.dl 微信开放平台:Senparc.Weixin.Open.dll 本库为.N ...