Revert "fix: decrease the time wasted of multi initialization"

This reverts commit 3b37f3bc07.

Signed-off-by: yhpeng <pengyuhao1@huawei.com>
This commit is contained in:
yhpeng 2024-06-11 18:04:25 +08:00
parent 4b88018972
commit c38644b97e

View File

@ -190,11 +190,6 @@ void GetLocalJsonObject(InternalResource::ResourceId id, std::string language, s
} }
} }
std::shared_ptr<SimpleDateFormat> simpleDateFormat_ = nullptr;
std::shared_ptr<TimeZone> timeZone_ = nullptr;
std::shared_ptr<Calendar> calendar_ = nullptr;
std::shared_ptr<DateTimePatternGenerator> patternGenerator_ = nullptr;
} // namespace } // namespace
// for entry.json // for entry.json
@ -209,12 +204,6 @@ void Localization::SetLocaleImpl(const std::string& language, const std::string&
locale_ = std::make_unique<LocaleProxy>(language.c_str(), countryOrRegion.c_str(), "", keywordsAndValues.c_str()); locale_ = std::make_unique<LocaleProxy>(language.c_str(), countryOrRegion.c_str(), "", keywordsAndValues.c_str());
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
simpleDateFormat_ = std::make_shared<SimpleDateFormat>(UnicodeString("mm:ss"), locale->instance, status)
calendar_.reset(Calendar::createInstance(locale->instance, status));
patternGenerator_.reset(DateTimePatternGenerator::createInstance(locale->instance, status));
timeZone_.reset(TimeZone::createInstance("GMT+0:00"));
status = U_ZERO_ERROR;
std::vector<std::string> keyValuePairs; std::vector<std::string> keyValuePairs;
StringUtils::StringSplitter(keywordsAndValues, ';', keyValuePairs); StringUtils::StringSplitter(keywordsAndValues, ';', keyValuePairs);
for (const auto& pair : keyValuePairs) { for (const auto& pair : keyValuePairs) {
@ -287,18 +276,19 @@ const std::string Localization::FormatDuration(uint32_t duration, bool needShowH
needShowHour = true; needShowHour = true;
} }
const char* engTimeFormat = needShowHour ? "HH:mm:ss" : "mm:ss"; const char* engTimeFormat = needShowHour ? "HH:mm:ss" : "mm:ss";
if (!simpleDateFormat_) { auto simpleDateFormat = std::make_unique<SimpleDateFormat>(UnicodeString(engTimeFormat), locale_->instance, status);
return ""; CHECK_RETURN(status, "");
} TimeZone* timeZone = TimeZone::createTimeZone("GMT+0:00");
simpleDateFormat_->applyPattern(UnicodeString(engTimeFormat)); simpleDateFormat->setTimeZone(*timeZone);
simpleDateFormat_->setTimeZone(*timeZone_);
UnicodeString simpleStr; UnicodeString simpleStr;
simpleDateFormat_->format(1000.0 * duration, simpleStr, status); simpleDateFormat->format(1000.0 * duration, simpleStr, status);
CHECK_RETURN(status, ""); CHECK_RETURN(status, "");
std::string ret; std::string ret;
UnicodeString2String(simpleStr, ret); UnicodeString2String(simpleStr, ret);
delete(timeZone);
return ret; return ret;
} }
@ -328,21 +318,27 @@ const std::string Localization::FormatDateTime(DateTime dateTime, const std::str
{ {
WaitingForInit(); WaitingForInit();
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
CHECK_NULL_RETURN(calendar_, ""); auto cal = Calendar::createInstance(locale_->instance, status);
calendar_->set(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute, dateTime.second); CHECK_RETURN(status, "");
cal->set(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute, dateTime.second);
UDate date = cal->getTime(status); UDate date = cal->getTime(status);
delete cal;
CHECK_RETURN(status, ""); CHECK_RETURN(status, "");
CHECK_RETURN(patternGenerator_, ""); auto patternGenerator = DateTimePatternGenerator::createInstance(locale_->instance, status);
UnicodeString pattern = patternGenerator_->getBestPattern(UnicodeString(format.c_str()), status);
CHECK_RETURN(status, ""); CHECK_RETURN(status, "");
UnicodeString pattern = patternGenerator->getBestPattern(UnicodeString(format.c_str()), status);
CHECK_RETURN(simpleDateFormat_, ""); delete patternGenerator;
simpleDateFormat_->applyPattern(pattern); CHECK_RETURN(status, "");
auto dateFormat = std::make_unique<SimpleDateFormat>(pattern, locale_->instance, status);
CHECK_RETURN(status, "");
UnicodeString dateTimeStr; UnicodeString dateTimeStr;
simpleDateFormat_->format(date, dateTimeStr, status); dateFormat->format(date, dateTimeStr, status);
CHECK_RETURN(status, ""); CHECK_RETURN(status, "");
std::string ret; std::string ret;
UnicodeString2String(dateTimeStr, ret); UnicodeString2String(dateTimeStr, ret);
return ret; return ret;