1cf69771bSopenharmony_ci# Timing and Time
2cf69771bSopenharmony_ci
3cf69771bSopenharmony_ci## Introduction
4cf69771bSopenharmony_ci
5cf69771bSopenharmony_ciThe timing and time module provides APIs for managing the system time.
6cf69771bSopenharmony_ci
7cf69771bSopenharmony_ci**Figure  1**  Subsystem architecture 
8cf69771bSopenharmony_ci
9cf69771bSopenharmony_ci![](figures/subsystem_architecture.png "subsystem-architecture")
10cf69771bSopenharmony_ci
11cf69771bSopenharmony_ci## Directory Structure
12cf69771bSopenharmony_ci
13cf69771bSopenharmony_ci```
14cf69771bSopenharmony_ci/base/time/time_service
15cf69771bSopenharmony_ci├── etc                      # Process configuration files
16cf69771bSopenharmony_ci├── figures                  # Architecture diagram
17cf69771bSopenharmony_ci├── framework/js/napi        # the js interface resolves to the napi interface
18cf69771bSopenharmony_ci├── interfaces/inner_api     # external interface code provided by the component
19cf69771bSopenharmony_ci├── services                 # time service realization
20cf69771bSopenharmony_ci│   └── sa_profile           # module contains the config files of system services and processes
21cf69771bSopenharmony_ci├── test                     # unit test of interface
22cf69771bSopenharmony_ci└── utils                    # module contains log printing and constants for ordered commonEvent
23cf69771bSopenharmony_ci```
24cf69771bSopenharmony_ci
25cf69771bSopenharmony_ci
26cf69771bSopenharmony_ci## Usage
27cf69771bSopenharmony_ci
28cf69771bSopenharmony_ci### Available JS APIs
29cf69771bSopenharmony_ci
30cf69771bSopenharmony_ci**Table  1**  Major functions of systemTime
31cf69771bSopenharmony_ci
32cf69771bSopenharmony_ci| Interface name                                               | describe                                                     |
33cf69771bSopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ |
34cf69771bSopenharmony_ci| setTime(time : number) : Promise<boolean>                    | Set the system time (1970-01-01 to the present in milliseconds), Promise method. |
35cf69771bSopenharmony_ci| setTime(time : number, callback : AsyncCallback<boolean>) : void | Set the system time (1970-01-01 to the present in milliseconds), callback mode. |
36cf69771bSopenharmony_ci| setDate(date: Date, callback: AsyncCallback<boolean>): void; | Set the system time (Date format), Promise method.           |
37cf69771bSopenharmony_ci| setDate(date: Date): Promise<boolean>                        | Set system time (Date format), callback method.              |
38cf69771bSopenharmony_ci| setTimezone(timezone: string, callback: AsyncCallback<boolean>): void | Set the system time zone, callback method.                   |
39cf69771bSopenharmony_ci| setTimezone(timezone: string): Promise<boolean>              | Set the system time zone, Promise method.                    |
40cf69771bSopenharmony_ci
41cf69771bSopenharmony_ci**表 2** Major functions of systemTimer
42cf69771bSopenharmony_ci
43cf69771bSopenharmony_ci| Interface name                                               | describe                              |
44cf69771bSopenharmony_ci| ------------------------------------------------------------ | ------------------------------------- |
45cf69771bSopenharmony_ci| createTimer(options: TimerOptions, callback: AsyncCallback<number>): void | Create timer, callback method         |
46cf69771bSopenharmony_ci| createTimer(options: TimerOptions): Promise<number>          | Create timer, promise method          |
47cf69771bSopenharmony_ci| startTimer(timer: number, triggerTime: number, callback: AsyncCallback<boolean>): void | Start the timer, callback mode        |
48cf69771bSopenharmony_ci| startTimer(timer: number, triggerTime: number): Promise<boolean> | Start the timer, promise mode         |
49cf69771bSopenharmony_ci| stopTimer(timer: number, callback: AsyncCallback<boolean>):  void | Stop the timer, callback mode         |
50cf69771bSopenharmony_ci| stopTimer(timer: number): Promise<boolean>                   | Stop the timer, promise mode          |
51cf69771bSopenharmony_ci| destroyTimer(timer: number, callback: AsyncCallback<boolean>): void | Destroy the timer, callback method    |
52cf69771bSopenharmony_ci| destroyTimer(timer: number): Promise<boolean>                | Destroy the timer, the promise method |
53cf69771bSopenharmony_ci
54cf69771bSopenharmony_ci**表 3**  parameter TimerOptions description of systemTimer
55cf69771bSopenharmony_ci
56cf69771bSopenharmony_ci| name      | type      | illustrate                                                   |
57cf69771bSopenharmony_ci| --------- | --------- | ------------------------------------------------------------ |
58cf69771bSopenharmony_ci| type      | number    | Timer type. <br/>If the value is 1, it is represented as the system startup time timer (the timer start time cannot be later than the currently set system time); <br/>If the value is 2, it is indicated as a wake-up timer; <br/>When the value is 4, it is represented as a precision timer; <br/>If the value is 5, it is represented as an IDLE mode timer (not supported). |
59cf69771bSopenharmony_ci| repeat    | boolean   | true Is a cyclic timer, false is a single timer.             |
60cf69771bSopenharmony_ci| interval  | number    | If it is a cyclic timer, the repeat value should be greater than 5000 milliseconds, and the non-repeated timer is set to 0. |
61cf69771bSopenharmony_ci| wantAgent | wantAgent | Set the wantagent to notify, and notify when the timer expires. |
62cf69771bSopenharmony_ci| callback  | => void   | Set the callback function, which will be triggered after the timer expires. |
63cf69771bSopenharmony_ci
64cf69771bSopenharmony_ci### Sample Code
65cf69771bSopenharmony_ci
66cf69771bSopenharmony_ciExample fo using systemTime
67cf69771bSopenharmony_ci
68cf69771bSopenharmony_ci```javascript
69cf69771bSopenharmony_ci// Import the module.
70cf69771bSopenharmony_ciimport systemTime from '@ohos.systemTime';
71cf69771bSopenharmony_ci
72cf69771bSopenharmony_ci// Set the system time asynchronously with a Promise.
73cf69771bSopenharmony_civar time = 1611081385000;   
74cf69771bSopenharmony_cisystemTime.setTime(time).then((value) => {        
75cf69771bSopenharmony_ci    console.log(`success to systemTime.setTime: ${value}`);   
76cf69771bSopenharmony_ci}).catch((err) => {        
77cf69771bSopenharmony_ci    console.error(`failed to systemTime.setTime because ${err.message}`)  
78cf69771bSopenharmony_ci});
79cf69771bSopenharmony_ci
80cf69771bSopenharmony_ci// Set the system time asynchronously with a callback.   
81cf69771bSopenharmony_civar time = 1611081385000;   
82cf69771bSopenharmony_cisystemTime.setTime(time, (err, value) => {   
83cf69771bSopenharmony_ci    if (err) {        
84cf69771bSopenharmony_ci        console.error(`failed to systemTime.setTime because ${err.message}`);   
85cf69771bSopenharmony_ci        return;   
86cf69771bSopenharmony_ci    }    
87cf69771bSopenharmony_ci    console.log(`success to systemTime.setTime: ${value}`);   
88cf69771bSopenharmony_ci});
89cf69771bSopenharmony_ci```
90cf69771bSopenharmony_ciExample fo using systemTimer
91cf69771bSopenharmony_ci```javascript
92cf69771bSopenharmony_ci// Import the module 
93cf69771bSopenharmony_ciimport systemTimer from '@ohos.systemTimer';
94cf69771bSopenharmony_ci
95cf69771bSopenharmony_ciconsole.log("start");
96cf69771bSopenharmony_civar options:TimerOptions{   
97cf69771bSopenharmony_ci   type:TIMER_TYPE_REALTIME,   
98cf69771bSopenharmony_ci   repeat:false,   
99cf69771bSopenharmony_ci   interval:Number.MAX_VALUE/2,   
100cf69771bSopenharmony_ci   persistent:false   
101cf69771bSopenharmony_ci}
102cf69771bSopenharmony_ci
103cf69771bSopenharmony_ciconsole.log("create timer")   
104cf69771bSopenharmony_cilet timerId = systemTimer.Timer(options)     
105cf69771bSopenharmony_ciconsole.log("start timer")   
106cf69771bSopenharmony_cilet startTimerRes = systemTimer.startTimer(timerId, 100000)   
107cf69771bSopenharmony_ciconsole.log("stop timer")   
108cf69771bSopenharmony_cilet stopTimerRes = systemTimer.stopTimer(timerId)   
109cf69771bSopenharmony_ciconsole.log("destroy timer")   
110cf69771bSopenharmony_cilet destroyTimerRes = systemTimer.destroyTimer(timerId)   
111cf69771bSopenharmony_ciconsole.log('end');   
112cf69771bSopenharmony_ci```
113cf69771bSopenharmony_ci
114cf69771bSopenharmony_ci## Repositories Involved
115cf69771bSopenharmony_ci
116cf69771bSopenharmony_ci**Time/Timezone subsystem**
117cf69771bSopenharmony_ci
118cf69771bSopenharmony_ci[time\_time\_service](https://gitee.com/openharmony/time_time_service/tree/master/)
119cf69771bSopenharmony_ci
120