1 /*
2 * Copyright (c) 2021 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
16 #ifndef GLOBAL_I18N_PATTERN_H
17 #define GLOBAL_I18N_PATTERN_H
18
19 #include <string>
20 #include "date_time_data.h"
21 #include "str_util.h"
22
23 // this file should only be included by date_time_format_impl.cpp
24 #define HOUR12_MINUTE_INDEX 0x00000000
25 #define HOUR24_MINUTE_INDEX 0x00000001
26 #define HOUR_MINUTE_INDEX 0x00000002
27 #define ABBR_MONTH_WEEKDAY_DAY_INDEX 0x00010000
28 #define ABBR_MONTH_DAY_INDEX 0x00010001
29 #define YEAR_ABBR_MONTH_ABBR_WEEKDAY_DAY_INDEX 0x00010002
30 #define YEAR_WIDE_MONTH_ABBR_WEEKDAY_DAY_INDEX 0x00010003
31 #define YEAR_SHORT_MONTH_WIDE_WEEKDAY_DAY_INDEX 0x00010004
32 #define YEAR_SHORT_MONTH_ABBR_WEEKDAY_DAY_INDEX 0x00010005
33 #define YEAR_ABBR_MONTH_WIDE_WEEKDAY_DAY_INDEX 0x00010006
34 #define YEAR_WIDE_MONTH_DAY_INDEX 0x00010007
35 #define WEEK_DAY_INDEX 0x00010008
36 #define NUMBER_MONTH_ABBR_WEEK_DAY_INDEX 0x00010009
37 #define NUMBER_MONTH_DAY_INDEX 0x0001000A
38 #define HOUR12_MINUTE_SECOND_INDEX 0x00020000
39 #define HOUR24_MINUTE_SECOND_INDEX 0x00020001
40 #define HOUR_MINUTE_SECOND_INDEX 0x00020002
41 #define FULL_INDEX 0x00030000
42 #define MEDIUM_INDEX 0x00030001
43 #define SHORT_INDEX 0x00030002
44 #define ELAPSED_MINUTE_SECOND_INDEX 0x00040000
45 #define ELAPSED_MINUTE_SECOND_MILLISECOND_INDEX 0x00040001
46
47 #define PATTERN_TYPE_SHIFT 16
48 #define PATTERN_INDEX_MASK 0x0000ffff
49
50 namespace OHOS {
51 namespace I18N {
52 enum PatternType {
53 PATTERN_TYPE_BEGIN = 0,
54 TIME_PATTERN = PATTERN_TYPE_BEGIN,
55 DATE_PATTERN,
56 HOUR_MINUTE_SECOND_PATTERN,
57 FULL_MEDIUM_SHORT_PATTERN,
58 ELAPSED_PATTERN,
59 PATTERN_TYPE_END,
60 };
61
GetPatternFromIndex(uint32_t index,const DateTimeData * const data)62 std::string GetPatternFromIndex(uint32_t index, const DateTimeData * const data)
63 {
64 uint32_t type = index >> PATTERN_TYPE_SHIFT;
65 if (type > PatternType::PATTERN_TYPE_END) {
66 return "";
67 }
68 uint32_t ind = index & PATTERN_INDEX_MASK;
69 PatternType patternType = static_cast<PatternType>(type);
70 switch (patternType) {
71 case TIME_PATTERN: {
72 return Parse(data->timePatterns, ind);
73 }
74 case DATE_PATTERN: {
75 return Parse(data->datePatterns, ind);
76 }
77 case HOUR_MINUTE_SECOND_PATTERN: {
78 return Parse(data->hourMinuteSecondPatterns, ind);
79 }
80 case FULL_MEDIUM_SHORT_PATTERN: {
81 return Parse(data->fullMediumShortPatterns, ind);
82 }
83 default: {
84 return Parse(data->elapsedPatterns, ind);
85 }
86 }
87 }
88
GetStringFromPattern2(const AvailableDateTimeFormatPattern & requestPattern,const DateTimeData * const data)89 std::string GetStringFromPattern2(const AvailableDateTimeFormatPattern &requestPattern, const DateTimeData * const data)
90 {
91 switch (requestPattern) {
92 case ABBR_MONTH_WEEKDAY_DAY: {
93 return GetPatternFromIndex(ABBR_MONTH_WEEKDAY_DAY_INDEX, data);
94 }
95 case FULL: {
96 return GetPatternFromIndex(FULL_INDEX, data);
97 }
98 case MEDIUM: {
99 return GetPatternFromIndex(MEDIUM_INDEX, data);
100 }
101 case SHORT: {
102 return GetPatternFromIndex(SHORT_INDEX, data);
103 }
104 case YEAR_ABBR_MONTH_ABBR_WEEKDAY_DAY: {
105 return GetPatternFromIndex(YEAR_ABBR_MONTH_ABBR_WEEKDAY_DAY_INDEX, data);
106 }
107 case YEAR_WIDE_MONTH_ABBR_WEEKDAY_DAY: {
108 return GetPatternFromIndex(YEAR_WIDE_MONTH_ABBR_WEEKDAY_DAY_INDEX, data);
109 }
110 case YEAR_SHORT_MONTH_WIDE_WEEKDAY_DAY: {
111 return GetPatternFromIndex(YEAR_SHORT_MONTH_WIDE_WEEKDAY_DAY_INDEX, data);
112 }
113 case YEAR_SHORT_MONTH_ABBR_WEEKDAY_DAY: {
114 return GetPatternFromIndex(YEAR_SHORT_MONTH_ABBR_WEEKDAY_DAY_INDEX, data);
115 }
116 case YEAR_ABBR_MONTH_WIDE_WEEKDAY_DAY: {
117 return GetPatternFromIndex(YEAR_ABBR_MONTH_WIDE_WEEKDAY_DAY_INDEX, data);
118 }
119 case YEAR_WIDE_MONTH_DAY: {
120 return GetPatternFromIndex(YEAR_WIDE_MONTH_DAY_INDEX, data);
121 }
122 case WEEK_DAY: {
123 return GetPatternFromIndex(WEEK_DAY_INDEX, data);
124 }
125 case NUMBER_MONTH_ABBR_WEEK_DAY: {
126 return GetPatternFromIndex(NUMBER_MONTH_ABBR_WEEK_DAY_INDEX, data);
127 }
128 case NUMBER_MONTH_DAY: {
129 return GetPatternFromIndex(NUMBER_MONTH_DAY_INDEX, data);
130 }
131 default: {
132 return "";
133 }
134 }
135 }
136
GetStringFromPattern(const AvailableDateTimeFormatPattern & requestPattern,const DateTimeData * const data)137 std::string GetStringFromPattern(const AvailableDateTimeFormatPattern &requestPattern, const DateTimeData * const data)
138 {
139 if (data == nullptr) {
140 return "";
141 }
142 switch (requestPattern) {
143 case HOUR12_MINUTE_SECOND: {
144 return GetPatternFromIndex(HOUR12_MINUTE_SECOND_INDEX, data);
145 }
146 case HOUR24_MINUTE_SECOND: {
147 return GetPatternFromIndex(HOUR24_MINUTE_SECOND_INDEX, data);
148 }
149 case HOUR_MINUTE_SECOND: {
150 return GetPatternFromIndex(HOUR_MINUTE_SECOND_INDEX, data);
151 }
152 case ABBR_MONTH_DAY: {
153 return GetPatternFromIndex(ABBR_MONTH_DAY_INDEX, data);
154 }
155 case HOUR12_MINUTE: {
156 return GetPatternFromIndex(HOUR12_MINUTE_INDEX, data);
157 }
158 case HOUR24_MINUTE: {
159 return GetPatternFromIndex(HOUR24_MINUTE_INDEX, data);
160 }
161 case HOUR_MINUTE: {
162 return GetPatternFromIndex(HOUR_MINUTE_INDEX, data);
163 }
164 default: {
165 return GetStringFromPattern2(requestPattern, data);
166 }
167 }
168 }
169
GetStringFromElapsedPattern(const ElapsedPatternType & type,const DateTimeData * const data)170 std::string GetStringFromElapsedPattern(const ElapsedPatternType &type, const DateTimeData * const data)
171 {
172 if (data == nullptr) {
173 return "";
174 }
175 switch (type) {
176 case ELAPSED_MINUTE_SECOND: {
177 return GetPatternFromIndex(ELAPSED_MINUTE_SECOND_INDEX, data);
178 }
179 case ELAPSED_MINUTE_SECOND_MILLISECOND: {
180 return GetPatternFromIndex(ELAPSED_MINUTE_SECOND_MILLISECOND_INDEX, data);
181 }
182 case ELAPSED_HOUR_MINUTE: {
183 return GetPatternFromIndex(HOUR24_MINUTE_INDEX, data);
184 }
185 case ELAPSED_HOUR_MINUTE_SECOND: {
186 return GetPatternFromIndex(HOUR24_MINUTE_SECOND_INDEX, data);
187 }
188 default: {
189 return "";
190 }
191 }
192 }
193 } // namespace I18N
194 } // namespace OHOS
195 #endif