system/core
Revision | 1c05cf91129fa331f0a66a5b4a8cb55a3f28509e (tree) |
---|---|
Time | 2019-03-05 14:43:31 |
Author | 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
@@ -690,8 +690,51 @@ static Result<Success> do_rmdir(const BuiltinArguments& args) { | ||
690 | 690 | return Success(); |
691 | 691 | } |
692 | 692 | |
693 | +// Read persist property from /data/property directly, because it may not have loaded. | |
694 | +// If the file not found, try to call GetProperty to get the vaule from /default.prop. | |
695 | +static std::string persist_property_get(const std::string& name) | |
696 | +{ | |
697 | + auto result = ReadFile("/data/property/" + name); | |
698 | + return !result ? base::GetProperty(name, "") : *result; | |
699 | +} | |
700 | + | |
693 | 701 | static Result<Success> do_sysclktz(const BuiltinArguments& args) { |
694 | 702 | struct timezone tz = {}; |
703 | + | |
704 | + if (persist_property_get("persist.rtc_local_time") == "1") { | |
705 | + struct timeval tv = {}; | |
706 | + | |
707 | + if (gettimeofday(&tv, NULL)) { | |
708 | + return ErrnoError() << "gettimeofday() failed"; | |
709 | + } | |
710 | + | |
711 | + // Set system time and saved system zone in case of network | |
712 | + // not available and auto syncing time not available. | |
713 | + std::string time_zone = persist_property_get("persist.sys.timezone"); | |
714 | + if (time_zone.empty()) { | |
715 | + LOG(INFO) << "sysclktz: persist.sys.timezone not found"; | |
716 | + tz.tz_minuteswest = 0; | |
717 | + } else { | |
718 | + LOG(INFO) << "sysclktz: persist.sys.timezone: " << time_zone; | |
719 | + // localtime_r need the property, we need to set it | |
720 | + property_set("persist.sys.timezone", time_zone.c_str()); | |
721 | + time_t t = tv.tv_sec; | |
722 | + struct tm tm; | |
723 | + if (localtime_r(&t, &tm)) { | |
724 | + tz.tz_minuteswest = -(tm.tm_gmtoff / 60); | |
725 | + LOG(INFO) << "sysclktz: tz.tz_minuteswest: " << tz.tz_minuteswest; | |
726 | + } | |
727 | + } | |
728 | + | |
729 | + // At this moment, system time should be local time too, | |
730 | + // set it back to utc which linux required. | |
731 | + tv.tv_sec += tz.tz_minuteswest * 60; | |
732 | + if (!settimeofday(&tv, &tz)) { | |
733 | + return Success(); | |
734 | + } | |
735 | + return ErrnoError() << "settimeofday() with tv failed"; | |
736 | + } | |
737 | + | |
695 | 738 | if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) { |
696 | 739 | return Error() << "Unable to parse mins_west_of_gmt"; |
697 | 740 | } |