• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hilog/log.h"
17 
18 namespace OHOS {
19 namespace Global {
20 namespace I18n {
21 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "DateTimeMatched" };
22 using namespace OHOS::HiviewDFX;
DateTimeMatched(std::string & locale)23 DateTimeMatched::DateTimeMatched(std::string& locale)
24 {
25     dateRuleInit = new DateRuleInit(locale);
26     if (dateRuleInit == nullptr) {
27         HiLog::Error(LABEL, "DateRuleInit construct failed.");
28     }
29 }
30 
~DateTimeMatched()31 DateTimeMatched::~DateTimeMatched()
32 {
33     delete dateRuleInit;
34 }
35 
GetMatchedDateTime(icu::UnicodeString & message)36 std::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(LABEL, "GetMatchedDateTime failed because this->dateRuleInit is nullptr.");
42         return result;
43     }
44     std::vector<MatchedDateTimeInfo> matches = this->dateRuleInit->Detect(messageStr);
45     int length = matches.size();
46     if (length > 0) {
47         int posDateTime = 2;
48         int posBegin = 1;
49         int posEnd = 2;
50         result.resize(posDateTime * length + 1);
51         result[0] = length;
52         for (int 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