• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Time Zone Setting
2
3## Use Cases
4
5The local time of different countries and regions varies according to their longitude. Therefore, different time zones are planned. For example, the UK uses time zone 0 and China uses time zone GMT+8. The time in China is eight hours earlier than that in the UK. For example, 12:00 in Beijing is 4 a.m. in London. The time zone module allows your application to obtain the time zone list to implement its own service logic, for example, a dual-clock application.
6
7## How to Develop
8
9### Time Zone-related Functions
10
111. Import the **i18n** module.
12   ```ts
13   import { i18n } from '@kit.LocalizationKit';
14   ```
15
162. Create a **TimeZone** object and implement functions such as calculating the offset between a fixed time zone and the actual time zone and obtaining and traversing the time zone list.
17   ```ts
18   // Obtain the time zone of Brazil.
19   let timezone: i18n.TimeZone = i18n.getTimeZone('America/Sao_Paulo'); // Pass in a specific time zone to create a TimeZone object.
20   let timezoneId: string = timezone.getID(); // timezoneId = 'America/Sao_Paulo'
21
22   // Obtain the TimeZone object corresponding to the city ID.
23   let aucklandTimezone: i18n.TimeZone = i18n.TimeZone.getTimezoneFromCity('Auckland');
24   timezoneId = aucklandTimezone.getID(); // timezoneId = 'Pacific/Auckland'
25
26   // Localized time zone name
27   let timeZoneName: string = timezone.getDisplayName ('zh-Hans', true); // timeZoneName ='Brasília Standard Time'
28
29   // Localized city name
30   let cityDisplayName: string = i18n.TimeZone.getCityDisplayName('Auckland', 'zh-Hans'); // cityDisplayName = 'Auckland (New Zealand)'
31
32   // Fixed offset of the time zone
33   let rawOffset: number = timezone.getRawOffset(); // rawOffset = -10800000
34
35   // Actual offset of the time zone (fixed offset + DST)
36   let offset: number = timezone.getOffset(1234567890); // offset = -10800000
37
38   // List of time zone IDs supported by the system.
39   let availableIDs: Array<string> = i18n.TimeZone.getAvailableIDs(); // availableIDs = ['America/Adak', 'Asia/Hovd', ...]
40
41   // List of time zone city IDs supported by the system.
42   let cityIDs: Array<string> = i18n.TimeZone.getAvailableZoneCityIDs(); // cityIDs = ['Auckland', 'Magadan', ...]
43
44   // Traverse the list of time zone city IDs.
45   let timezoneList: Array<object> = []; // Time zone list displayed to the user
46
47   class Item {
48     cityDisplayName: string = "";
49     timezoneId: string = "";
50     offset: string = "";
51     cityId: string = ""
52   }
53
54   for (let i = 0; i < cityIDs.length; i++) {
55     let cityId: string = cityIDs[i];
56     let timezone: i18n.TimeZone = i18n.TimeZone.getTimezoneFromCity(cityId); // TimeZone object corresponding to the city ID
57     let cityDisplayName: string = i18n.TimeZone.getCityDisplayName(cityId, 'zh-CN'); // Localized city name
58     let timestamp: number = (new Date()).getTime();
59     let item: Item = {
60        cityDisplayName: cityDisplayName,
61        timezoneId: timezone.getID(),
62        offset: 'GMT' + (timezone.getOffset(timestamp) / 3600 * 1000),
63        cityId: cityId
64     };
65     timezoneList.push(item);
66   }
67
68   // TimeZone object array corresponding to the specified geographical coordinates
69   let timezoneArray: Array<i18n.TimeZone> = i18n.TimeZone.getTimezonesByLocation(-43.1, -22.5);
70   ```
71
72### Dual-Clock Application
73
741. Import the **i18n** module.
75   ```ts
76   import { i18n } from '@kit.LocalizationKit';
77   ```
78
792. Add a time zone to the preferred time zone list of the application.
80   ```ts
81   let pauloTimezone: i18n.TimeZone = i18n.getTimeZone('America/Sao_Paulo');
82   let defaultTimezone: i18n.TimeZone = i18n.getTimeZone();
83   let appPreferredTimeZoneList: Array<i18n.TimeZone> = []; // Application preferred time zone list
84   appPreferredTimeZoneList.push(pauloTimezone);
85   appPreferredTimeZoneList.push(defaultTimezone);
86   ```
87
883. Traverse the preferred time zone list of the application to obtain the time of each time zone.
89   ```ts
90   let locale: string = i18n.System.getSystemLocale();
91   for (let i = 0; i < appPreferredTimeZoneList.length; i++) {
92     let timezone: string = appPreferredTimeZoneList[i].getID();
93     let calendar: i18n.Calendar = i18n.getCalendar(locale);
94     calendar.setTimeZone(timezone); // Set the time zone of the Calendar object.
95     // Obtain the year, month, day, hour, minute, and second.
96     let year: number = calendar.get('year');
97     let month: number = calendar.get('month');
98     let day: number = calendar.get('date');
99     let hour: number = calendar.get('hour');
100     let minute: number = calendar.get('minute');
101     let second: number = calendar.get('second');
102   }
103   ```
104