1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "date_time_matched.h" 16 #include "i18n_hilog.h" 17 18 namespace OHOS { 19 namespace Global { 20 namespace I18n { DateTimeMatched(std::string & locale)21DateTimeMatched::DateTimeMatched(std::string& locale) 22 { 23 dateRuleInit = new DateRuleInit(locale); 24 if (dateRuleInit == nullptr) { 25 HILOG_ERROR_I18N("DateRuleInit construct failed."); 26 } 27 } 28 ~DateTimeMatched()29DateTimeMatched::~DateTimeMatched() 30 { 31 if (dateRuleInit != nullptr) { 32 delete dateRuleInit; 33 } 34 } 35 GetMatchedDateTime(icu::UnicodeString & message)36std::vector<int> DateTimeMatched::GetMatchedDateTime(icu::UnicodeString& message) 37 { 38 icu::UnicodeString messageStr = message; 39 std::vector<int> result {0}; 40 if (this->dateRuleInit == nullptr) { 41 HILOG_ERROR_I18N("GetMatchedDateTime failed because this->dateRuleInit is nullptr."); 42 return result; 43 } 44 std::vector<MatchedDateTimeInfo> matches = this->dateRuleInit->Detect(messageStr); 45 size_t length = matches.size(); 46 if (length > 0) { 47 size_t posDateTime = 2; 48 size_t posBegin = 1; 49 size_t posEnd = 2; 50 result.resize(posDateTime * length + 1); 51 result[0] = static_cast<int>(length); 52 for (size_t i = 0; i < length; i++) { 53 result[i * posDateTime + posBegin] = matches[i].GetBegin(); 54 result[i * posDateTime + posEnd] = matches[i].GetEnd(); 55 } 56 } 57 return result; 58 } 59 } // namespace I18n 60 } // namespace Global 61 } // namespace OHOS