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 #include "measure_data.h"
16 #include <string>
17 #include <unordered_map>
18 #include <cmath>
19 #include "i18n_hilog.h"
20 #include "utils.h"
21
22 namespace OHOS {
23 namespace Global {
24 namespace I18n {
25 using namespace std;
26 const double FT_TO_M = 0.3048;
27 const double FT2_TO_M2 = FT_TO_M * FT_TO_M;
28 const double FT3_TO_M3 = FT_TO_M * FT2_TO_M2;
29 const double IN3_TO_M3 = FT3_TO_M3 / (12 * 12 * 12);
30 const double GAL_IMP_TO_M3 = 0.00454609;
31 const double GAL_TO_M3 = 231 * IN3_TO_M3;
32 const int REGION_OFFSET = 7;
33 const int BASE_VALUE_SIZE = 2;
34 const int FACTOR_SIZE = 2;
35 const int CHAR_OFFSET = 48;
36 const int MAX_UNIT_NUM = 500;
37 const size_t REGION_SIZE = 2;
38
39 const std::unordered_map<std::string, std::vector<std::string>> USAGE_001 {
40 { "area-land-agricult", { "hectare" } },
41 { "area-land-commercl", { "hectare" } },
42 { "area-land-residntl", { "hectare" } },
43 { "length-person", { "centimeter" } },
44 { "length-person-small", { "centimeter" } },
45 { "length-rainfall", { "millimeter" } },
46 { "length-road", { "meter" } },
47 { "length-road-small", { "meter" } },
48 { "length-snowfall", { "centimeter" } },
49 { "length-vehicle", { "meter" } },
50 { "length-visiblty", { "meter" } },
51 { "length-visiblty-small", { "meter" } },
52 { "speed-road-travel", { "kilometer-per-hour" } },
53 { "speed-wind", { "kilometer-per-hour" } },
54 { "temperature-person", { "celsius" } },
55 { "temperature-weather", { "celsius" } },
56 { "volume-vehicle-fuel", { "liter" } },
57 };
58
59 const std::unordered_map<std::string, std::vector<std::string>> USAGE_DEFAULT_001 {
60 { "length", { "kilometer", "meter", "centimeter" } },
61 { "area", { "square-kilometer", "hectare", "square-meter", "square-centimeter" } },
62 { "speed", { "kilometer-per-hour" } },
63 { "temperature", { "celsius" } },
64 { "volume", { "cubic-meter", "cubic-centimeter" } },
65 };
66
67 const std::unordered_map<std::string, std::vector<std::string>> USAGE_AT {
68 { "length-person", { "meter", "centimeter" } },
69 };
70
71 const std::unordered_map<std::string, std::vector<std::string>> USAGE_BR {
72 { "length-person-informal", { "meter", "centimeter" } },
73 { "length-rainfall", { "centimeter" } },
74 };
75
76 const std::unordered_map<std::string, std::vector<std::string>> USAGE_BS {
77 { "temperature-weather", { "fahrenheit" } },
78 };
79
80 const std::unordered_map<std::string, std::vector<std::string>> USAGE_CA {
81 { "length-person", { "inch" } },
82 { "length-person-informal", { "foot", "inch" } },
83 { "length-person-small-informal", { "inch" } },
84 };
85
86 const std::unordered_map<std::string, std::vector<std::string>> USAGE_CN {
87 { "length-person-informal", { "meter", "centimeter" } },
88 };
89
90 const std::unordered_map<std::string, std::vector<std::string>> USAGE_DE {
91 { "length-person-informal", { "meter", "centimeter" } },
92 { "length-visiblty", { "meter" } },
93 };
94
95 const std::unordered_map<std::string, std::vector<std::string>> USAGE_GB {
96 { "area-land-agricult", { "acre" } },
97 { "area-land-commercl", { "acre" } },
98 { "area-land-residntl", { "acre" } },
99 { "length-person", { "inch" } },
100 { "length-person-informal", { "foot", "inch" } },
101 { "length-person-small-informal", { "inch" } },
102 { "length-road", { "yard" } },
103 { "length-road-small", { "yard" } },
104 { "length-vehicle", { "foot", "inch" } },
105 { "length-visiblty", { "mile", "foot" } },
106 { "length-visiblty-small", { "foot" } },
107 { "speed-road-travel", { "mile-per-hour" } },
108 };
109
110 const std::unordered_map<std::string, std::vector<std::string>> USAGE_DEFAULT_GB {
111 { "length", { "mile", "foot", "inch" } },
112 { "area", { "square-mile", "acre", "square-foot", "square-inch" } },
113 { "speed", { "mile-per-hour" } },
114 { "volume", { "cubic-foot", "cubic-inch" } },
115 };
116
117 const std::unordered_map<std::string, std::vector<std::string>> USAGE_KR {
118 { "speed-wind", { "meter-per-second" } },
119 };
120
121 const std::unordered_map<std::string, std::vector<std::string>> USAGE_MX {
122 { "length-person-informal", { "meter", "centimeter" } },
123 { "length-vehicle", { "meter", "centimeter" } },
124 };
125
126 const std::unordered_map<std::string, std::vector<std::string>> USAGE_NL {
127 { "length-person-informal", { "meter", "centimeter" } },
128 { "length-visiblty", { "meter" } },
129 };
130
131 const std::unordered_map<std::string, std::vector<std::string>> USAGE_NO {
132 { "length-person-informal", { "meter", "centimeter" } },
133 { "speed-wind", { "meter-per-second" } },
134 };
135
136 const std::unordered_map<std::string, std::vector<std::string>> USAGE_SE {
137 { "length-person", { "meter", "centimeter" } },
138 { "length-road", { "mile-scandinavian", "kilometer" } },
139 { "length-road-informal", { "mile-scandinavian" } },
140 { "speed-wind", { "meter-per-second" } },
141 };
142
143 const std::unordered_map<std::string, std::vector<std::string>> USAGE_US {
144 { "area-land-agricult", { "acre" } },
145 { "area-land-commercl", { "acre" } },
146 { "area-land-residntl", { "acre" } },
147 { "length-person", { "inch" } },
148 { "length-person-informal", { "foot", "inch" } },
149 { "length-person-small", { "inch" } },
150 { "length-rainfall", { "inch" } },
151 { "length-road", { "mile" } },
152 { "length-road-small", { "foot" } },
153 { "length-snowfall", { "inch" } },
154 { "length-vehicle", { "foot", "inch" } },
155 { "length-visiblty", { "mile", "foot" } },
156 { "length-visiblty-small", { "foot" } },
157 { "speed-road-travel", { "mile-per-hour" } },
158 { "speed-wind", { "mile-per-hour" } },
159 { "temperature-person", { "fahrenheit" } },
160 { "temperature-weather", { "fahrenheit" } },
161 { "volume-vehicle-fuel", { "gallon" } },
162 };
163
164 const std::unordered_map<std::string, std::vector<std::string>> USAGE_DEFAULT_US {
165 { "length", { "mile", "foot", "inch" } },
166 { "area", { "square-mile", "acre", "square-foot", "square-inch" } },
167 { "speed", { "mile-per-hour" } },
168 { "temperature", { "fahrenheit" } },
169 { "volume", { "cubic-foot", "cubic-inch" } },
170 };
171
172 const std::unordered_map<std::string, std::vector<double>> CONVERT_FACTORS {
173 { "acre", { FT2_TO_M2 * 43560, 0 } },
174 { "hectare", { 10000, 0 } },
175 { "dunam", { 1000, 0 } },
176 { "fathom", { FT_TO_M * 6, 0 } },
177 { "foot", { FT_TO_M, 0 } },
178 { "furlong", { FT_TO_M * 660, 0 } },
179 { "inch", { FT_TO_M / 12, 0 } },
180 { "meter", { 1, 0 } },
181 { "mile", { FT_TO_M * 5280, 0 } },
182 { "mile-scandinavian", { 10000, 0 } },
183 { "nautical-mile", { 1852, 0 } },
184 { "point", { FT_TO_M / 864, 0 } },
185 { "yard", { FT_TO_M * 3, 0 } },
186 { "knot", { 1852 / 3600.0, 0 } },
187 { "kilometer-per-hour", { 1000 / 3600.0, 0 } },
188 { "celsius", { 1, 273.15 }},
189 { "fahrenheit", {5.0 / 9, 2298.35 / 9 }},
190 { "kelvin", { 1, 0 } },
191 { "acre-foot", { 43560 * FT3_TO_M3, 0 } },
192 { "bushel", { GAL_IMP_TO_M3 * 8, 0 } },
193 { "bushel-UK", { GAL_IMP_TO_M3 * 8, 0 } },
194 { "bushel-US", { IN3_TO_M3 * 2150.42, 0 } },
195 { "cup", { GAL_IMP_TO_M3 / 16, 0 } },
196 { "cup-UK", { GAL_IMP_TO_M3 / 16, 0 } },
197 { "cup-US", { GAL_TO_M3 / 16, 0 } },
198 { "cup-metric", {0.00025, 0 } },
199 { "fluid-ounce", { GAL_IMP_TO_M3 / 128, 0 } },
200 { "fluid-ounce-imperial", { GAL_IMP_TO_M3 / 160, 0 } },
201 { "gallon", { GAL_TO_M3, 0 } },
202 { "gallon-imperial", { GAL_IMP_TO_M3, 0 } },
203 { "liter", {0.001, 0 } },
204 { "pint-metric", { 0.0005, 0 } },
205 { "pint", { GAL_IMP_TO_M3 / 8, 0 } },
206 { "pint-UK", { GAL_IMP_TO_M3 / 8, 0 } },
207 { "pint-US", { GAL_TO_M3 / 8, 0 } },
208 { "quart", { GAL_IMP_TO_M3 / 4, 0 } },
209 { "quart-UK", { GAL_IMP_TO_M3 / 4, 0 } },
210 { "quart-US", { GAL_TO_M3 / 4, 0 } },
211 { "tablespoon", { GAL_IMP_TO_M3 / 256, 0 } },
212 { "tablespoon-UK", { GAL_IMP_TO_M3 / 256, 0 } },
213 { "tablespoon-US", { GAL_TO_M3 / 256, 0 } },
214 { "teaspoon", { GAL_IMP_TO_M3 / (16 * 48), 0 } },
215 { "teaspoon-UK", { GAL_IMP_TO_M3 / (16 * 48), 0 } },
216 { "teaspoon-US", { GAL_TO_M3 / (16 * 48), 0 } },
217 { "barrel", { GAL_IMP_TO_M3 * 36, 0 } },
218 { "barrel-UK", { GAL_IMP_TO_M3 * 36, 0 } },
219 { "barrel-US", { GAL_TO_M3 * 42, 0 } },
220 { "hour", { 3600, 0 } },
221 { "second", { 1, 0 } },
222 };
223
224 const std::unordered_map<std::string, std::string> CONVERT_TO_UNIT {
225 { "acre", "square-meter" },
226 { "hectare", "square-meter" },
227 { "dunam", "square-meter" },
228 { "fathom", "meter" },
229 { "foot", "meter" },
230 { "furlong", "meter" },
231 { "inch", "meter" },
232 { "meter", "meter" },
233 { "mile", "meter" },
234 { "mile-scandinavian", "meter" },
235 { "nautical-mile", "meter" },
236 { "point", "meter" },
237 { "yard", "meter" },
238 { "knot", "meter-per-second" },
239 { "kilometer-per-hour", "meter-per-second" },
240 { "celsius", "kelvin" },
241 { "fahrenheit", "kelvin" },
242 { "kelvin", "kelvin" },
243 { "acre-foot", "cubic-meter" },
244 { "bushel", "cubic-meter" },
245 { "bushel-UK", "cubic-meter" },
246 { "bushel-US", "cubic-meter" },
247 { "cup", "cubic-meter" },
248 { "cup-UK", "cubic-meter" },
249 { "cup-US", "cubic-meter" },
250 { "cup-metric", "cubic-meter" },
251 { "fluid-ounce", "cubic-meter" },
252 { "fluid-ounce-imperial", "cubic-meter" },
253 { "gallon", "cubic-meter" },
254 { "gallon-imperial", "cubic-meter" },
255 { "liter", "cubic-meter" },
256 { "pint-metric", "cubic-meter" },
257 { "pint", "cubic-meter" },
258 { "pint-UK", "cubic-meter" },
259 { "pint-US", "cubic-meter" },
260 { "quart", "cubic-meter" },
261 { "quart-UK", "cubic-meter" },
262 { "quart-US", "cubic-meter" },
263 { "tablespoon", "cubic-meter" },
264 { "tablespoon-UK", "cubic-meter" },
265 { "tablespoon-US", "cubic-meter" },
266 { "teaspoon", "cubic-meter" },
267 { "teaspoon-UK", "cubic-meter" },
268 { "teaspoon-US", "cubic-meter" },
269 { "barrel", "cubic-meter" },
270 { "barrel-US", "cubic-meter" },
271 { "barrel-UK", "cubic-meter" },
272 { "hour", "second" },
273 { "second", "second" },
274 };
275
276 const std::unordered_map<std::string, double> PREFIX_VALUE {
277 { "deci", pow(10, -1) },
278 { "centi", pow(10, -2) },
279 { "milli", pow(10, -3) },
280 { "micro", pow(10, -6) },
281 { "nano", pow(10, -9) },
282 { "pico", pow(10, -12) },
283 { "kilo", pow(10, 3) },
284 { "hecto", pow(10, 2) },
285 { "mega", pow(10, 6) },
286 };
287
288 const std::vector<std::string> BYTE_VALUE {
289 "byte",
290 "kilobyte",
291 "megabyte",
292 "gigabyte",
293 "terabyte",
294 "petabyte"
295 };
296
297 const std::vector<std::pair<std::string, double>> DATE_VALUE {
298 { "second", 1 },
299 { "minute", 60 },
300 { "hour", 60 },
301 { "day", 24},
302 { "month", 30 },
303 { "year", 365 / 30 }
304 };
305
306 const std::unordered_map<std::string, double> POWER_VALUE {
307 { "square-", 2 },
308 { "cubic-", 3 },
309 };
310
GetMask(const string & region)311 uint32_t GetMask(const string ®ion)
312 {
313 if (region.length() < REGION_SIZE) {
314 HILOG_ERROR_I18N("GetMask: region is invalid.");
315 return 0;
316 }
317 uint32_t firstChar = (region.c_str()[0] - CHAR_OFFSET);
318 uint32_t secondChar = (region.c_str()[1] - CHAR_OFFSET);
319 return (firstChar << REGION_OFFSET) | secondChar;
320 }
321
GetDefaultPreferredUnit(const string & region,const string & type,vector<string> & units)322 void GetDefaultPreferredUnit(const string ®ion, const string &type, vector<string> &units)
323 {
324 switch (GetMask(region)) {
325 case 0x00000B92: {
326 if (USAGE_DEFAULT_GB.count(type) > 0) {
327 units.assign(USAGE_DEFAULT_GB.at(type).begin(), USAGE_DEFAULT_GB.at(type).end());
328 }
329 break;
330 }
331 case 0x000012A3: {
332 if (USAGE_DEFAULT_US.count(type) > 0) {
333 units.assign(USAGE_DEFAULT_US.at(type).begin(), USAGE_DEFAULT_US.at(type).end());
334 }
335 break;
336 }
337 default: {
338 if (USAGE_DEFAULT_001.count(type) > 0) {
339 units.assign(USAGE_DEFAULT_001.at(type).begin(), USAGE_DEFAULT_001.at(type).end());
340 }
341 break;
342 }
343 }
344 }
345
GetFallbackPreferredUnit(const string & region,const string & usage,vector<string> & units)346 void GetFallbackPreferredUnit(const string ®ion, const string &usage, vector<string> &units)
347 {
348 switch (GetMask(region)) {
349 case 0x00000EA8: {
350 if (USAGE_MX.count(usage) > 0) {
351 units.assign(USAGE_MX.at(usage).begin(), USAGE_MX.at(usage).end());
352 return;
353 }
354 break;
355 }
356 case 0x00000F1C: {
357 if (USAGE_NL.count(usage) > 0) {
358 units.assign(USAGE_NL.at(usage).begin(), USAGE_NL.at(usage).end());
359 return;
360 }
361 break;
362 }
363 case 0x00000F1F:
364 case 0x0000101C:
365 case 0x00001125: {
366 if (USAGE_NO.count(usage) > 0) {
367 units.assign(USAGE_NO.at(usage).begin(), USAGE_NO.at(usage).end());
368 return;
369 }
370 break;
371 }
372 case 0x00001195: {
373 if (USAGE_SE.count(usage) > 0) {
374 units.assign(USAGE_SE.at(usage).begin(), USAGE_SE.at(usage).end());
375 return;
376 }
377 break;
378 }
379 case 0x000012A3: {
380 if (USAGE_US.count(usage) > 0) {
381 units.assign(USAGE_US.at(usage).begin(), USAGE_US.at(usage).end());
382 return;
383 }
384 break;
385 }
386 default: {
387 if (USAGE_001.count(usage) > 0) {
388 units.assign(USAGE_001.at(usage).begin(), USAGE_001.at(usage).end());
389 return;
390 }
391 break;
392 }
393 }
394 }
395
GetRestPreferredUnit(const string & region,const string & usage,vector<string> & units)396 void GetRestPreferredUnit(const string ®ion, const string &usage, vector<string> &units)
397 {
398 switch (GetMask(region)) {
399 case 0x00000991:
400 case 0x00000C9E: {
401 if (USAGE_CA.count(usage) > 0) {
402 units.assign(USAGE_CA.at(usage).begin(), USAGE_CA.at(usage).end());
403 return;
404 }
405 break;
406 }
407 case 0x0000099E:
408 case 0x00000A1B:
409 case 0x00001024: {
410 if (USAGE_CN.count(usage) > 0) {
411 units.assign(USAGE_CN.at(usage).begin(), USAGE_CN.at(usage).end());
412 return;
413 }
414 break;
415 }
416 case 0x00000A15: {
417 if (USAGE_DE.count(usage) > 0) {
418 units.assign(USAGE_DE.at(usage).begin(), USAGE_DE.at(usage).end());
419 return;
420 }
421 break;
422 }
423 case 0x00000B92: {
424 if (USAGE_GB.count(usage) > 0) {
425 units.assign(USAGE_GB.at(usage).begin(), USAGE_GB.at(usage).end());
426 return;
427 }
428 break;
429 }
430 case 0x00000DA2: {
431 if (USAGE_KR.count(usage) > 0) {
432 units.assign(USAGE_KR.at(usage).begin(), USAGE_KR.at(usage).end());
433 return;
434 }
435 break;
436 }
437 default: {
438 GetFallbackPreferredUnit(region, usage, units);
439 break;
440 }
441 }
442 if (!units.size()) {
443 GetDefaultPreferredUnit(region, usage, units);
444 }
445 }
446
GetPreferredUnit(const string & region,const string & usage,vector<string> & units)447 void GetPreferredUnit(const string ®ion, const string &usage, vector<string> &units)
448 {
449 switch (GetMask(region)) {
450 case 0x000008A4:
451 case 0x00000915:
452 case 0x00000A2A:
453 case 0x00000A97:
454 case 0x00000AA3:
455 case 0x00000B22:
456 case 0x00000C1B:
457 case 0x00000C94:
458 case 0x00000C9C:
459 case 0x00000CA4:
460 case 0x00000D1F:
461 case 0x00000EA9:
462 case 0x00001191:
463 case 0x00001222:
464 case 0x0000131E: {
465 if (USAGE_AT.count(usage) > 0) {
466 units.assign(USAGE_AT.at(usage).begin(), USAGE_AT.at(usage).end());
467 return;
468 }
469 break;
470 }
471 case 0x00000922: {
472 if (USAGE_BR.count(usage) > 0) {
473 units.assign(USAGE_BR.at(usage).begin(), USAGE_BR.at(usage).end());
474 return;
475 }
476 break;
477 }
478 case 0x00000923:
479 case 0x0000092A:
480 case 0x00001022:
481 case 0x00001027: {
482 if (USAGE_BS.count(usage) > 0) {
483 units.assign(USAGE_BS.at(usage).begin(), USAGE_BS.at(usage).end());
484 return;
485 }
486 break;
487 }
488 default: {
489 GetRestPreferredUnit(region, usage, units);
490 break;
491 }
492 }
493 if (!units.size()) {
494 GetRestPreferredUnit(region, usage, units);
495 }
496 }
497
ComputeFactorValue(const string & unit,const string & measSys,vector<double> & factors)498 void ComputeFactorValue(const string &unit, const string &measSys, vector<double> &factors)
499 {
500 string unitKey = unit + "-" + measSys;
501 if (CONVERT_FACTORS.count(unitKey) > 0) {
502 factors[0] = CONVERT_FACTORS.at(unitKey)[0];
503 factors[1] = CONVERT_FACTORS.at(unitKey)[1];
504 return;
505 }
506 if (CONVERT_FACTORS.count(unit) > 0) {
507 factors[0] = CONVERT_FACTORS.at(unit)[0];
508 factors[1] = CONVERT_FACTORS.at(unit)[1];
509 }
510 }
511
ComputeSIPrefixValue(const string & unit)512 double ComputeSIPrefixValue(const string &unit)
513 {
514 for (auto& prefixValue : PREFIX_VALUE) {
515 if (!unit.rfind(prefixValue.first, 0)) {
516 return prefixValue.second;
517 }
518 }
519 return 0.0;
520 }
521
ComputePowerValue(const string & unit,const string & measSys,vector<double> & factors)522 void ComputePowerValue(const string &unit, const string &measSys, vector<double> &factors)
523 {
524 for (auto& powerValue : POWER_VALUE) {
525 if (!unit.rfind(powerValue.first, 0)) {
526 string baseUnit = unit.substr(powerValue.first.length());
527 double value = ComputeSIPrefixValue(baseUnit);
528 double compare = 0.0;
529 if (fabs(value - compare) < 1e-6) {
530 ComputeFactorValue(baseUnit, measSys, factors);
531 }
532 factors[0] = pow(factors[0], powerValue.second);
533 }
534 }
535 }
536
ComputeValue(const string & unit,const string & measSys,vector<double> & factors)537 int ComputeValue(const string &unit, const string &measSys, vector<double> &factors)
538 {
539 if (unit.find("-per-") != string::npos) {
540 vector<string> baseValues;
541 Split(unit, "-per-", baseValues);
542 if (baseValues.size() == BASE_VALUE_SIZE) {
543 vector<double> numerator = { 1.0, 0.0 };
544 int status = ComputeValue(baseValues[0], measSys, numerator);
545 if (!status) {
546 return 0;
547 }
548 vector<double> denominator = { 1.0, 0.0 };
549 status = ComputeValue(baseValues[1], measSys, denominator);
550 if (!status) {
551 return 0;
552 }
553 factors[0] = numerator[0] / denominator[0];
554 return 1;
555 }
556 }
557 double compare = 0.0;
558 factors[0] = ComputeSIPrefixValue(unit);
559 if (fabs(factors[0] - compare) < 1e-6) {
560 ComputePowerValue(unit, measSys, factors);
561 }
562 if (fabs(factors[0] - compare) < 1e-6) {
563 ComputeFactorValue(unit, measSys, factors);
564 }
565 if (fabs(factors[0] - compare) < 1e-6) {
566 factors[0] = 1.0;
567 return 0;
568 }
569 return 1;
570 }
571
ConvertByte(double & number,std::string & unit)572 bool ConvertByte(double& number, std::string& unit)
573 {
574 double tempNumber = number;
575 if (tempNumber < 0) {
576 tempNumber *= -1;
577 }
578 std::string tempUnit = unit;
579 int indexFirst = -1;
580 for (size_t i = 0; i < BYTE_VALUE.size(); i++) {
581 if (BYTE_VALUE[i] == tempUnit) {
582 indexFirst = static_cast<int>(i);
583 break;
584 }
585 }
586 bool status = (indexFirst >= 0);
587 size_t indexSecond = status ? static_cast<size_t>(indexFirst) : 0;
588 for (size_t i = indexSecond; i > 0; i--) {
589 if (tempNumber < 1) {
590 // 10 ^ 3 is the threshold for carry
591 tempNumber *= pow(10, 3);
592 tempUnit = BYTE_VALUE[i - 1];
593 indexSecond = i - 1;
594 }
595 }
596
597 for (size_t i = indexSecond + 1; i < BYTE_VALUE.size(); i++) {
598 // 900 is the threshold for carry
599 if (tempNumber > 900) {
600 // 10 ^ 3 is the threshold for carry
601 tempNumber /= pow(10, 3);
602 tempUnit = BYTE_VALUE[i];
603 } else {
604 break;
605 }
606 }
607
608 if (status) {
609 number = tempNumber;
610 unit = tempUnit;
611 return true;
612 }
613 return false;
614 }
615
ConvertDate(double & number,std::string & unit)616 bool ConvertDate(double& number, std::string& unit)
617 {
618 double tempNumber = number;
619 if (tempNumber < 0) {
620 tempNumber *= -1;
621 }
622 std::string tempUnit = unit;
623
624 int indexFirst = -1;
625 for (size_t i = 0; i < DATE_VALUE.size(); i++) {
626 if (DATE_VALUE[i].first == tempUnit) {
627 indexFirst = static_cast<int>(i);
628 break;
629 }
630 }
631 bool status = (indexFirst >= 0);
632 size_t indexSecond = status ? static_cast<size_t>(indexFirst) : 0;
633 for (size_t i = indexSecond; i > 0; i--) {
634 if (tempNumber < 1) {
635 tempNumber *= DATE_VALUE[i].second;
636 tempUnit = DATE_VALUE[i - 1].first;
637 indexSecond = i - 1;
638 }
639 }
640
641 for (size_t i = indexSecond + 1; i < DATE_VALUE.size(); i++) {
642 if ((tempNumber / DATE_VALUE[i].second) >= 1) {
643 tempNumber /= DATE_VALUE[i].second;
644 tempUnit = DATE_VALUE[i].first;
645 } else {
646 break;
647 }
648 }
649
650 if (status) {
651 number = floor(tempNumber);
652 number *= -1;
653 unit = tempUnit;
654 return true;
655 }
656 return false;
657 }
658
Convert(double & value,const string & fromUnit,const string & fromMeasSys,const string & toUnit,const string & toMeasSys)659 int Convert(double &value, const string &fromUnit, const string &fromMeasSys, const string &toUnit,
660 const string &toMeasSys)
661 {
662 double baseResult = 0.0;
663 double result = 0.0;
664 vector<double> fromFactors = {0.0, 0.0};
665 string fromUnitType;
666 string toUnitType;
667 icu::MeasureUnit unitArray[MAX_UNIT_NUM];
668 UErrorCode icuStatus = U_ZERO_ERROR;
669 icu::MeasureUnit::getAvailable(unitArray, MAX_UNIT_NUM, icuStatus);
670 if (!U_SUCCESS(icuStatus)) {
671 return 0;
672 }
673 for (icu::MeasureUnit curUnit : unitArray) {
674 if (!strcmp(curUnit.getSubtype(), fromUnit.c_str())) {
675 fromUnitType = curUnit.getType();
676 }
677 if (!strcmp(curUnit.getSubtype(), toUnit.c_str())) {
678 toUnitType = curUnit.getType();
679 }
680 }
681 if (fromUnitType.empty() || toUnitType.empty() || strcmp(fromUnitType.c_str(), toUnitType.c_str())) {
682 return 0;
683 }
684 int status = ComputeValue(fromUnit, fromMeasSys, fromFactors);
685 if (!status) {
686 return 0;
687 }
688 vector<double> toFactors = {0.0, 0.0};
689 status = ComputeValue(toUnit, toMeasSys, toFactors);
690 if (!status) {
691 return 0;
692 }
693 if (fromFactors.size() == FACTOR_SIZE) {
694 baseResult = fromFactors[0] * value + fromFactors[1];
695 }
696 if (toFactors.size() == FACTOR_SIZE) {
697 result = (baseResult - toFactors[1]) / toFactors[0];
698 }
699 value = result;
700 return 1;
701 }
702 } // namespace I18n
703 } // namespace Global
704 } // namespace OHOS