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 icu::FormattedRelativeDateTime FormatToFormattedValue(double number, const std::string &unit); 54 void FormatToParts(double number, const std::string &unit, std::vector<std::vector<std::string>> &timeVector); 55 void GetResolvedOptions(std::map<std::string, std::string> &map); 56 57 private: 58 icu::Locale locale; 59 std::string localeBaseName; 60 std::string styleString = "long"; 61 std::string numeric = "always"; 62 std::string numberingSystem; 63 std::unique_ptr<LocaleInfo> localeInfo; 64 std::unique_ptr<icu::RelativeDateTimeFormatter> relativeTimeFormat; 65 UDateRelativeDateTimeFormatterStyle style = UDAT_STYLE_LONG; 66 bool createSuccess = false; 67 static const char *DEVICE_TYPE_NAME; 68 static constexpr int CONFIG_LEN = 128; 69 static std::unordered_map<std::string, URelativeDateTimeUnit> relativeUnits; 70 static std::unordered_map<std::string, UDateRelativeDateTimeFormatterStyle> relativeFormatStyle; 71 static std::unordered_map<std::string, std::string> defaultFormatStyle; 72 void InsertInfo(std::vector<std::vector<std::string>> &timeVector, const std::string &unit, bool isInteger, 73 const std::string &value); 74 void ProcessIntegerField(const std::map<size_t, size_t> &indexMap, 75 std::vector<std::vector<std::string>> &timeVector, size_t &startIndex, const std::string &unit, 76 const std::string &result); 77 void ParseConfigs(std::map<std::string, std::string> &configs); 78 void SetDefaultStyle(); 79 }; 80 } // namespace I18n 81 } // namespace Global 82 } // namespace OHOS 83 #endif