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.getTimeZone("America/Sao_Paulo"); // Pass in a specific time zone to create a TimeZone object.
20   let timezoneId = timezone.getID();// America/Sao_Paulo
21
22   // Obtain the TimeZone object corresponding to the city ID.
23   let aucklandTimezone = i18n.TimeZone.getTimezoneFromCity("Auckland");
24   let aucklandTzId = aucklandTimezone.getID(); // Pacific/Auckland
25
26   // Localized time zone name
27   let timeZoneName = timezone.getDisplayName("zh-Hans", true); // Brasilia Standard Time
28
29   // Localized city name
30   let cityDisplayName = i18n.TimeZone.getCityDisplayName("Auckland", "zh-Hans") // Auckland (New Zealand)
31
32   // Fixed offset of the time zone
33   let rawOffset = timezone.getRawOffset(); // -10800000
34
35   // Actual offset of the time zone (fixed offset + DST)
36   let offset = timezone.getOffset(1234567890);// -10800000
37
38   // List of time zone IDs supported by the system
39   let ids = i18n.TimeZone.getAvailableIDs(); // ["America/Adak", "Asia/Hovd", "America/Sao_Paulo", "Asia/Jerusalem", "Europe/London",...]
40
41   // List of time zone city IDs supported by the system
42   let cityIdArray = i18n.TimeZone.getAvailableZoneCityIDs();  // ["Auckland", "Magadan", "Lord Howe Island",...]
43   // Traverse the list of time zone city IDs.
44   let timezoneList: Array<object> = []; // Time zone list displayed to the user
45   class Item {
46       cityDisplayName : string = "";
47       timezoneId : string = "";
48       offset : string = "";
49       cityId : string = ""
50   }
51   for (let i =0 ; i < cityIdArray.length ; i++) {
52       let cityId = cityIdArray[i];
53       let timezone = i18n.TimeZone.getTimezoneFromCity(cityId); // TimeZone object corresponding to the city ID
54       let cityDisplayName = i18n.TimeZone.getCityDisplayName(cityId, "zh-CN"); // Localized city name
55       let timestamp = (new Date()).getTime()
56       let item : Item = {
57           cityDisplayName : cityDisplayName,
58           timezoneId : timezone.getID(),
59           offset : 'GMT'+ (timezone.getOffset(timestamp) / 3600*1000),
60           cityId : cityId
61       }
62       timezoneList.push(item);
63    }
64
65   // TimeZone object array corresponding to the specified geographical coordinates
66   let timezoneArray = i18n.TimeZone.getTimezonesByLocation(-43.1, -22.5)
67   for (let i =0;i < timezoneArray.length; i++) {
68       let tzId = timezoneArray[i].getID(); // America/Sao_Paulo
69   }
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 timezone1 = i18n.getTimeZone("America/Sao_Paulo");
82   let timezone2 = i18n.getTimeZone();
83   let appPreferredTimeZoneList: Array<i18n.TimeZone> = [] // Application preferred time zone list
84   appPreferredTimeZoneList.push(timezone1);
85   appPreferredTimeZoneList.push(timezone2);
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 = i18n.System.getSystemLocale();
91   for (let i = 0 ; i < appPreferredTimeZoneList.length ; i++) {
92       let timezone = appPreferredTimeZoneList[i].getID();
93       let 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 = calendar.get("year");
97       let month = calendar.get("month");
98       let day = calendar.get("date");
99       let hour = calendar.get("hour");
100       let minute = calendar.get("minute");
101       let second = calendar.get("second");
102   }
103   ```
104