date -s “2018-04-08 12:00:00”
hwclock -r
修改完时间后,记得使用hwclock –w将系统时间同步到硬件时间。
hwclock -w 将系统时钟写入硬件时钟(hwclock -r 可以用来查看硬件时钟)
at91_rtc f80480b0.rtc: setting system clock to 2012-01-01 00:04:47 UTC (1325376287)
date -s “2017-04-08 12:00:00”
hwclock –r 显示硬件时钟与日期
hwclock –s 将系统时钟调整为与目前的硬件时钟一致。
hwclock –w 将硬件时钟调整为与目前的系统时钟一致。
RTC - real time clock维护着系统的hardware时间,当linux启动时需要用RTC hardware时钟设置system 时间。
这个过程是在drivers/rtc/hctosys.c驱动中实现的,这个驱动实际只有一个init函数,并且把自己的init 函数声明为
late_initcall,这样可以保证RTC驱动已经正常运转。
init函数从RTC设备读取当前硬件时钟,然后调用do_settimeofday改写系统时钟。
staticint __init rtc_hctosys(void)
/home/xxx/ArmProject/multi-serial/kernel/SAMA_XIN2/drivers/rtc/hctosys.c
* published bythe Free Software Foundation.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt#include
/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
* whether it stores the most close value orthe value with partial
* seconds truncated. However, itis important that we use itto store
* the truncated value. This is because otherwise itis necessary,
* in an rtc sync function, toread both xtime.tv_sec and
* xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
* of >32bits isnot possible. So storing the most close value would
* slow down the sync API. So here we have the truncated value and
* the best guess isto add 0.5s.
*/
static int __init rtc_hctosys(void)
{
int err = -ENODEV;
struct rtc_time tm;
struct timespec64 tv64 = {
.tv_nsec = NSEC_PER_SEC >> 1,
};
struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
if (rtc == NULL) {
pr_info("unable to open rtc device (%s)
",
CONFIG_RTC_HCTOSYS_DEVICE);
goto err_open;
}
err = rtc_read_time(rtc, &tm);
if (err) {
dev_err(rtc->dev.parent,
"hctosys: unable to read the hardware clock
");
goto err_read;
}
tv64.tv_sec = rtc_tm_to_time64(&tm);
err = do_settimeofday64(&tv64);
dev_info(rtc->dev.parent,
"setting system clock to ""%d-%02d-%02d %02d:%02d:%02d UTC (%lld)
",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
(long long) tv64.tv_sec);
err_read:
rtc_class_close(rtc);
err_open:
rtc_hctosys_ret = err;
return err;
}
late_initcall(rtc_hctosys);