• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "i18n_types.h"
43 #include "locale_info.h"
44 #include "measure_data.h"
45 
46 namespace OHOS {
47 namespace Global {
48 namespace I18n {
49 class RelativeTimeFormat {
50 public:
51     RelativeTimeFormat(const std::vector<std::string> &localeTag, std::map<std::string, std::string> &configs);
52     RelativeTimeFormat(const std::vector<std::string> &locales,
53         std::map<std::string, std::string> &configs, bool fromArkTs);
54     virtual ~RelativeTimeFormat();
55     std::string Format(double number, const std::string &unit);
56     icu::FormattedRelativeDateTime FormatToFormattedValue(double number, const std::string &unit);
57     void FormatToParts(double number, const std::string &unit, std::vector<std::vector<std::string>> &timeVector);
58     void GetResolvedOptions(std::map<std::string, std::string> &map);
59     static std::vector<std::string> SupportedLocalesOf(const std::vector<std::string> &requestLocales,
60         const std::map<std::string, std::string> &configs, I18nErrorCode &status);
61     static std::vector<std::string> CanonicalizeLocales(
62         const std::vector<std::string>& localeTags, ErrorMessage& errorMsg);
63 
64 private:
65     icu::Locale locale;
66     std::string localeBaseName;
67     std::string styleString = "long";
68     std::string numeric = "always";
69     std::string numberingSystem = "latn";
70     std::string intlNumberingSystem = "";
71     std::string localeMatcher = "best fit";
72     std::unique_ptr<icu::RelativeDateTimeFormatter> relativeTimeFormat;
73     UDateRelativeDateTimeFormatterStyle style = UDAT_STYLE_LONG;
74     bool createSuccess = false;
75     bool createFromArkTs = false;
76     static const char *DEVICE_TYPE_NAME;
77     static constexpr int CONFIG_LEN = 128;
78     static std::unordered_map<std::string, URelativeDateTimeUnit> relativeUnits;
79     static std::unordered_map<std::string, UDateRelativeDateTimeFormatterStyle> relativeFormatStyle;
80     static std::unordered_map<std::string, std::string> defaultFormatStyle;
81     static std::unordered_map<std::string, std::string> pluralUnitMap;
82     void InsertInfo(std::vector<std::vector<std::string>> &timeVector, const std::string &unit, bool isInteger,
83         const std::string &value);
84     void ProcessIntegerField(const std::map<size_t, size_t> &indexMap,
85         std::vector<std::vector<std::string>> &timeVector, size_t &startIndex, const std::string &unit,
86         const std::string &result);
87     void ParseConfigs(std::map<std::string, std::string> &configs);
88     std::string GetSingularUnit(const std::string &unit);
89     std::string GetNumberSystemFromLocale();
90     void SetDefaultStyle();
91     void SetRelativeTimeFormat(const std::vector<std::string> &localeTags,
92         std::map<std::string, std::string> &configs, bool fromArkTs);
93     void IntlFormatToParts(icu::FormattedValue &fmtRelativeTime, const std::string& result,
94         std::vector<std::vector<std::string>> &timeVector, double number, const std::string &unit);
95     std::vector<std::string> FormatPart(const std::string& type, const std::string& value,
96         const std::string& unit);
97     void CreateRelativeTimeFormatWithDefaultLocale(bool fromArkTs);
98 };
99 } // namespace I18n
100 } // namespace Global
101 } // namespace OHOS
102 #endif