• 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 router from '@ohos.router';
17import systemTime from '@ohos.systemTime';
18import Logger from '../../utils/Logger';
19import TitleBar from '../../components/TitleBar';
20import IntlUtil from '../../utils/IntlUtil';
21
22class TimeZoneInfo {
23  key: string = '';
24  timeZone: string = '';
25  display: string = '';
26}
27
28const TAG: string = 'TimeZone';
29
30@Entry
31@Component
32struct TimeZone {
33  @State timeZoneSrc: TimeZoneInfo[] = [];
34
35  async setTimeZone(key: string) {
36    try {
37      let timeZone = await systemTime.getTimezone();
38      if (timeZone === key) {
39        router.back();
40        return
41      }
42      await systemTime.setTimezone(key);
43      Logger.info(TAG, `setTimezone, ${key}`);
44      router.back();
45    } catch (err) {
46      Logger.error(TAG, JSON.stringify(err));
47    }
48  }
49
50  getTimeZoneSrc() {
51    this.timeZoneSrc = [];
52    let zoneCityIDs = i18n.TimeZone.getAvailableZoneCityIDs();
53    for (let zoneCityId of zoneCityIDs) {
54      let cityDisplayName = i18n.TimeZone.getCityDisplayName(zoneCityId, "zh-Hans");
55      let timeZone = i18n.TimeZone.getTimezoneFromCity(zoneCityId);
56      this.timeZoneSrc.push({
57        key: timeZone.getID(),
58        timeZone: IntlUtil.getTimeZoneShortString(timeZone),
59        display: cityDisplayName
60      });
61    }
62    ;
63  }
64
65  aboutToAppear() {
66    this.getTimeZoneSrc();
67  }
68
69  build() {
70    Column() {
71      TitleBar({ hasBackPress: true, title: $r('app.string.time_zone') })
72      Scroll() {
73        Column() {
74          ForEach(this.timeZoneSrc, (item: TimeZoneInfo, index: number) => {
75            Column() {
76              if (index !== 0) {
77                Divider()
78                  .color($r('app.color.divider'))
79                  .width('100%')
80                  .strokeWidth(1)
81                  .margin({ top: 10, bottom: 10 })
82              }
83              Text(item.display)
84                .fontSize(22)
85                .width('100%')
86                .margin({ top: 10, bottom: 10 })
87                .fontColor($r('app.color.black'))
88              Text(item.timeZone)
89                .fontColor($r('app.color.gray'))
90                .fontSize(20)
91                .width('100%')
92                .margin({ top: 5, bottom: 5 })
93            }
94            .id(`time_zone_item${index}`)
95            .onClick(() => {
96              this.setTimeZone(item.key);
97            })
98          }, (item: TimeZoneInfo) => item.key)
99        }
100        .width('95%')
101        .padding(10)
102        .margin({ top: 20, bottom: 10 })
103        .backgroundColor($r('app.color.white'))
104        .border({ color: $r('app.color.white'), width: 1, radius: 15 })
105      }
106      .height('92%')
107    }
108    .width('100%')
109    .height('100%')
110    .backgroundColor($r('app.color.f5f5f5'))
111  }
112}