system/core
修訂 | 0e4eb2e9b47ce94b2ca721973550e9d507b895a5 (tree) |
---|---|
時間 | 2019-12-17 14:24:55 |
作者 | Ma Jian <majian@jide...> |
Commiter | Chih-Wei Huang |
Support use local time for RTC
When default timezone isn't UTC, there will no persist.sys.timezone
under /data/property/, so init won't get the default timezone for
setting time from rtc.
This change adds a fallback to read the property when the persist file
does not exists.
Notice, the default property of persist.sys.timezone should be set in
/default.prop instead of /system/build.prop
NO_REF_TASK
Tested: set default timezone with Asia/Shanghai, make sure bios time
is correct in local time, reboot to android, the local time should
be correct.
Change-Id: Ifbd20cb3710f833ab65852b4e5d51e38cc7c2d79
@@ -788,8 +788,51 @@ static Result<Success> do_rmdir(const BuiltinArguments& args) { | ||
788 | 788 | return Success(); |
789 | 789 | } |
790 | 790 | |
791 | +// Read persist property from /data/property directly, because it may not have loaded. | |
792 | +// If the file not found, try to call GetProperty to get the vaule from /default.prop. | |
793 | +static std::string persist_property_get(const std::string& name) | |
794 | +{ | |
795 | + auto result = ReadFile("/data/property/" + name); | |
796 | + return !result ? base::GetProperty(name, "") : *result; | |
797 | +} | |
798 | + | |
791 | 799 | static Result<Success> do_sysclktz(const BuiltinArguments& args) { |
792 | 800 | struct timezone tz = {}; |
801 | + | |
802 | + if (persist_property_get("persist.rtc_local_time") == "1") { | |
803 | + struct timeval tv = {}; | |
804 | + | |
805 | + if (gettimeofday(&tv, NULL)) { | |
806 | + return ErrnoError() << "gettimeofday() failed"; | |
807 | + } | |
808 | + | |
809 | + // Set system time and saved system zone in case of network | |
810 | + // not available and auto syncing time not available. | |
811 | + std::string time_zone = persist_property_get("persist.sys.timezone"); | |
812 | + if (time_zone.empty()) { | |
813 | + LOG(INFO) << "sysclktz: persist.sys.timezone not found"; | |
814 | + tz.tz_minuteswest = 0; | |
815 | + } else { | |
816 | + LOG(INFO) << "sysclktz: persist.sys.timezone: " << time_zone; | |
817 | + // localtime_r need the property, we need to set it | |
818 | + property_set("persist.sys.timezone", time_zone.c_str()); | |
819 | + time_t t = tv.tv_sec; | |
820 | + struct tm tm; | |
821 | + if (localtime_r(&t, &tm)) { | |
822 | + tz.tz_minuteswest = -(tm.tm_gmtoff / 60); | |
823 | + LOG(INFO) << "sysclktz: tz.tz_minuteswest: " << tz.tz_minuteswest; | |
824 | + } | |
825 | + } | |
826 | + | |
827 | + // At this moment, system time should be local time too, | |
828 | + // set it back to utc which linux required. | |
829 | + tv.tv_sec += tz.tz_minuteswest * 60; | |
830 | + if (!settimeofday(&tv, &tz)) { | |
831 | + return Success(); | |
832 | + } | |
833 | + return ErrnoError() << "settimeofday() with tv failed"; | |
834 | + } | |
835 | + | |
793 | 836 | if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) { |
794 | 837 | return Error() << "Unable to parse mins_west_of_gmt"; |
795 | 838 | } |