嵌入式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. 170. Two Sum III - Data structure design

    题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...

  2. Android 各个版本WebView

    转载请注明出处   http://blog.csdn.net/typename/ powered by miechal zhao : miechalzhao@gmail.com 前言: 根据Googl ...

  3. HTML本地测试成功后上传博客注意事项

    需要注意不要跟博客已经存在的样式(CSS)或功能(JavaScript)起冲突 功能名一定不要一样 样式名尽量不一样 如果样式名一样,存在属性名的对应属性值尽量跟博客内相同

  4. mac 上iterm终端显示中文为乱码解决方案

    1.首先需要去设置下iterm,的终端编码方式为utf-8 检查方案为:iterm - > perferences - > Terminal ->Character Encoding ...

  5. bzoj1196

    带有限制的生成树 首先不难想到二分答案转化为判定性问题 假设二分出了一个答案p, 首先我们先考虑建一级公路. 由于一级公路费用是大于二级公路的,所以对于那些一级公路花费<=p的道路, 不难想到让 ...

  6. [转] Java之ACM速成

    转载自:http://blog.csdn.net/skiffloveblue/article/details/7032290 相关题目:POJ 1131 1205 1220 1405 1503 160 ...

  7. -_-#【减少 DOM 访问】缓存已经访问过的元素

    Minimize DOM Access Cache references to accessed elements 选择器查询是开销很大的方法.所以,使用选择器的次数应该越少越好,并且尽可能缓存选中的 ...

  8. 物联网操作系统HelloX V1.77(beta)版本发布

    物联网操作系统HelloX V1.77发布 经过近半年的努力,物联网操作系统HelloX V1.77版本正式完成,源代码已上载到github(github.com/hellox-project/Hel ...

  9. diamond专题(三)—— diamond架构

    大家好,这次为大家带来的是diamond的架构,架构如下图所示: 对该图进行一些说明: 1.作为一个配置中心,diamond的功能分为发布和订阅两部分.因为diamond存放的是持久数据,这些数据的变 ...

  10. 转载--C++ STL

    转自:http://wenku.baidu.com/view/15d18b4533687e21af45a9a4.html 1.C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vec ...