• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# DST Transition
2
3
4## Use Cases
5
6DST is a local time system that sees manual adjustment to the time with the aim of saving energy in certain countries/regions. When DST is enabled, the time is usually advanced by a certain period compared to the standard time.
7
8
9## How It Works
10
11When the system time reaches the DST transition point, DST transition is automatically implemented based on the DST transition rules configured in the system. If an application uses a standard TS API, for example, `Date()`, to obtain and display the time, it also displays the DST time when the DST transition time is reached.
12
13The DST transition rules are as follows:
14
151. Calculate the number of hours in a day.
16   The number of hours in a day changes on the day of DST transition. In most countries, there are 23 hours on the day when DST starts and there are 25 hours on the day when the DST ends.
17
18   Calculate the number of hours between the same clock time before and after the DST transition. The sample code is as follows:
19   ```ts
20   import { i18n } from '@kit.LocalizationKit';
21
22   let calendar: i18n.Calendar = i18n.getCalendar('zh-Hans');
23   calendar.setTimeZone('Europe/London');
24   calendar.set (2021, 2, 27, 16, 0, 0); // Time before the DST starts
25   let startTime: number = calendar.getTimeInMillis();
26   calendar.set (2021, 2, 28, 16, 0, 0); // Time in the DST period
27   let finishTime: number = calendar.getTimeInMillis();
28   let hours: number = (finishTime - startTime) / (3600 * 1000); // hours = 23
29   ```
30
312. Store and display time data.
32   Store and display time data according to the local DST timing rules. The time gap and repetition caused by DST transition need to be processed.
33
34   The transition into DST will cause a period of time gap, for example, transition from 1:59:59 to 3:00:00. The transition out of DST will cause a period of time overlap, for example, rollback from 3:59:59 to 3:00:00.
35
36   It is recommended that the DST flag be added to the local time when DST is active.
37
38   ![DST flag](figures/dst-flag.png)
39
403. Store and transmit time data.
41   You are advised to use the standard time (UTC or GMT) of time zone 0 for time data storage and transmission. This helps prevent data loss or errors caused by DST transition.
42