提交 b53b4123 编写于 作者: E ester.zhou

Update docs (13058)

Signed-off-by: Nester.zhou <ester.zhou@huawei.com>
上级 05b03302
# @ohos.systemDateTime
The **systemDateTime** module provides system time and time zone features. You can use the APIs of this module to set and obtain the system time and time zone.
> **NOTE**
>
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```js
import systemDateTime from '@ohos.systemDateTime';
```
## systemDateTime.setTime
setTime(time : number, callback : AsyncCallback&lt;void&gt;) : void
Sets the system time. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------- | ---- | ---------------- |
| time | number | Yes | Timestamp to set, in milliseconds. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result.|
**Example**
```js
// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
systemDateTime.setTime(time, (error) => {
if (error) {
console.info(`Failed to set time. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in setting time`);
});
} catch(e) {
console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.setTime
setTime(time : number) : Promise&lt;void&gt;
Sets the system time. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------ |
| time | number | Yes | Timestamp to set, in milliseconds.|
**Return value**
| Type | Description |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
**Example**
```js
// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
systemDateTime.setTime(time).then(() => {
console.info(`Succeeded in setting time.`);
}).catch((error) => {
console.info(`Failed to set time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getCurrentTime
getCurrentTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------- | ---- | ------------------ |
| isNano | boolean | Yes | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the time elapsed since the Unix epoch. |
**Example**
```js
try {
systemDateTime.getCurrentTime(true, (error, time) => {
if (error) {
console.info(`Failed to get currentTime. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in getting currentTime : ${time}`);
});
} catch(e) {
console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getCurrentTime
getCurrentTime(callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------- | ---- | ---------------------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the time elapsed since the Unix epoch. |
**Example**
```js
try {
systemDateTime.getCurrentTime((error, time) => {
if (error) {
console.info(`Failed to get currentTime. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in getting currentTime : ${time}`);
});
} catch(e) {
console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getCurrentTime
getCurrentTime(isNano?: boolean): Promise&lt;number&gt;
Obtains the time elapsed since the Unix epoch. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------- | ---- | ------------------------- |
| isNano | boolean | No | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
**Return value**
| Type | Description |
| --------------------- | --------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since the Unix epoch.|
**Example**
```js
try {
systemDateTime.getCurrentTime().then((time) => {
console.info(`Succeeded in getting currentTime : ${time}`);
}).catch((error) => {
console.info(`Failed to get currentTime. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getRealActiveTime
getRealActiveTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------- | ---- | -------------------------- |
| isNano | boolean | Yes | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the time.|
**Example**
```js
try {
systemDateTime.getRealActiveTime(true, (error, time) => {
if (error) {
console.info(`Failed to get real active time. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in getting real active time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getRealActiveTime
getRealActiveTime(callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------- | ---- | --------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the time.|
**Example**
```js
try {
systemDateTime.getRealActiveTime((error, time) => {
if (error) {
console.info(`Failed to get real active time. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in getting real active time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getRealActiveTime
getRealActiveTime(isNano?: boolean): Promise&lt;number&gt;
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------- | ---- | ----------------------------------- |
| isNano | boolean | No | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
**Return value**
| Type | Description |
| -------------- | -------------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since system startup, excluding the deep sleep time.|
**Example**
```js
try {
systemDateTime.getRealActiveTime().then((time) => {
console.info(`Succeeded in getting real active time : ${time}`);
}).catch((error) => {
console.info(`Failed to get real active time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getRealTime
getRealTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------- | ---- | ------------------------------- |
| isNano | boolean | Yes | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the time. |
**Example**
```js
try {
systemDateTime.getRealTime(true, (error, time) => {
if (error) {
console.info(`Failed to get real time. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in getting real time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getRealTime
getRealTime(callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes | Callback used to return the time. |
**Example**
```js
try {
systemDateTime.getRealTime((error, time) => {
if (error) {
console.info(`Failed to get real time. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in getting real time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getRealTime
getRealTime(isNano?: boolean): Promise&lt;number&gt;
Obtains the time elapsed since system startup, including the deep sleep time. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------- | ---- | ------------------------------- |
| isNano | boolean | No | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
**Return value**
| Type | Description |
| --------------------- | ------------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since system startup, including the deep sleep time.|
**Example**
```js
try {
systemDateTime.getRealTime().then((time) => {
console.info(`Succeeded in getting real time : ${time}`);
}).catch((error) => {
console.info(`Failed to get real time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.setDate
setDate(date: Date, callback: AsyncCallback&lt;void&gt;): void
Sets the system date. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | --------------------- |
| date | Date | Yes | Target date to set. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result.|
**Example**
```js
let date = new Date();
try {
systemDateTime.setDate(date, (error) => {
if (error) {
console.info(`Failed to set date. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in setting date.`);
});
} catch(e) {
console.info(`Failed to set date. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.setDate
setDate(date: Date): Promise&lt;void&gt;
Sets the system date. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name| Type| Mandatory| Description |
| ------ | ---- | ---- | ---------- |
| date | Date | Yes | Target date to set.|
**Return value**
| Type | Description |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
**Example**
```js
let date = new Date();
try {
systemDateTime.setDate(date).then(() => {
console.info(`Succeeded in setting date.`);
}).catch((error) => {
console.info(`Failed to set date. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to set date. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getDate
getDate(callback: AsyncCallback&lt;Date&gt;): void
Obtains the current system date. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------- | ---- | --------------------- |
| callback | AsyncCallback&lt;Date&gt; | Yes | Callback used to return the current system date.|
**Example**
```js
try {
systemDateTime.getDate((error, date) => {
if (error) {
console.info(`Failed to get date. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in getting date : ${date}`);;
});
} catch(e) {
console.info(`Failed to get date. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getDate
getDate(): Promise&lt;Date&gt;
Obtains the current system date. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Return value**
| Type | Description |
| ------------------- | ----------------------------------------- |
| Promise&lt;Date&gt; | Promise used to return the current system date.|
**Example**
```js
try {
systemDateTime.getDate().then((date) => {
console.info(`Succeeded in getting date : ${date}`);
}).catch((error) => {
console.info(`Failed to get date. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get date. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.setTimezone
setTimezone(timezone: string, callback: AsyncCallback&lt;void&gt;): void
Sets the system time zone. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------- | ---- | -------------------------- |
| timezone | string | Yes | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones). |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result.|
**Example**
```js
try {
systemDateTime.setTimezone('Asia/Shanghai', (error) => {
if (error) {
console.info(`Failed to set timezone. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in setting timezone.`);
});
} catch(e) {
console.info(`Failed to set timezone. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.setTimezone
setTimezone(timezone: string): Promise&lt;void&gt;
Sets the system time zone. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ---------- |
| timezone | string | Yes | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones).|
**Return value**
| Type | Description |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
**Example**
```js
try {
systemDateTime.setTimezone('Asia/Shanghai').then(() => {
console.info(`Succeeded in setting timezone.`);
}).catch((error) => {
console.info(`Failed to set timezone. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to set timezone. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getTimezone
getTimezone(callback: AsyncCallback&lt;string&gt;): void
Obtains the system time zone. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------- | ---- | ------------------------ |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).|
**Example**
```js
try {
systemDateTime.getTimezone((error, data) => {
if (error) {
console.info(`Failed to get timezone. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in get timezone : ${data}`);;
});
} catch(e) {
console.info(`Failed to get timezone. message:${e.message}, code:${e.code}`);
}
```
## systemDateTime.getTimezone
getTimezone(): Promise&lt;string&gt;
Obtains the system time zone. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.Time
**Return value**
| Type | Description |
| --------------------- | ------------------------------------- |
| Promise&lt;string&gt; | Promise used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).|
**Example**
```js
try {
systemDateTime.getTimezone().then((data) => {
console.info(`Succeeded in getting timezone: ${data}`);
}).catch((error) => {
console.info(`Failed to get timezone. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get timezone. message:${e.message}, code:${e.code}`);
}
```
## Supported System Time Zones
The following table lists the supported system time zones and the respective offset (unit: h) between each time zone and time zone 0.
| Time Zone | Offset |
| ------------------------------ | --------------------- |
| Antarctica/McMurdo | 12 |
| America/Argentina/Buenos_Aires | -3 |
| Australia/Sydney | 10 |
| America/Noronha | -2 |
| America/St_Johns | -3 |
| Africa/Kinshasa | 1 |
| America/Santiago | -3 |
| Asia/Shanghai | 8 |
| Asia/Nicosia | 3 |
| Europe/Berlin | 2 |
| America/Guayaquil | -5 |
| Europe/Madrid | 2 |
| Pacific/Pohnpei | 11 |
| America/Godthab | -2 |
| Asia/Jakarta | 7 |
| Pacific/Tarawa | 12 |
| Asia/Almaty | 6 |
| Pacific/Majuro | 12 |
| Asia/Ulaanbaatar | 8 |
| America/Mexico_City | -5 |
| Asia/Kuala_Lumpur | 8 |
| Pacific/Auckland | 12 |
| Pacific/Tahiti | -10 |
| Pacific/Port_Moresby | 10 |
| Asia/Gaza | 3 |
| Europe/Lisbon | 1 |
| Europe/Moscow | 3 |
| Europe/Kiev | 3 |
| Pacific/Wake | 12 |
| America/New_York | -4 |
| Asia/Tashkent | 5 |
# System Time and Time Zone # @ohos.systemTime
The **systemTime** module provides system time and time zone features. You can use the APIs of this module to set and obtain the system time and time zone. The **systemTime** module provides system time and time zone features. You can use the APIs of this module to set and obtain the system time and time zone.
...@@ -12,7 +12,7 @@ The **systemTime** module provides system time and time zone features. You can u ...@@ -12,7 +12,7 @@ The **systemTime** module provides system time and time zone features. You can u
import systemTime from '@ohos.systemTime'; import systemTime from '@ohos.systemTime';
``` ```
## systemTime.setTime ## systemTime.setTime<sup>(deprecated)</sup>
setTime(time : number, callback : AsyncCallback&lt;void&gt;) : void setTime(time : number, callback : AsyncCallback&lt;void&gt;) : void
...@@ -34,16 +34,20 @@ Sets the system time. This API uses an asynchronous callback to return the resul ...@@ -34,16 +34,20 @@ Sets the system time. This API uses an asynchronous callback to return the resul
```js ```js
// Set the system time to 2021-01-20 02:36:25. // Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000; let time = 1611081385000;
systemTime.setTime(time, (error, data) => { try {
if (error) { systemTime.setTime(time, (error) => {
console.error(`Failed to set systemTime. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to setting time. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in setting systemTime. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in setting time`);
});
} catch(e) {
console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.setTime ## systemTime.setTime<sup>(deprecated)</sup>
setTime(time : number) : Promise&lt;void&gt; setTime(time : number) : Promise&lt;void&gt;
...@@ -70,19 +74,27 @@ Sets the system time. This API uses a promise to return the result. ...@@ -70,19 +74,27 @@ Sets the system time. This API uses a promise to return the result.
```js ```js
// Set the system time to 2021-01-20 02:36:25. // Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000; let time = 1611081385000;
systemTime.setTime(time).then((data) => { try {
console.log(`Succeeded in setting systemTime. Data:` + JSON.stringify(data)); systemTime.setTime(time).then(() => {
}).catch((error) => { console.info(`Succeeded in setting time.`);
console.error(`Failed to set systemTime. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to setting time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getCurrentTime<sup>8+</sup> ## systemTime.getCurrentTime<sup>(deprecated)</sup>
getCurrentTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void getCurrentTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result. Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -95,21 +107,29 @@ Obtains the time elapsed since the Unix epoch. This API uses an asynchronous cal ...@@ -95,21 +107,29 @@ Obtains the time elapsed since the Unix epoch. This API uses an asynchronous cal
**Example** **Example**
```js ```js
systemTime.getCurrentTime(true, (error, data) => { try {
if (error) { systemTime.getCurrentTime(true, (error, time) => {
console.error(`Failed to get systemTime. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to getting currentTime. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in getting systemTime. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in getting currentTime : ${time}`);
});
} catch(e) {
console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getCurrentTime<sup>8+</sup> ## systemTime.getCurrentTime<sup>(deprecated)</sup>
getCurrentTime(callback: AsyncCallback&lt;number&gt;): void getCurrentTime(callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result. Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime-1).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -121,21 +141,29 @@ Obtains the time elapsed since the Unix epoch. This API uses an asynchronous cal ...@@ -121,21 +141,29 @@ Obtains the time elapsed since the Unix epoch. This API uses an asynchronous cal
**Example** **Example**
```js ```js
systemTime.getCurrentTime((error, data) => { try {
if (error) { systemTime.getCurrentTime((error, time) => {
console.error(`Succeeded in getting systemTime. Data:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to getting currentTime. message:${error.message}, code:${error.code}`);
} return;
console.log(`Failed to get systemTime. Cause:` + JSON.stringify(data)); }
}); console.info(`Succeeded in getting currentTime : ${time}`);
});
} catch(e) {
console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getCurrentTime<sup>8+</sup> ## systemTime.getCurrentTime<sup>(deprecated)</sup>
getCurrentTime(isNano?: boolean): Promise&lt;number&gt; getCurrentTime(isNano?: boolean): Promise&lt;number&gt;
Obtains the time elapsed since the Unix epoch. This API uses a promise to return the result. Obtains the time elapsed since the Unix epoch. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime-2).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -153,19 +181,27 @@ Obtains the time elapsed since the Unix epoch. This API uses a promise to return ...@@ -153,19 +181,27 @@ Obtains the time elapsed since the Unix epoch. This API uses a promise to return
**Example** **Example**
```js ```js
systemTime.getCurrentTime().then((data) => { try {
console.log(`Succeeded in getting systemTime. Data:` + JSON.stringify(data)); systemTime.getCurrentTime().then((time) => {
}).catch((error) => { console.info(`Succeeded in getting currentTime : ${time}`);
console.error(`Failed to get systemTime. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to getting currentTime. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getRealActiveTime<sup>8+</sup> ## systemTime.getRealActiveTime<sup>(deprecated)</sup>
getRealActiveTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void getRealActiveTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result. Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -178,21 +214,29 @@ Obtains the time elapsed since system startup, excluding the deep sleep time. Th ...@@ -178,21 +214,29 @@ Obtains the time elapsed since system startup, excluding the deep sleep time. Th
**Example** **Example**
```js ```js
systemTime.getRealActiveTime(true, (error, data) => { try {
if (error) { systemTime.getRealActiveTime(true, (error, time) => {
console.error(`Failed to get real active time. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to getting real active time. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in getting real active time. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in getting real active time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getRealActiveTime<sup>8+</sup> ## systemTime.getRealActiveTime<sup>(deprecated)</sup>
getRealActiveTime(callback: AsyncCallback&lt;number&gt;): void getRealActiveTime(callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result. Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime-1).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -204,21 +248,29 @@ Obtains the time elapsed since system startup, excluding the deep sleep time. Th ...@@ -204,21 +248,29 @@ Obtains the time elapsed since system startup, excluding the deep sleep time. Th
**Example** **Example**
```js ```js
systemTime.getRealActiveTime((error, data) => { try {
if (error) { systemTime.getRealActiveTime((error, time) => {
console.error(`Failed to get real active time. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to getting real active time. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in getting real active time. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in getting real active time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getRealActiveTime<sup>8+</sup> ## systemTime.getRealActiveTime<sup>(deprecated)</sup>
getRealActiveTime(isNano?: boolean): Promise&lt;number&gt; getRealActiveTime(isNano?: boolean): Promise&lt;number&gt;
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses a promise to return the result. Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime-2).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -236,19 +288,27 @@ Obtains the time elapsed since system startup, excluding the deep sleep time. Th ...@@ -236,19 +288,27 @@ Obtains the time elapsed since system startup, excluding the deep sleep time. Th
**Example** **Example**
```js ```js
systemTime.getRealActiveTime().then((data) => { try {
console.log(`Succeeded in getting real active time. Data:` + JSON.stringify(data)); systemTime.getRealActiveTime().then((time) => {
}).catch((error) => { console.info(`Succeeded in getting real active time : ${time}`);
console.error(`Failed to get real active time. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to getting real active time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getRealTime<sup>8+</sup> ## systemTime.getRealTime<sup>(deprecated)</sup>
getRealTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void getRealTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result. Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -261,21 +321,29 @@ Obtains the time elapsed since system startup, including the deep sleep time. Th ...@@ -261,21 +321,29 @@ Obtains the time elapsed since system startup, including the deep sleep time. Th
**Example** **Example**
```js ```js
systemTime.getRealTime(true, (error, data) => { try {
if (error) { systemTime.getRealTime(true, (error, time) => {
console.error(`Failed to get real time. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to getting real time. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in getting real time. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in getting real time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getRealTime<sup>8+</sup> ## systemTime.getRealTime<sup>(deprecated)</sup>
getRealTime(callback: AsyncCallback&lt;number&gt;): void getRealTime(callback: AsyncCallback&lt;number&gt;): void
Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result. Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime-1).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -287,28 +355,36 @@ Obtains the time elapsed since system startup, including the deep sleep time. Th ...@@ -287,28 +355,36 @@ Obtains the time elapsed since system startup, including the deep sleep time. Th
**Example** **Example**
```js ```js
systemTime.getRealTime((error, data) => { try {
if (error) { systemTime.getRealTime((error, time) => {
console.error(`Failed to get real time. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to getting real time. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in getting real time. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in getting real time : ${time}`);
});
} catch(e) {
console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getRealTime<sup>8+</sup> ## systemTime.getRealTime<sup>(deprecated)</sup>
getRealTime(isNano?: boolean): Promise&lt;number&gt; getRealTime(isNano?: boolean): Promise&lt;number&gt;
Obtains the time elapsed since system startup, including the deep sleep time. This API uses a promise to return the result. Obtains the time elapsed since system startup, including the deep sleep time. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime-2).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------- | ---- | ------------------------------- | | ------ | ------- | ---- | ------------------------------- |
| isNano | boolean | No | Whether the time to return is in nanoseconds.<<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).| | isNano | boolean | No | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).|
**Return value** **Return value**
...@@ -319,14 +395,18 @@ Obtains the time elapsed since system startup, including the deep sleep time. Th ...@@ -319,14 +395,18 @@ Obtains the time elapsed since system startup, including the deep sleep time. Th
**Example** **Example**
```js ```js
systemTime.getRealTime().then((data) => { try {
console.log(`Succeeded in getting real time. Data:` + JSON.stringify(data)); systemTime.getRealTime().then((time) => {
}).catch((error) => { console.info(`Succeeded in getting real time : ${time}`);
console.error(`Failed to get real time. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to getting real time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.setDate ## systemTime.setDate<sup>(deprecated)</sup>
setDate(date: Date, callback: AsyncCallback&lt;void&gt;): void setDate(date: Date, callback: AsyncCallback&lt;void&gt;): void
...@@ -346,17 +426,21 @@ Sets the system date. This API uses an asynchronous callback to return the resul ...@@ -346,17 +426,21 @@ Sets the system date. This API uses an asynchronous callback to return the resul
**Example** **Example**
```js ```js
let data = new Date(); let date = new Date();
systemTime.setDate(data,(error, data) => { try {
if (error) { systemTime.setDate(date, (error) => {
console.error('Failed to set system date. Cause:' + JSON.stringify(error)); if (error) {
return; console.info(`Failed to setting date. message:${error.message}, code:${error.code}`);
} return;
console.info('Succeeded in setting system date. Data:' + JSON.stringify(data)); }
}); console.info(`Succeeded in setting date.`);
});
} catch(e) {
console.info(`Failed to set date. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.setDate ## systemTime.setDate<sup>(deprecated)</sup>
setDate(date: Date): Promise&lt;void&gt; setDate(date: Date): Promise&lt;void&gt;
...@@ -381,20 +465,28 @@ Sets the system date. This API uses a promise to return the result. ...@@ -381,20 +465,28 @@ Sets the system date. This API uses a promise to return the result.
**Example** **Example**
```js ```js
let data = new Date(); let date = new Date();
systemTime.setDate(data).then((value) => { try {
console.log(`Succeeded in setting system date. Data:` + JSON.stringify(value)); systemTime.setDate(date).then(() => {
}).catch((error) => { console.info(`Succeeded in setting date.`);
console.error(`Failed to set system date. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to setting date. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to set date. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getDate<sup>8+</sup> ## systemTime.getDate<sup>(deprecated)</sup>
getDate(callback: AsyncCallback&lt;Date&gt;): void getDate(callback: AsyncCallback&lt;Date&gt;): void
Obtains the current system date. This API uses an asynchronous callback to return the result. Obtains the current system date. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getDate](./js-apis-system-date-time.md#systemdatetimegetdate).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -406,21 +498,29 @@ Obtains the current system date. This API uses an asynchronous callback to retur ...@@ -406,21 +498,29 @@ Obtains the current system date. This API uses an asynchronous callback to retur
**Example** **Example**
```js ```js
systemTime.getDate((error, data) => { try {
if (error) { systemTime.getDate((error, date) => {
console.error(`Failed to get system date. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to get date. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in getting system date. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in get date : ${date}`);;
});
} catch(e) {
console.info(`Failed to get date. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getDate<sup>8+</sup> ## systemTime.getDate<sup>(deprecated)</sup>
getDate(): Promise&lt;Date&gt; getDate(): Promise&lt;Date&gt;
Obtains the current system date. This API uses a promise to return the result. Obtains the current system date. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getDate](./js-apis-system-date-time.md#systemdatetimegetdate-1).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Return value** **Return value**
...@@ -432,14 +532,18 @@ Obtains the current system date. This API uses a promise to return the result. ...@@ -432,14 +532,18 @@ Obtains the current system date. This API uses a promise to return the result.
**Example** **Example**
```js ```js
systemTime.getDate().then((data) => { try {
console.log(`Succeeded in getting system date. Data:` + JSON.stringify(data)); systemTime.getDate().then((date) => {
}).catch((error) => { console.info(`Succeeded in getting date : ${date}`);
console.error(`Failed to get system date. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to getting date. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get date. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.setTimezone ## systemTime.setTimezone<sup>(deprecated)</sup>
setTimezone(timezone: string, callback: AsyncCallback&lt;void&gt;): void setTimezone(timezone: string, callback: AsyncCallback&lt;void&gt;): void
...@@ -459,16 +563,20 @@ Sets the system time zone. This API uses an asynchronous callback to return the ...@@ -459,16 +563,20 @@ Sets the system time zone. This API uses an asynchronous callback to return the
**Example** **Example**
```js ```js
systemTime.setTimezone('Asia/Shanghai', (error, data) => { try {
if (error) { systemTime.setTimezone('Asia/Shanghai', (error) => {
console.error('Failed to set system time zone. Cause:' + JSON.stringify(error)); if (error) {
return; console.info(`Failed to setting timezone. message:${error.message}, code:${error.code}`);
} return;
console.info('Succeeded in setting system time zone. Data:' + JSON.stringify(data)); }
}); console.info(`Succeeded in setting timezone.`);
});
} catch(e) {
console.info(`Failed to set timezone. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.setTimezone ## systemTime.setTimezone<sup>(deprecated)</sup>
setTimezone(timezone: string): Promise&lt;void&gt; setTimezone(timezone: string): Promise&lt;void&gt;
...@@ -493,19 +601,27 @@ Sets the system time zone. This API uses a promise to return the result. ...@@ -493,19 +601,27 @@ Sets the system time zone. This API uses a promise to return the result.
**Example** **Example**
```js ```js
systemTime.setTimezone('Asia/Shanghai').then((data) => { try {
console.log(`Succeeded in setting system time zone. Data:` + JSON.stringify(data)); systemTime.setTimezone('Asia/Shanghai').then(() => {
}).catch((error) => { console.info(`Succeeded in setting timezone.`);
console.error(`Failed to set system time zone. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to setting timezone. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to set timezone. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getTimezone<sup>8+</sup> ## systemTime.getTimezone<sup>(deprecated)</sup>
getTimezone(callback: AsyncCallback&lt;string&gt;): void getTimezone(callback: AsyncCallback&lt;string&gt;): void
Obtains the system time zone. This API uses an asynchronous callback to return the result. Obtains the system time zone. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getTimezone](./js-apis-system-date-time.md#systemdatetimegettimezone).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -517,21 +633,29 @@ Obtains the system time zone. This API uses an asynchronous callback to return t ...@@ -517,21 +633,29 @@ Obtains the system time zone. This API uses an asynchronous callback to return t
**Example** **Example**
```js ```js
systemTime.getTimezone((error, data) => { try {
if (error) { systemTime.getTimezone((error, data) => {
console.error(`Failed to get system time zone. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to get timezone. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in getting system time zone. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in get timezone : ${data}`);;
});
} catch(e) {
console.info(`Failed to get timezone. message:${e.message}, code:${e.code}`);
}
``` ```
## systemTime.getTimezone<sup>8+</sup> ## systemTime.getTimezone<sup>(deprecated)</sup>
getTimezone(): Promise&lt;string&gt; getTimezone(): Promise&lt;string&gt;
Obtains the system time zone. This API uses a promise to return the result. Obtains the system time zone. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getTimezone](./js-apis-system-date-time.md#systemdatetimegettimezone-1).
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Return value** **Return value**
...@@ -543,11 +667,15 @@ Obtains the system time zone. This API uses a promise to return the result. ...@@ -543,11 +667,15 @@ Obtains the system time zone. This API uses a promise to return the result.
**Example** **Example**
```js ```js
systemTime.getTimezone().then((data) => { try {
console.log(`Succeeded in getting system time zone. Data:` + JSON.stringify(data)); systemTime.getTimezone().then((data) => {
}).catch((error) => { console.info(`Succeeded in getting timezone: ${data}`);
console.error(`Failed to get system time zone. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to getting timezone. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to get timezone. message:${e.message}, code:${e.code}`);
}
``` ```
## Supported System Time Zones ## Supported System Time Zones
......
# System Timer # @ohos.systemTimer
The **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services. The **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services.
...@@ -33,13 +33,13 @@ Defines the initialization options for **createTimer**. ...@@ -33,13 +33,13 @@ Defines the initialization options for **createTimer**.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | --------------------------------- | ---- | ------------------------------------------------------------ | | --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| type | number | Yes | Timer type.<br>**1**: CPU time type. The start time of the timer cannot be later than the current system time.<br>**2**: wakeup type.<br>**4**: exact type.<br>**5**: idle type (not supported currently).| | type | number | Yes | Timer type.<br>**1**: CPU time type. (The start time of the timer cannot be later than the current system time.)<br>**2**: wakeup type.<br>**4**: exact type.<br>**8**: idle type (not supported currently).|
| repeat | boolean | Yes | Whether the timer is a repeating timer. The value **true** means that the timer is a repeating timer, and **false** means that the timer is a one-shot timer. | | repeat | boolean | Yes | Whether the timer is a repeating timer. The value **true** means that the timer is a repeating timer, and **false** means that the timer is a one-shot timer. |
| interval | number | No | Repeat interval. For a repeating timer, the value must be greater than 5000 ms. For a one-shot timer, the value is **0**.| | interval | number | No | Repeat interval. For a repeating timer, the value must be greater than 5000 ms. For a one-shot timer, the value is **0**.|
| wantAgent | [WantAgent](js-apis-wantAgent.md) | No | **WantAgent** object of the notification to be sent when the timer expires. (An application MainAbility can be started, but not a Service ability.)| | wantAgent | [WantAgent](js-apis-app-ability-wantAgent.md) | No | **WantAgent** object of the notification to be sent when the timer expires. (An application MainAbility can be started, but not a Service ability.)|
| callback | number | Yes | Callback used to return the timer ID. | | callback | number | Yes | Callback used to return the timer ID. |
## systemTimer.createTimer ## systemTimer.createTimer
...@@ -48,6 +48,8 @@ createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void ...@@ -48,6 +48,8 @@ createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void
Creates a timer. This API uses an asynchronous callback to return the result. Creates a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -66,13 +68,17 @@ export default { ...@@ -66,13 +68,17 @@ export default {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat: false repeat: false
}; };
systemTimer.createTimer(options, (error, data) => { try {
if (error) { systemTimer.createTimer(options, (error) => {
console.error(`Failed to create timer. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to create timer. message:${error.message}, code:${error.code}`);
} return;
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data)); }
}); console.info(`Succeeded in creating timer.`);
});
} catch(e) {
console.info(`Failed to create timer. message:${e.message}, code:${e.code}`);
}
} }
} }
``` ```
...@@ -83,6 +89,8 @@ createTimer(options: TimerOptions): Promise&lt;number&gt; ...@@ -83,6 +89,8 @@ createTimer(options: TimerOptions): Promise&lt;number&gt;
Creates a timer. This API uses a promise to return the result. Creates a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -105,12 +113,16 @@ export default { ...@@ -105,12 +113,16 @@ export default {
let options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
}; };
systemTimer.createTimer(options).then((data) => { try {
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data)); systemTimer.createTimer(options).then(() => {
}).catch((error) => { console.info(`Succeeded in creating timer.`);
console.error(`Failed to create timer. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to create timer. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to create timer. message:${e.message}, code:${e.code}`);
}
} }
} }
``` ```
...@@ -121,6 +133,8 @@ startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&g ...@@ -121,6 +133,8 @@ startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&g
Starts a timer. This API uses an asynchronous callback to return the result. Starts a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -143,12 +157,17 @@ export default { ...@@ -143,12 +157,17 @@ export default {
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime, (error) => { try {
if (error) { systemTimer.startTimer(timerId, triggerTime, (error) => {
console.error(`Failed to start timer. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to start timer. message:${error.message}, code:${error.code}`);
} return;
}); }
console.info(`Succeeded in starting timer.`);
});
} catch(e) {
console.info(`Failed to start timer. message:${e.message}, code:${e.code}`);
}
} }
} }
``` ```
...@@ -159,6 +178,8 @@ startTimer(timer: number, triggerTime: number): Promise&lt;void&gt; ...@@ -159,6 +178,8 @@ startTimer(timer: number, triggerTime: number): Promise&lt;void&gt;
Starts a timer. This API uses a promise to return the result. Starts a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -186,11 +207,15 @@ export default { ...@@ -186,11 +207,15 @@ export default {
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime).then((data) => { try {
console.log(`Succeeded in startting timer. Data:` + JSON.stringify(data)); systemTimer.startTimer(timerId, triggerTime).then(() => {
}).catch((error) => { console.info(`Succeeded in starting timer.`);
console.error(`Failed to start timer. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to start timer. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to start timer. message:${e.message}, code:${e.code}`);
}
} }
} }
``` ```
...@@ -201,6 +226,8 @@ stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void ...@@ -201,6 +226,8 @@ stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
Stops a timer. This API uses an asynchronous callback to return the result. Stops a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -214,22 +241,27 @@ Stops a timer. This API uses an asynchronous callback to return the result. ...@@ -214,22 +241,27 @@ Stops a timer. This API uses an asynchronous callback to return the result.
```js ```js
export default { export default {
async systemTimer () { async systemTimer () {
let options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId, (error) => { try {
if (error) { systemTimer.stopTimer(timerId, (error) => {
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to stop timer. message:${error.message}, code:${error.code}`);
} return;
}); }
} console.info(`Succeeded in stopping timer.`);
});
} catch(e) {
console.info(`Failed to stop timer. message:${e.message}, code:${e.code}`);
}
}
} }
``` ```
...@@ -239,6 +271,8 @@ stopTimer(timer: number): Promise&lt;void&gt; ...@@ -239,6 +271,8 @@ stopTimer(timer: number): Promise&lt;void&gt;
Stops a timer. This API uses a promise to return the result. Stops a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -257,21 +291,25 @@ Stops a timer. This API uses a promise to return the result. ...@@ -257,21 +291,25 @@ Stops a timer. This API uses a promise to return the result.
```js ```js
export default { export default {
async systemTimer (){ async systemTimer (){
let options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = await systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId).then((data) => { try {
console.log(`Succeeded in stopping timer. Data:` + JSON.stringify(data)); systemTimer.stopTimer(timerId).then(() => {
}).catch((error) => { console.info(`Succeeded in stopping timer.`);
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to stop timer. message:${error.message}, code:${error.code}`);
} });
} catch(e) {
console.info(`Failed to stop timer. message:${e.message}, code:${e.code}`);
}
}
} }
``` ```
...@@ -281,6 +319,8 @@ destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void ...@@ -281,6 +319,8 @@ destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
Destroys a timer. This API uses an asynchronous callback to return the result. Destroys a timer. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -304,12 +344,17 @@ export default { ...@@ -304,12 +344,17 @@ export default {
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId) systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId, (error) => { try {
if (error) { systemTimer.destroyTimer(timerId, (error) => {
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error)); if (error) {
return; console.info(`Failed to destroy timer. message:${error.message}, code:${error.code}`);
} return;
}); }
console.info(`Succeeded in destroying timer.`);
});
} catch(e) {
console.info(`Failed to destroying timer. message:${e.message}, code:${e.code}`);
}
} }
} }
``` ```
...@@ -320,6 +365,8 @@ destroyTimer(timer: number): Promise&lt;void&gt; ...@@ -320,6 +365,8 @@ destroyTimer(timer: number): Promise&lt;void&gt;
Destroys a timer. This API uses a promise to return the result. Destroys a timer. This API uses a promise to return the result.
**System API**: This is a system API.
**System capability**: SystemCapability.MiscServices.Time **System capability**: SystemCapability.MiscServices.Time
**Parameters** **Parameters**
...@@ -348,11 +395,15 @@ export default { ...@@ -348,11 +395,15 @@ export default {
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId) systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId).then((data) => { try {
console.log(`Succeeded in destroying timer. Data:` + JSON.stringify(data)); systemTimer.destroyTimer(timerId).then(() => {
}).catch((error) => { console.info(`Succeeded in destroying timer.`);
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error)); }).catch((error) => {
}); console.info(`Failed to destroy timer. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to destroying timer. message:${e.message}, code:${e.code}`);
}
} }
} }
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册