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
随机推荐
- RTMP
实时消息传输协议 RTMP(Real Time Messaging Protocol) http://blog.csdn.net/defonds/article/details/17403225 译序 ...
- ANDROID_MARS学习笔记_S01原始版_003_对话框
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...
- new[]上面居然有一个内存计数,怪不得delete[]从来不出错
开眼界了,留个爪,以后再仔细看几遍: http://www.cnblogs.com/hazir/p/new_and_delete.html
- Android:WebView深入使用
webView = (WebView) findViewById(R.id.info_detail_webview); WebSettings webSettings = webView.getSet ...
- linux下跨服务器文件文件夹的复制
文件的复制:scp –P (端口号) ./authorized_keys berchina@hadoop002:/home/berchina 文件夹的复制:scp -r -P (端口号) /home/ ...
- Oracle系列之索引
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 Oracle索引.权限 介绍 为什么添加了索引后,会加快查询速度呢? 索引是用于加速数据存取的数据对象.合理的使用索引可以大大降低i/ ...
- -fembed-bitcode is not supported on versions of iOS prior to 6.0 问题修复
参考一下网址 http://stackoverflow.com/questions/30867544/fembed-bitcode-is-not-supported-on-versions-of-io ...
- UVA_11468_Substring_(AC自动机+概率动态规划)
描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- maven常用技巧
安装Maven后我们会在用户目录下发现.m2 文件夹.默认情况下,该文件夹下放置了Maven本地仓库.m2/repository.所有的Maven构件(artifact)都被存储到该仓库中,以方便重用 ...
- CI 在nginx中出现404错误的解决方式
因为你的nginx配置的是截取.php文件后缀的访问转发到PHP-CGI,而index.php和index.php/是不一样的.. 你在nginx里面写一句: if (!-e $request_fil ...