时间由rtc硬件模块来进行维护的,时间更新后会将时间信息写入此硬件模块,在系统启动时,RTC硬件驱动会读取此值进行设置。
关于android的时间
网上资料了解到,android Linux都有两个时间,一个是系统时间,一个是硬件时间(RTC时间)。RTC是实时时钟的英文 缩写,基本上在硬件上都有纽扣电池,不会断电,会一直计时;但系统时钟不一样,系统运行的时候能一直计时,但系统关机或者断电的时候就gg了。所以每次系统开机的时候,会去读取rtc中的硬件时间,写入到系统时间,然后系统时间就自己搞起来了,不再关rtc时间什么事情了。等系统关机或者什么时候,会把时间写入到rtc,这样才能保证下次开机你这个系统的时间不会错乱。
/kernel/msm-4.9/drivers/rtc/systohc.c
int rtc_set_ntp_time(struct timespec64 now)
{
struct rtc_device *rtc;
struct rtc_time tm;
int err = -ENODEV;
if (now.tv_nsec < (NSEC_PER_SEC >> 1))
rtc_time64_to_tm(now.tv_sec, &tm);
else
rtc_time64_to_tm(now.tv_sec + 1, &tm);
rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE);
if (rtc) {
/* rtc_hctosys exclusively uses UTC, so we call set_time here,
* not set_mmss. */
if (rtc->ops &&
(rtc->ops->set_time ||
rtc->ops->set_mmss64 ||
rtc->ops->set_mmss))
err = rtc_set_time(rtc, &tm);
rtc_class_close(rtc);
}
return err;
}
frameworks/base/services/core/java/com/android/server/AlarmManagerService.java
// We have to set current TimeZone info to kernel
// because kernel doesn't keep this after reboot
setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY));
// Also sure that we're booting with a halfway sensible current time
if (mNativeData != 0) {
final long systemBuildTime = Environment.getRootDirectory().lastModified();
if (System.currentTimeMillis() < systemBuildTime) {
Slog.i(TAG, "Current time only " + System.currentTimeMillis()
+ ", advancing to build time " + systemBuildTime);
setKernelTime(mNativeData, systemBuildTime);
}
}
system会取当前时间与system分区文件夹的时间进行比较。 如果时间小于文件夹时间,那么就会改成文件夹时间
只要设置过一次时间,不会出现问题
时间戳
https://tool.lu/timestamp/
系统时间
date
RTC时间
hwclock -r
通过(@xx,xx以s为单位)时间戳转换为时间。
date -d @1600335717
产品编译信息信息:
adb shell getprop | grep date
获取:"ro.build.date.utc"的值
查看system分区时间:
adb shell -> ls -al
如:drwxr-xr-x 1 root root 3488 2020-09-17 17:41 system
获取时间
# date
Thu Jun 4 22:53:13 PDT 2015
# date +%s
1433483623
# date +%D
06/04/15
# date "+%d %B %Y"
04 June 2015
设置时间
#date //设置时间前
Thu Jun 4 23:09:05 PDT 2015
# date -s "21 June 2016 11:01:20" //设置时间
Tue Jun 21 11:01:20 PDT 2016
# date //设置时间后
Tue Jun 21 11:01:22 PDT 2016
设置完成后,使用clock -w,不然重启后时间又回到以前的值了
$ date +%s --date 19700101 --utc // 1970-01-01 00:00 UTC
0
$ date +%s --date 20120101 --utc // 2012-01-01 00:00 UTC
1325376000
$ date +%s --date 20160101 --utc // 2016-01-01 00:00 UTC
1451606400