提交 c67750ed 编写于 作者: W wangtong

adapt time d.ts

Signed-off-by: Nwangtong <wangtong94@huawei.com>
上级 a73884fe
# @ohos.systemDateTime (系统时间、时区)
本模块主要由系统时间和系统时区功能组成。开发者可以设置、获取系统时间及系统时区。
> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import systemDateTime from '@ohos.systemDateTime';
```
## systemDateTime.setTime
setTime(time : number, callback : AsyncCallback&lt;void&gt;) : void
设置系统时间,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------- | ---- | ---------------- |
| time | number | 是 | 目标时间戳(ms)。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:**
```js
// time对应的时间为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;
设置系统时间,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------------------ |
| time | number | 是 | 目标时间戳(ms)。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
**示例:**
```js
// time对应的时间为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
获取自Unix纪元以来经过的时间,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------- | ---- | ------------------ |
| isNano | boolean | 是 | 返回结果是否为纳秒数。<br>- true:表示返回结果为纳秒数(ns)。 <br>- false:表示返回结果为毫秒数(ms)。 |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回自Unix纪元以来经过的时间。 |
**示例:**
```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
获取自Unix纪元以来经过的时间,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------- | ---- | ---------------------------------- |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回自Unix纪元以来经过的时间。 |
**示例:**
```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;
获取自Unix纪元以来经过的时间,使用Promise异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | ------------------------- |
| isNano | boolean | 否 | 返回结果是否为纳秒数。<br/>- true:表示返回结果为纳秒数(ns)。 <br/>- false:表示返回结果为毫秒数(ms)。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | --------------------------- |
| Promise&lt;number&gt; | Promise对象,返回自Unix纪元以来经过的时间。 |
**示例:**
```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
获取自系统启动以来经过的时间,不包括深度睡眠时间,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------- | ---- | -------------------------- |
| isNano | boolean | 是 | 返回结果是否为纳秒数。<br/>- true:表示返回结果为纳秒数(ns)。 <br/>- false:表示返回结果为毫秒数(ms)。 |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回自系统启动以来经过的时间,但不包括度睡眠时间。 |
**示例:**
```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
获取自系统启动以来经过的时间,不包括深度睡眠时间,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------- | ---- | --------------------- |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回自系统启动以来经过的时间,但不包括度睡眠时间。 |
**示例:**
```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;
获取自系统启动以来经过的时间,不包括深度睡眠时间,使用Promise异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | ----------------------------------- |
| isNano | boolean | 否 | 返回结果是否为纳秒数。<br/>- true:表示返回结果为纳秒数(ns)。 <br/>- false:表示返回结果为毫秒数(ms)。 |
**返回值:**
| 类型 | 说明 |
| -------------- | -------------------------------- |
| Promise&lt;number&gt; | Promise对象,返回自系统启动以来经过的时间,但不包括深度睡眠时间。 |
**示例:**
```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
获取自系统启动以来经过的时间,包括深度睡眠时间,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | ------------------------------- |
| isNano | boolean | 是 | 返回结果是否为纳秒数。<br/>- true:表示返回结果为纳秒数(ns)。 <br/>- false:表示返回结果为毫秒数(ms)。 |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回自系统启动以来经过的时间,包括深度睡眠时间。 |
**示例:**
```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
获取自系统启动以来经过的时间,包括深度睡眠时间,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回自系统启动以来经过的时间,包括深度睡眠时间。 |
**示例:**
```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;
获取自系统启动以来经过的时间,包括深度睡眠时间,使用Promise异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | ------------------------------- |
| isNano | boolean | 否 | 返回结果是否为纳秒数。<br/>- true:表示返回结果为纳秒数(ns)。 <br/>- false:表示返回结果为毫秒数(ms)。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------------- |
| Promise&lt;number&gt; | Promise对象,返回自系统启动以来经过的时间,包括深度睡眠时间。 |
**示例:**
```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
设置系统日期,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | --------------------- |
| date | Date | 是 | 目标日期。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:**
```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;
设置系统日期,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ---- | ---- | ---------- |
| date | Date | 是 | 目标日期。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
**示例:**
```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
获取当前系统日期,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------- | ---- | --------------------- |
| callback | AsyncCallback&lt;Date&gt; | 是 | 回调函数,返回当前系统日期。 |
**示例:**
```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;
获取当前系统日期,使用Promise异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**返回值:**
| 类型 | 说明 |
| ------------------- | ----------------------------------------- |
| Promise&lt;Date&gt; | Promise对象,返回当前系统日期。 |
**示例:**
```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
设置系统时区,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | -------------------------- |
| timezone | string | 是 | 系统时区。 具体可见[支持的系统时区](#支持的系统时区) 。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:**
```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;
设置系统时区,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------ | ---- | ---------- |
| timezone | string | 是 | 系统时区。具体可见[支持的系统时区](#支持的系统时区) 。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
**示例:**
```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
获取系统时区,使用callback异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------- | ---- | ------------------------ |
| callback | AsyncCallback&lt;string&gt; | 是 | 回调函数,返回系统时区。具体可见[支持的系统时区](#支持的系统时区) 。 |
**示例:**
```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;
获取系统时区,使用Promise异步回调。
**系统能力:** SystemCapability.MiscServices.Time
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------------------- |
| Promise&lt;string&gt; | Promise对象,返回系统时区。具体可见[支持的系统时区](#支持的系统时区) 。 |
**示例:**
```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}`);
}
```
## 支持的系统时区
支持的系统时区及各时区与0时区相比的偏移量(单位:h)可见下表。
| 时区 | 偏移量 |
| ------------------------------ | --------------------- |
| 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 |
\ No newline at end of file
......@@ -12,7 +12,7 @@
import systemTime from '@ohos.systemTime';
```
## systemTime.setTime
## systemTime.setTime<sup>(deprecated)</sup>
setTime(time : number, callback : AsyncCallback&lt;void&gt;) : void
......@@ -34,16 +34,20 @@ setTime(time : number, callback : AsyncCallback&lt;void&gt;) : void
```js
// time对应的时间为2021-01-20 02:36:25
let time = 1611081385000;
systemTime.setTime(time, (error, data) => {
if (error) {
console.error(`Failed to set systemTime. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in setting systemTime. Data:` + JSON.stringify(data));
});
try {
systemTime.setTime(time, (error) => {
if (error) {
console.info(`Failed to setting 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}`);
}
```
## systemTime.setTime
## systemTime.setTime<sup>(deprecated)</sup>
setTime(time : number) : Promise&lt;void&gt;
......@@ -70,19 +74,27 @@ setTime(time : number) : Promise&lt;void&gt;
```js
// time对应的时间为2021-01-20 02:36:25
let time = 1611081385000;
systemTime.setTime(time).then((data) => {
console.log(`Succeeded in setting systemTime. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to set systemTime. Cause:` + JSON.stringify(error));
});
try {
systemTime.setTime(time).then(() => {
console.info(`Succeeded in setting time.`);
}).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
获取自Unix纪元以来经过的时间,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -95,21 +107,29 @@ getCurrentTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
**示例:**
```js
systemTime.getCurrentTime(true, (error, data) => {
if (error) {
console.error(`Failed to get systemTime. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in getting systemTime. Data:` + JSON.stringify(data));
});
try {
systemTime.getCurrentTime(true, (error, time) => {
if (error) {
console.info(`Failed to getting 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}`);
}
```
## systemTime.getCurrentTime<sup>8+</sup>
## systemTime.getCurrentTime<sup>(deprecated)</sup>
getCurrentTime(callback: AsyncCallback&lt;number&gt;): void
获取自Unix纪元以来经过的时间,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime-1)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -121,21 +141,29 @@ getCurrentTime(callback: AsyncCallback&lt;number&gt;): void
**示例:**
```js
systemTime.getCurrentTime((error, data) => {
if (error) {
console.error(`Succeeded in getting systemTime. Data:` + JSON.stringify(error));
return;
}
console.log(`Failed to get systemTime. Cause:` + JSON.stringify(data));
});
try {
systemTime.getCurrentTime((error, time) => {
if (error) {
console.info(`Failed to getting 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}`);
}
```
## systemTime.getCurrentTime<sup>8+</sup>
## systemTime.getCurrentTime<sup>(deprecated)</sup>
getCurrentTime(isNano?: boolean): Promise&lt;number&gt;
获取自Unix纪元以来经过的时间,使用Promise异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime-2)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -153,19 +181,27 @@ getCurrentTime(isNano?: boolean): Promise&lt;number&gt;
**示例:**
```js
systemTime.getCurrentTime().then((data) => {
console.log(`Succeeded in getting systemTime. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to get systemTime. Cause:` + JSON.stringify(error));
});
try {
systemTime.getCurrentTime().then((time) => {
console.info(`Succeeded in getting currentTime : ${time}`);
}).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
获取自系统启动以来经过的时间,不包括深度睡眠时间,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -178,21 +214,29 @@ getRealActiveTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
**示例:**
```js
systemTime.getRealActiveTime(true, (error, data) => {
if (error) {
console.error(`Failed to get real active time. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in getting real active time. Data:` + JSON.stringify(data));
});
try {
systemTime.getRealActiveTime(true, (error, time) => {
if (error) {
console.info(`Failed to getting 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}`);
}
```
## systemTime.getRealActiveTime<sup>8+</sup>
## systemTime.getRealActiveTime<sup>(deprecated)</sup>
getRealActiveTime(callback: AsyncCallback&lt;number&gt;): void
获取自系统启动以来经过的时间,不包括深度睡眠时间,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime-1)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -204,21 +248,29 @@ getRealActiveTime(callback: AsyncCallback&lt;number&gt;): void
**示例:**
```js
systemTime.getRealActiveTime((error, data) => {
if (error) {
console.error(`Failed to get real active time. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in getting real active time. Data:` + JSON.stringify(data));
});
try {
systemTime.getRealActiveTime((error, time) => {
if (error) {
console.info(`Failed to getting 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}`);
}
```
## systemTime.getRealActiveTime<sup>8+</sup>
## systemTime.getRealActiveTime<sup>(deprecated)</sup>
getRealActiveTime(isNano?: boolean): Promise&lt;number&gt;
获取自系统启动以来经过的时间,不包括深度睡眠时间,使用Promise异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime-2)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -236,19 +288,27 @@ getRealActiveTime(isNano?: boolean): Promise&lt;number&gt;
**示例:**
```js
systemTime.getRealActiveTime().then((data) => {
console.log(`Succeeded in getting real active time. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to get real active time. Cause:` + JSON.stringify(error));
});
try {
systemTime.getRealActiveTime().then((time) => {
console.info(`Succeeded in getting real active time : ${time}`);
}).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
获取自系统启动以来经过的时间,包括深度睡眠时间,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -261,21 +321,29 @@ getRealTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
**示例:**
```js
systemTime.getRealTime(true, (error, data) => {
if (error) {
console.error(`Failed to get real time. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in getting real time. Data:` + JSON.stringify(data));
});
try {
systemTime.getRealTime(true, (error, time) => {
if (error) {
console.info(`Failed to getting 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}`);
}
```
## systemTime.getRealTime<sup>8+</sup>
## systemTime.getRealTime<sup>(deprecated)</sup>
getRealTime(callback: AsyncCallback&lt;number&gt;): void
获取自系统启动以来经过的时间,包括深度睡眠时间,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime-1)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -287,28 +355,36 @@ getRealTime(callback: AsyncCallback&lt;number&gt;): void
**示例:**
```js
systemTime.getRealTime((error, data) => {
if (error) {
console.error(`Failed to get real time. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in getting real time. Data:` + JSON.stringify(data));
});
try {
systemTime.getRealTime((error, time) => {
if (error) {
console.info(`Failed to getting 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}`);
}
```
## systemTime.getRealTime<sup>8+</sup>
## systemTime.getRealTime<sup>(deprecated)</sup>
getRealTime(isNano?: boolean): Promise&lt;number&gt;
获取自系统启动以来经过的时间,包括深度睡眠时间,使用Promise异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime-2)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------- | ---- | ------------------------------- |
| isNano | boolean | 否 | 返回结果是否为纳秒数。<<br/>- true:表示返回结果为纳秒数(ns)。 <br/>- false:表示返回结果为毫秒数(ms)。 |
| isNano | boolean | 否 | 返回结果是否为纳秒数。<br/>- true:表示返回结果为纳秒数(ns)。 <br/>- false:表示返回结果为毫秒数(ms)。 |
**返回值:**
......@@ -319,14 +395,18 @@ getRealTime(isNano?: boolean): Promise&lt;number&gt;
**示例:**
```js
systemTime.getRealTime().then((data) => {
console.log(`Succeeded in getting real time. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to get real time. Cause:` + JSON.stringify(error));
});
try {
systemTime.getRealTime().then((time) => {
console.info(`Succeeded in getting real time : ${time}`);
}).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
......@@ -346,17 +426,21 @@ setDate(date: Date, callback: AsyncCallback&lt;void&gt;): void
**示例:**
```js
let data = new Date();
systemTime.setDate(data,(error, data) => {
if (error) {
console.error('Failed to set system date. Cause:' + JSON.stringify(error));
return;
}
console.info('Succeeded in setting system date. Data:' + JSON.stringify(data));
});
let date = new Date();
try {
systemTime.setDate(date, (error) => {
if (error) {
console.info(`Failed to setting 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}`);
}
```
## systemTime.setDate
## systemTime.setDate<sup>(deprecated)</sup>
setDate(date: Date): Promise&lt;void&gt;
......@@ -381,20 +465,28 @@ setDate(date: Date): Promise&lt;void&gt;
**示例:**
```js
let data = new Date();
systemTime.setDate(data).then((value) => {
console.log(`Succeeded in setting system date. Data:` + JSON.stringify(value));
}).catch((error) => {
console.error(`Failed to set system date. Cause:` + JSON.stringify(error));
});
let date = new Date();
try {
systemTime.setDate(date).then(() => {
console.info(`Succeeded in setting date.`);
}).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
获取当前系统日期,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getDate](./js-apis-system-date-time.md#systemdatetimegetdate)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -406,21 +498,29 @@ getDate(callback: AsyncCallback&lt;Date&gt;): void
**示例:**
```js
systemTime.getDate((error, data) => {
if (error) {
console.error(`Failed to get system date. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in getting system date. Data:` + JSON.stringify(data));
});
try {
systemTime.getDate((error, date) => {
if (error) {
console.info(`Failed to get date. message:${error.message}, code:${error.code}`);
return;
}
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;
获取当前系统日期,使用Promise异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getDate](./js-apis-system-date-time.md#systemdatetimegetdate-1)替代。
**系统能力:** SystemCapability.MiscServices.Time
**返回值:**
......@@ -432,14 +532,18 @@ getDate(): Promise&lt;Date&gt;
**示例:**
```js
systemTime.getDate().then((data) => {
console.log(`Succeeded in getting system date. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to get system date. Cause:` + JSON.stringify(error));
});
try {
systemTime.getDate().then((date) => {
console.info(`Succeeded in getting date : ${date}`);
}).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
......@@ -459,16 +563,20 @@ setTimezone(timezone: string, callback: AsyncCallback&lt;void&gt;): void
**示例:**
```js
systemTime.setTimezone('Asia/Shanghai', (error, data) => {
if (error) {
console.error('Failed to set system time zone. Cause:' + JSON.stringify(error));
return;
}
console.info('Succeeded in setting system time zone. Data:' + JSON.stringify(data));
});
try {
systemTime.setTimezone('Asia/Shanghai', (error) => {
if (error) {
console.info(`Failed to setting 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}`);
}
```
## systemTime.setTimezone
## systemTime.setTimezone<sup>(deprecated)</sup>
setTimezone(timezone: string): Promise&lt;void&gt;
......@@ -493,19 +601,27 @@ setTimezone(timezone: string): Promise&lt;void&gt;
**示例:**
```js
systemTime.setTimezone('Asia/Shanghai').then((data) => {
console.log(`Succeeded in setting system time zone. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to set system time zone. Cause:` + JSON.stringify(error));
});
try {
systemTime.setTimezone('Asia/Shanghai').then(() => {
console.info(`Succeeded in setting timezone.`);
}).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
获取系统时区,使用callback异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getTimezone](./js-apis-system-date-time.md#systemdatetimegettimezone)替代。
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -517,21 +633,29 @@ getTimezone(callback: AsyncCallback&lt;string&gt;): void
**示例:**
```js
systemTime.getTimezone((error, data) => {
if (error) {
console.error(`Failed to get system time zone. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in getting system time zone. Data:` + JSON.stringify(data));
});
try {
systemTime.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}`);
}
```
## systemTime.getTimezone<sup>8+</sup>
## systemTime.getTimezone<sup>(deprecated)</sup>
getTimezone(): Promise&lt;string&gt;
获取系统时区,使用Promise异步回调。
> **说明:**
>
> 从 API Version 7 开始支持,从 API Version 9 开始废弃,建议使用[systemDateTime.getTimezone](#syste./js-apis-system-date-time.md#systemdatetimegettimezone-1)替代。
**系统能力:** SystemCapability.MiscServices.Time
**返回值:**
......@@ -543,11 +667,15 @@ getTimezone(): Promise&lt;string&gt;
**示例:**
```js
systemTime.getTimezone().then((data) => {
console.log(`Succeeded in getting system time zone. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to get system time zone. Cause:` + JSON.stringify(error));
});
try {
systemTime.getTimezone().then((data) => {
console.info(`Succeeded in getting timezone: ${data}`);
}).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}`);
}
```
## 支持的系统时区
......
......@@ -35,7 +35,7 @@ import systemTimer from '@ohos.systemTimer';
| 名称 | 类型 | 必填 | 说明 |
| --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| type | number | 是 | 定时器类型。<br>取值为1时,表示为系统启动时间定时器(定时器启动时间不能晚于当前设置的系统时间) ;<br>取值为2时,表示为唤醒定时器;<br>取值为4时,表示为精准定时器;<br>取值为5时,表示为IDLE模式定时器(暂不支持)。 |
| type | number | 是 | 定时器类型。<br>取值为1,表示为系统启动时间定时器(定时器启动时间不能晚于当前设置的系统时间) ;<br>取值为2,表示为唤醒定时器;<br>取值为4,表示为精准定时器;<br>取值为8,表示为IDLE模式定时器(暂不支持)。 |
| repeat | boolean | 是 | true为循环定时器,false为单次定时器。 |
| interval | number | 否 | 如果是循环定时器,repeat值应大于5000毫秒,非重复定时器置为0。 |
| wantAgent | [WantAgent](js-apis-app-ability-wantAgent.md) | 否 | 设置通知的WantAgent,定时器到期后通知。(支持拉起应用MainAbility,暂不支持拉起ServiceAbility。) |
......@@ -48,6 +48,8 @@ createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void
创建定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -66,13 +68,17 @@ export default {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat: false
};
systemTimer.createTimer(options, (error, data) => {
if (error) {
console.error(`Failed to create timer. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data));
});
try {
systemTimer.createTimer(options, (error) => {
if (error) {
console.info(`Failed to create timer. message:${error.message}, code:${error.code}`);
return;
}
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;
创建定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -105,12 +113,16 @@ export default {
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
};
systemTimer.createTimer(options).then((data) => {
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to create timer. Cause:` + JSON.stringify(error));
});
};
try {
systemTimer.createTimer(options).then(() => {
console.info(`Succeeded in creating timer.`);
}).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
开始定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -143,12 +157,17 @@ export default {
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime, (error) => {
if (error) {
console.error(`Failed to start timer. Cause:` + JSON.stringify(error));
return;
}
});
try {
systemTimer.startTimer(timerId, triggerTime, (error) => {
if (error) {
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;
开始定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -186,11 +207,15 @@ export default {
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime).then((data) => {
console.log(`Succeeded in startting timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to start timer. Cause:` + JSON.stringify(error));
});
try {
systemTimer.startTimer(timerId, triggerTime).then(() => {
console.info(`Succeeded in starting timer.`);
}).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
停止定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -214,22 +241,27 @@ stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
```js
export default {
async systemTimer () {
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId, (error) => {
if (error) {
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error));
return;
}
});
}
async systemTimer () {
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
try {
systemTimer.stopTimer(timerId, (error) => {
if (error) {
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;
停止定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -257,21 +291,25 @@ stopTimer(timer: number): Promise&lt;void&gt;
```js
export default {
async systemTimer (){
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId).then((data) => {
console.log(`Succeeded in stopping timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error));
});
}
async systemTimer (){
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
try {
systemTimer.stopTimer(timerId).then(() => {
console.info(`Succeeded in stopping timer.`);
}).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
销毁定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -304,12 +344,17 @@ export default {
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId, (error) => {
if (error) {
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error));
return;
}
});
try {
systemTimer.destroyTimer(timerId, (error) => {
if (error) {
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;
销毁定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -348,11 +395,15 @@ export default {
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId).then((data) => {
console.log(`Succeeded in destroying timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error));
});
try {
systemTimer.destroyTimer(timerId).then(() => {
console.info(`Succeeded in destroying timer.`);
}).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}`);
}
}
}
```
\ No newline at end of file
# 时间时区子系统ChangeLog
## cl.time.1 接口异常抛出变更
时间时区子系统时间相关接口异常抛出:201权限否定异常、202非系统应用异常和401参数无效异常。
**变更影响**
基于此前版本开发的应用,继续使用无影响,使用新接口需要捕获并处理抛出的新异常。
**关键接口/组件变更**
变更前:
- 接口异常抛出message,错误码-1。
变更后:
- 接口异常抛出message和code,包括201权限否定异常、202非系统应用异常和401参数无效异常。
原接口中标记为废弃的接口,可以使用新接口中的同名接口替换
| 原接口 | 新接口 |
| ---------------- | -------------------- |
| @ohos.systemTime | @ohos.systemDateTime |
**适配指导**
应用中调用systemTime所有接口可参考下列代码进行异常捕获,以setTime接口为例,其他接口适配方法相同。
callback形式调用:
**示例:**
```js
import systemDateTime from @ohos.systemDateTime
// time对应的时间为2021-01-20 02:36:25
let time = 1611081385000;
try {
systemDateTime.setTime(time, (error) => {
//捕获权限否定异常和非系统应用异常
if (error) {
console.info(`Failed to setting 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}`);
}
```
promise形式调用:
**示例:**
```js
import systemDateTime from @ohos.systemDateTime
// time对应的时间为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 setting time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
//捕获参数校验失败异常
console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册