• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 时间时区子系统ChangeLog
2
3## cl.time.1 接口异常抛出变更
4
5时间时区子系统定时器接口异常抛出:202非系统应用异常和401参数无效异常。
6
7**变更影响**
8
9该接口变更前向兼容,基于此前版本开发的应用可继续使用接口,增加相应的异常处理,原有功能不受影响。
10
11**关键接口/组件变更**
12
13变更前:
14  - 接口异常抛出message,无错误码。
15
16变更后:
17  - 接口异常抛出message和code,包括202非系统应用异常和401参数无效异常。
18
19    | 模块名            | 类名        | 方法/属性/枚举/常量                                          | 变更类型 |
20    | ----------------- | ----------- | ------------------------------------------------------------ | -------- |
21    | @ohos.systemTimer | systemTimer | function createTimer(options: TimerOptions, callback: AsyncCallback<number>): void | 变更     |
22    | @ohos.systemTimer | systemTimer | function createTimer(options: TimerOptions): Promise<number> | 变更     |
23    | @ohos.systemTimer | systemTimer | function startTimer(timer: number, triggerTime: number, callback: AsyncCallback<void>): void | 变更     |
24    | @ohos.systemTimer | systemTimer | function startTimer(timer: number, triggerTime: number): Promise<void> | 变更     |
25    | @ohos.systemTimer | systemTimer | function stopTimer(timer: number, callback: AsyncCallback<void>): void | 变更     |
26    | @ohos.systemTimer | systemTimer | function stopTimer(timer: number): Promise<void>             | 变更     |
27    | @ohos.systemTimer | systemTimer | function destroyTimer(timer: number, callback: AsyncCallback<void>): void | 变更     |
28    | @ohos.systemTimer | systemTimer | function destroyTimer(timer: number): Promise<void>          | 变更     |
29
30
31**适配指导**
32
33应用中调用systemTimer所有接口可参考下列代码进行异常捕获
34
35createTimer callback形式调用:
36
37**示例:**
38
39```js
40export default {
41  systemTimer () {
42    let options = {
43      type: systemTimer.TIMER_TYPE_REALTIME,
44      repeat: false
45    };
46    try {
47      systemTimer.createTimer(options, (error, timerId) => {
48        if (error) {
49          //捕获权限否定异常
50          console.info(`Failed to create timer. message: ${error.message}, code: ${error.code}`);
51        }
52        console.info(`Succeeded in creating timer. timerId: ${timerId}`);
53      });
54    } catch(e) {
55      //捕获参数校验失败异常
56      console.info(`Failed to create timer. message: ${e.message}, code: ${e.code}`);
57    }
58  }
59}
60```
61
62createTimer promise形式调用:
63
64**示例:**
65
66```js
67export default {
68  systemTimer () {
69    let options = {
70      type: systemTimer.TIMER_TYPE_REALTIME,
71      repeat: false
72    };
73    try {
74      systemTimer.createTimer(options).then((timerId) => {
75        console.info(`Succeeded in creating timer. timerId: ${timerId}`);
76      }).catch((error) => {
77        //捕获权限否定异常
78        console.info(`Failed to create timer. message: ${error.message}, code: ${error.code}`);
79      });
80    } catch(e) {
81      //捕获参数校验失败异常
82      console.info(`Failed to create timer. message: ${e.message}, code: ${e.code}`);
83    }
84  }
85}
86```
87
88startTimer callback形式调用:
89
90**示例:**
91
92```js
93export default {
94  async systemTimer () {
95    let options = {
96      type: systemTimer.TIMER_TYPE_REALTIME,
97      repeat:false
98    }
99    let timerId = await systemTimer.createTimer(options);
100    let triggerTime = new Date().getTime();
101    triggerTime += 3000;
102    try {
103      systemTimer.startTimer(timerId, triggerTime, (error) => {
104        if (error) {
105          //捕获权限否定异常
106          console.error(`Failed to start timer. message: ${error.message}, code: ${error.code}`);
107        }
108        });
109    } catch (e) {
110      //捕获参数校验失败异常
111      console.info(`Failed to start timer. message: ${e.message}, code: ${e.code}`);
112    }
113  }
114}
115```
116
117startTimer promise形式调用:
118
119**示例:**
120
121```js
122export default {
123  async systemTimer (){
124    let options = {
125      type: systemTimer.TIMER_TYPE_REALTIME,
126      repeat:false
127    }
128    let timerId = await systemTimer.createTimer(options);
129    let triggerTime = new Date().getTime();
130    triggerTime += 3000;
131    try {
132      systemTimer.startTimer(timerId, triggerTime).then((data) => {
133        console.log(`Succeeded in startting timer. Data:` + data);
134      }).catch((error) => {
135        //捕获权限否定异常
136        console.info(`Failed to start timer. message: ${error.message}, code: ${error.code}`);
137      });
138    } catch (e) {
139      //捕获参数校验失败异常
140      console.info(`Failed to start timer. message: ${e.message}, code: ${e.code}`);
141    }
142  }
143}
144```
145
146stopTimer callback形式调用:
147
148**示例:**
149
150```js
151export default {
152  async systemTimer () {
153    let options = {
154      type: systemTimer.TIMER_TYPE_REALTIME,
155      repeat:false
156    }
157    let timerId = await systemTimer.createTimer(options);
158    let triggerTime = new Date().getTime();
159    triggerTime += 3000;
160    systemTimer.startTimer(timerId, triggerTime);
161    try {
162      systemTimer.stopTimer(timerId, triggerTime, (error) => {
163        if (error) {
164          //捕获权限否定异常
165          console.error(`Failed to stop timer. message: ${error.message}, code: ${error.code}`);
166        }
167        });
168    } catch (e) {
169      //捕获参数校验失败异常
170      console.info(`Failed to stop timer. message: ${e.message}, code: ${e.code}`);
171    }
172  }
173}git
174```
175
176stopTimer promise形式调用:
177
178**示例:**
179
180```js
181export default {
182  async systemTimer (){
183    let options = {
184      type: systemTimer.TIMER_TYPE_REALTIME,
185      repeat:false
186    }
187    let timerId = await systemTimer.createTimer(options);
188    let triggerTime = new Date().getTime();
189    triggerTime += 3000;
190    systemTimer.startTimer(timerId, triggerTime);
191    try {
192      systemTimer.stopTimer(timerId, triggerTime).then((data) => {
193        console.log(`Succeeded in stop timer. Data:` + data);
194      }).catch((error) => {
195        //捕获权限否定异常
196        console.info(`Failed to stop timer. message: ${error.message}, code: ${error.code}`);
197      });
198    } catch (e) {
199      //捕获参数校验失败异常
200      console.info(`Failed to stop timer. message: ${e.message}, code: ${e.code}`);
201    }
202  }
203}
204```
205
206destroyTimer callback形式调用:
207
208**示例:**
209
210```js
211export default {
212  async systemTimer () {
213    let options = {
214      type: systemTimer.TIMER_TYPE_REALTIME,
215      repeat:false
216    }
217    let timerId = await systemTimer.createTimer(options);
218    let triggerTime = new Date().getTime();
219    triggerTime += 3000;
220    systemTimer.startTimer(timerId, triggerTime);
221    systemTimer.stopTimer(timerId);
222    try {
223      systemTimer.destroyTimer(timerId, triggerTime, (error) => {
224        if (error) {
225          //捕获权限否定异常
226          console.error(`Failed to destroy timer. message: ${error.message}, code: ${error.code}`);
227        }
228        });
229    } catch (e) {
230      //捕获参数校验失败异常
231      console.info(`Failed to destroy timer. message: ${e.message}, code: ${e.code}`);
232    }
233  }
234}
235```
236
237destroyTimer promise形式调用:
238
239**示例:**
240
241```js
242export default {
243  async systemTimer (){
244    let options = {
245      type: systemTimer.TIMER_TYPE_REALTIME,
246      repeat:false
247    }
248    let timerId = await systemTimer.createTimer(options);
249    let triggerTime = new Date().getTime();
250    triggerTime += 3000;
251    systemTimer.startTimer(timerId, triggerTime);
252    systemTimer.stopTimer(timerId);
253    try {
254      systemTimer.destroyTimer(timerId, triggerTime).then((data) => {
255        console.log(`Succeeded in destroy timer. Data:` + data);
256      }).catch((error) => {
257        //捕获权限否定异常
258        console.info(`Failed to destroy timer. message: ${error.message}, code: ${error.code}`);
259      });
260    } catch (e) {
261      //捕获参数校验失败异常
262      console.info(`Failed to destroy timer. message: ${e.message}, code: ${e.code}`);
263    }
264  }
265}
266```
267
268## cl.time.2 接口异常抛出变更
269
270时间时区子系统时间相关接口异常抛出:201权限否定异常、202非系统应用异常和401参数无效异常。
271
272**变更影响**
273
274基于此前版本开发的应用,继续使用无影响,使用新接口需要捕获并处理抛出的新异常。
275
276**关键接口/组件变更**
277
278变更前:
279  - 接口异常抛出message,错误码-1。
280
281变更后:
282  - 接口异常抛出message和code,包括201权限否定异常、202非系统应用异常和401参数无效异常。
283
284原接口中标记为废弃的接口,可以使用新接口中的同名接口替换
285
286| 原接口           | 新接口               |
287| ---------------- | -------------------- |
288| @ohos.systemTime | @ohos.systemDateTime |
289
290**适配指导**
291
292应用中调用systemTime所有接口可参考下列代码进行异常捕获,以setTime接口为例,其他接口适配方法相同。
293
294callback形式调用:
295
296**示例:**
297
298```js
299import systemDateTime from @ohos.systemDateTime
300// time对应的时间为2021-01-20 02:36:25
301let time = 1611081385000;
302try {
303  systemDateTime.setTime(time, (error) => {
304    //捕获权限否定异常和非系统应用异常
305    if (error) {
306      console.info(`Failed to setting time. message: ${error.message}, code: ${error.code}`);
307      return;
308    }
309    console.info(`Succeeded in setting time.`);
310  })
311} catch(e) {
312  //捕获参数校验失败异常
313  console.info(`Failed to set time. message: ${e.message}, code: ${e.code}`);
314}
315```
316
317promise形式调用:
318
319**示例:**
320
321```js
322import systemDateTime from @ohos.systemDateTime
323// time对应的时间为2021-01-20 02:36:25
324let time = 1611081385000;
325try {
326  systemDateTime.setTime(time).then(() => {
327    console.info(`Succeeded in setting time.`);
328  }).catch((error) => {
329    //捕获权限否定异常和非系统应用异常
330    console.info(`Failed to setting time. message: ${error.message}, code: ${error.code}`);
331  });
332} catch(e) {
333   //捕获参数校验失败异常
334  console.info(`Failed to set time. message: ${e.message}, code: ${e.code}`);
335}
336```
337