• 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_DATE_TIME_FORMAT_H
16 #define OHOS_GLOBAL_I18N_DATE_TIME_FORMAT_H
17 
18 #include <map>
19 #include <vector>
20 #include <climits>
21 #include <set>
22 #include "locale_info.h"
23 #include "unicode/datefmt.h"
24 #include "unicode/dtptngen.h"
25 #include "unicode/localebuilder.h"
26 #include "unicode/locid.h"
27 #include "unicode/smpdtfmt.h"
28 #include "unicode/timezone.h"
29 #include "unicode/calendar.h"
30 #include "unicode/numsys.h"
31 #include "unicode/dtitvfmt.h"
32 
33 namespace OHOS {
34 namespace Global {
35 namespace I18n {
36 class DateTimeFormat {
37 public:
38     DateTimeFormat(const std::vector<std::string> &localeTags, std::map<std::string, std::string> &configs);
39     virtual ~DateTimeFormat();
40     std::string Format(int64_t milliseconds);
41     std::string FormatRange(int64_t fromMilliseconds, int64_t toMilliseconds);
42     void GetResolvedOptions(std::map<std::string, std::string> &map);
43     std::string GetDateStyle() const;
44     std::string GetTimeStyle() const;
45     std::string GetHourCycle() const;
46     std::string GetTimeZone() const;
47     std::string GetTimeZoneName() const;
48     std::string GetNumberingSystem() const;
49     std::string GetHour12() const;
50     std::string GetWeekday() const;
51     std::string GetEra() const;
52     std::string GetYear() const;
53     std::string GetMonth() const;
54     std::string GetDay() const;
55     std::string GetHour() const;
56     std::string GetMinute() const;
57     std::string GetSecond() const;
58     static std::unique_ptr<DateTimeFormat> CreateInstance(const std::vector<std::string> &localeTags,
59                                                           std::map<std::string, std::string> &configs);
60 
61 private:
62     std::string localeTag;
63     std::string dateStyle;
64     std::string timeStyle;
65     std::string hourCycle;
66     std::string timeZone;
67     std::string numberingSystem;
68     std::string hour12;
69     std::string weekday;
70     std::string era;
71     std::string year;
72     std::string month;
73     std::string day;
74     std::string hour;
75     std::string minute;
76     std::string second;
77     std::string timeZoneName;
78     std::string dayPeriod;
79     std::string localeMatcher;
80     std::string formatMatcher;
81     icu::DateFormat *dateFormat = nullptr;
82     icu::DateIntervalFormat *dateIntvFormat = nullptr;
83     icu::Calendar *calendar = nullptr;
84     LocaleInfo *localeInfo = nullptr;
85     icu::Locale locale;
86     icu::UnicodeString pattern;
87     char16_t yearChar = 'Y';
88     char16_t monthChar = 'M';
89     char16_t dayChar = 'd';
90     char16_t hourChar = 'h';
91     char16_t minuteChar = 'm';
92     char16_t secondChar = 's';
93     char16_t timeZoneChar = 'z';
94     char16_t weekdayChar = 'E';
95     char16_t eraChar = 'G';
96     char16_t amPmChar = 'a';
97     std::string hourTwoDigitString = "HH";
98     std::string hourNumericString = "H";
99     bool createSuccess = false;
100     static const int32_t NUMERIC_LENGTH = 1;
101     static const int32_t TWO_DIGIT_LENGTH = 2;
102     static const int32_t SHORT_LENGTH = 3;
103     static const int32_t LONG_LENGTH = 4;
104     static const int32_t NARROW_LENGTH = 5;
105     static const size_t YEAR_INDEX = 0;
106     static const size_t MONTH_INDEX = 1;
107     static const size_t DAY_INDEX = 2;
108     static const size_t HOUR_INDEX = 3;
109     static const size_t MINUTE_INDEX = 4;
110     static const size_t SECOND_INDEX = 5;
111     static const int32_t SHORT_ERA_LENGTH = 1;
112     static const int32_t LONG_ERA_LENGTH = 4;
113     static const int HALF_HOUR = 30;
114     static const int HOURS_OF_A_DAY = 24;
115     static bool icuInitialized;
116     static const char *TIMEZONE_KEY;
117     static const char *DEFAULT_TIMEZONE;
118     static constexpr int SYS_PARAM_LEN = 128;
119     static bool Init();
120     static std::map<std::string, icu::DateFormat::EStyle> dateTimeStyle;
121     bool InitWithLocale(const std::string &curLocale, std::map<std::string, std::string> &configs);
122     bool InitWithDefaultLocale(std::map<std::string, std::string> &configs);
123     void ParseConfigsPartOne(std::map<std::string, std::string> &configs);
124     void ParseConfigsPartTwo(std::map<std::string, std::string> &configs);
125     void AddOptions(std::string option, char16_t optionChar);
126     void ComputeSkeleton();
127     void ComputePattern();
128     void ComputePartOfPattern(std::string option, char16_t character, std::string twoDigitChar,
129         std::string numericChar);
130     void ComputeHourCycleChars();
131     void ComputeWeekdayOrEraOfPattern(std::string option, char16_t character, std::string longChar,
132         std::string shortChar, std::string narrowChar);
133     void InitDateFormatWithoutConfigs(UErrorCode &status);
134     void InitDateFormat(UErrorCode &status);
135     void GetAdditionalResolvedOptions(std::map<std::string, std::string> &map);
136     void FixPatternPartOne();
137     void FixPatternPartTwo();
138     void RemoveAmPmChar();
139     int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size);
140     bool CheckInitSuccess();
141     void FreeDateTimeFormat();
142     std::string GetSystemTimezone();
143 };
144 } // namespace I18n
145 } // namespace Global
146 } // namespace OHOS
147 #endif