Linux 设置系统时间和日期 API
嵌入式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的更多相关文章
- date 显示或设置系统时间和日期
显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...
- linux设置系统时间
设置系统时间 - date命令:显示系统的时间,可以在直接输入"date"命令来查看系统的时间 - date+%y/%m/%d - ...
- linux设置系统时间和时区
1.设置系统时间 date命令将日期设置为2014年6月18日 ---- date -s 06/18/14 将时间设置为14点20分50秒 ---- date -s 14:20:50 将时间设 ...
- linux设置系统时间与时区以及设置bios时间同步系统时间
有装过Linux系统的人,可能都会有这样的经历,就是该机器安装windows系统时,时间正确,但是安装了linux系统后,尽管时区选择正确,也会发现系统时间不对.这是由于安装系统时采用了UTC,那么什 ...
- date---显示或设置系统时间与日期
date命令可以用来显示或设定系统的日期与时间,格式设定为一个加号后接数个标记,其中可用的标记列表如下: 时间方面: %H : 小时(00..23) %M : 分钟(00..59) %p : 显示本地 ...
- linux设置系统时间与各种阻塞
前阵子做了一个P2P的通信系统,发现开机的时候和中间运行的时候会莫名报错,这个问题找了好久,后来从日志中看出来,所有节点上阻塞的操作同时超时. 而在超时左右,有新节点自动加入系统. 在新节点加入系统的 ...
- linux 设置系统时间
第一种: 服务器date时间不准: root@mdy-zabbix2:~# date Fri Sep 28 09:58:56 UTC 2018 实际是下午6点 第一步:执行tzselect 第二步: ...
- Linux 设置系统时间和时区2.Ubuntu
查看当前时间状态 timedatectl status 设置时区 sudo dpkg-reconfigure tzdata Asia shanghai
- Linux 设置系统时间和时区1.Centos
随机推荐
- crtmpserver系列之一:流媒体概述
阅读目录 概述 流媒体系统的组成 媒体文件封装 传输协议 回到顶部 概述 所谓流媒体按照字面意思理解就是像流一样的媒体,看起来像是废话.流媒体现在司空见惯,所以一般人大概不会有疑问.事实上在流媒体还没 ...
- 如何配置Java环境
下载JDK并安装 搜索JDK,官网立马就出来了,下载之后个人觉得毕竟开发,毕竟这东西不大,C盘稳一点,安装在C盘可以的 配置 右键打开计算机->属性->高级系统设置->高级-> ...
- windows权限错误
1.installer "内部错误 2203 C:\WINDOWS\Installer\354787.ipi,-2147287035” 用户的local\Temp目录没有system权限 2 ...
- iOS开发中自定义字体的方法
http://www.cnblogs.com/iyou/archive/2014/05/25/3751669.html 1. 首先下载你想要设置的字体库,例如设置方正启体简体 2. 添加到工程,一定要 ...
- WCF大数据量传输解决方案
文章内容列表:1. 场景:2. 解决方案3. WCF契约与服务实现设计静态图4. WCF契约与服务实现设计详细说明6. 服务端启动服务代码:7. 客户端代码8. WCF大数据量传输解决方案源码下载 ...
- BrnShop开源网上商城第四讲:自定义插件
重要通知:BrnShop企业版NOSQL设计(基于Redis)已经开源!源码内置于最新版的BrnShop中,感兴趣的园友可以去下载来看看.官网地址:www.brnshop.com. 好了现在进入今天的 ...
- HDU3047 Zjnu Stadium 带权并查集
转:http://blog.csdn.net/shuangde800/article/details/7983965 #include <cstdio> #include <cstr ...
- [Tommas] dateadd() 函数用法
DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的间隔数:对于 ...
- FIRST集和FOLLOW集
省略号代表其他相关产生式得出的终结符号,一开始的时候,省略号里面是没有的 求FIRST集 情况壹 如果A只在→的右边出现,那么FIRST(A)={A},例子M→α,FIRST(α)={α} 情况 ...
- 关于sharepoint事件接收器中properties.AfterProperties[""].Tostring()取值的问题。
这个这个属性是不能获取到中文的意思,他是获取AfterProperties的集合的值. string name=properties.AfterProperties["登录人"]. ...