• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 */
15import i18n from '@ohos.i18n';
16import Intl from '@ohos.intl';
17import Logger from './Logger';
18
19const TAG: string = 'IntlUtil';
20
21class IntlUtil {
22  fillNum(num: number) {
23    return num > 9 ? `${num}` : `0${num}`;
24  }
25
26  getDateString() {
27    let is24Hours = i18n.System.is24HourClock();
28    let hour12 = 'h12';
29    if (is24Hours) {
30      hour12 = 'h24';
31    }
32    let currentLocale = new Intl.Locale();
33    let locale = i18n.System.getSystemLocale();
34    let language = i18n.System.getSystemLanguage();
35    let params = [language, locale];
36    let dateFormat = new Intl.DateTimeFormat(params, {
37      locale: locale,
38      dateStyle: 'medium',
39      timeStyle: 'medium',
40      hourCycle: hour12,
41      timeZone: '',
42      numberingSystem: currentLocale.numberingSystem,
43      hour12: !is24Hours,
44      weekday: 'long',
45      era: 'long',
46      year: 'medium',
47      month: 'medium',
48      day: 'medium',
49      hour: 'medium',
50      minute: 'medium',
51      second: 'medium',
52      timeZoneName: '',
53      dayPeriod: 'narrow',
54      localeMatcher: 'best fit',
55      formatMatcher: 'best fit'
56    });
57    let date = new Date();
58    Logger.info(TAG, `language = ${language}`);
59    let formatString = dateFormat.format(date);
60    if (language === 'zh-Hans') {
61      let format = formatString.split(' ');
62      return [format[0], format[1]];
63    } else {
64      let format = formatString.split(', ');
65      return [`${format[0]}, ${format[1]}`, format[2]];
66    }
67  }
68
69  getTimeZoneString(timeZone: i18n.TimeZone) {
70    let offset = timeZone.getRawOffset();
71    return `GMT${offset > 0 ? '+' : '-'}${this.getTimeString(offset)}  ${timeZone.getDisplayName(i18n.System.getSystemLanguage())}`;
72  }
73
74  getTimeZoneShortString(timeZone: i18n.TimeZone) {
75    let offset = timeZone.getRawOffset();
76    return `GMT${offset > 0 ? '+' : '-'}${this.getTimeString(offset)}`;
77  }
78
79  getTimeString(duration: number): string {
80    let time = duration;
81    if (time < 0) {
82      time = 0 - time;
83    }
84    let hour = Math.floor(time / (1000 * 60 * 60));
85    let minute = Math.floor(time % (1000 * 60));
86    if (hour > 0) {
87      return `${this.fillNum(hour)}:${this.fillNum(minute)}`;
88    }
89    return '';
90  }
91
92  getNumberFormatString() {
93    let currentLocale = new Intl.Locale();
94    let locale = i18n.System.getSystemLocale();
95    let numfmt = new Intl.NumberFormat();
96    let options = numfmt.resolvedOptions();
97    options.locale = locale;
98    options.numberingSystem = currentLocale.numberingSystem;
99    options.useGrouping = true;
100    let params = [i18n.System.getSystemLanguage(), locale];
101    let numberFormat = new Intl.NumberFormat(params, options);
102    let currencyFormat = new Intl.NumberFormat(params, {
103      locale: locale,
104      currency: 'CNY',
105      style: 'currency'
106    });
107    options.style = 'percent';
108    let percentFormat = new Intl.NumberFormat(params, options);
109    // 此处返回的只是一个示例,因此1234567,123456,78.9并不具有实际意义
110    return [numberFormat.format(1234567), currencyFormat.format(123456), percentFormat.format(78.9)];
111  }
112}
113
114export default new IntlUtil()