1 /* 2 * Copyright (c) 2021-2022 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 #ifndef OHOS_GLOBAL_I18N_RELATIVE_TIME_FORMAT_H 16 #define OHOS_GLOBAL_I18N_RELATIVE_TIME_FORMAT_H 17 18 #include <map> 19 #include <set> 20 #include <unordered_map> 21 #include <vector> 22 #include "unicode/numberformatter.h" 23 #include "unicode/locid.h" 24 #include "unicode/numfmt.h" 25 #include "unicode/unum.h" 26 #include "unicode/decimfmt.h" 27 #include "unicode/localebuilder.h" 28 #include "unicode/numsys.h" 29 #include "unicode/measfmt.h" 30 #include "unicode/measunit.h" 31 #include "unicode/measure.h" 32 #include "unicode/currunit.h" 33 #include "unicode/fmtable.h" 34 #include "unicode/reldatefmt.h" 35 #include "unicode/uformattedvalue.h" 36 #include "unicode/ures.h" 37 #include "unicode/unum.h" 38 #include "unicode/ulocdata.h" 39 #include "unicode/ureldatefmt.h" 40 #include "number_utils.h" 41 #include "number_utypes.h" 42 #include "locale_info.h" 43 #include "measure_data.h" 44 45 namespace OHOS { 46 namespace Global { 47 namespace I18n { 48 class RelativeTimeFormat { 49 public: 50 RelativeTimeFormat(const std::vector<std::string> &localeTag, std::map<std::string, std::string> &configs); 51 virtual ~RelativeTimeFormat(); 52 std::string Format(double number, const std::string &unit); 53 void FormatToParts(double number, const std::string &unit, std::vector<std::vector<std::string>> &timeVector); 54 void GetResolvedOptions(std::map<std::string, std::string> &map); 55 56 private: 57 icu::Locale locale; 58 std::string localeBaseName; 59 std::string styleString = "long"; 60 std::string numeric = "always"; 61 std::string numberingSystem; 62 std::unique_ptr<LocaleInfo> localeInfo; 63 std::unique_ptr<icu::RelativeDateTimeFormatter> relativeTimeFormat; 64 UDateRelativeDateTimeFormatterStyle style = UDAT_STYLE_LONG; 65 bool createSuccess = false; 66 static std::unordered_map<std::string, URelativeDateTimeUnit> relativeUnits; 67 static std::unordered_map<std::string, UDateRelativeDateTimeFormatterStyle> relativeFormatStyle; 68 void InsertInfo(std::vector<std::vector<std::string>> &timeVector, const std::string &unit, bool isInteger, 69 const std::string &value); 70 void ProcessIntegerField(const std::map<size_t, size_t> &indexMap, 71 std::vector<std::vector<std::string>> &timeVector, size_t &startIndex, const std::string &unit, 72 const std::string &result); 73 void ParseConfigs(std::map<std::string, std::string> &configs); 74 }; 75 } // namespace I18n 76 } // namespace Global 77 } // namespace OHOS 78 #endif