changelogs-time.md 10.4 KB
Newer Older
G
Gloria 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
# Time Subsystem ChangeLog

## cl.time.1 API Error Change

Errors thrown by timer APIs of the time subsystem: **202** (non-system application) and **401** (invalid parameters).

**Change Impacts**

The API change is forward compatible. Applications developed based on earlier versions can still use the APIs, and corresponding error handling is added. The original functions are not affected.

**Key API/Component Changes**

Before change:
  - The API throws an error message without an error code.

After change:
  - The API throws an error message with an error code. Error code **202** indicates that the application is not a system application, and error code **401** indicates that the parameters are invalid.

    | Module           | Class       | Method/Attribute/Enumeration/Constant                                         | Change Type|
    | ----------------- | ----------- | ------------------------------------------------------------ | -------- |
    | @ohos.systemTimer | systemTimer | function createTimer(options: TimerOptions, callback: AsyncCallback\<number>): void | Changed    |
    | @ohos.systemTimer | systemTimer | function createTimer(options: TimerOptions): Promise\<number> | Changed    |
    | @ohos.systemTimer | systemTimer | function startTimer(timer: number, triggerTime: number, callback: AsyncCallback\<void>): void | Changed    |
    | @ohos.systemTimer | systemTimer | function startTimer(timer: number, triggerTime: number): Promise\<void> | Changed    |
    | @ohos.systemTimer | systemTimer | function stopTimer(timer: number, callback: AsyncCallback\<void>): void | Changed    |
    | @ohos.systemTimer | systemTimer | function stopTimer(timer: number): Promise\<void>            | Changed    |
    | @ohos.systemTimer | systemTimer | function destroyTimer(timer: number, callback: AsyncCallback\<void>): void | Changed    |
    | @ohos.systemTimer | systemTimer | function destroyTimer(timer: number): Promise\<void>         | Changed    |


**Adaptation Guide**

Refer to the code below to capture errors when **systemTimer** APIs are called in applications.

createTimer callback mode:

**Example**

```js
export default {
  systemTimer () {
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat: false
    };
    try {
      systemTimer.createTimer(options, (error, timerId) => {
        if (error) {
          // Capture the permission denial error.
          console.info(`Failed to create timer. message: ${error.message}, code: ${error.code}`);
        }
        console.info(`Succeeded in creating timer. timerId: ${timerId}`);
      });
    } catch(e) {
      // Capture the parameter verification error.
      console.info(`Failed to create timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}
```

createTimer promise mode:

**Example**

```js
export default {
  systemTimer () {
    let options = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat: false
    };
    try {
      systemTimer.createTimer(options).then((timerId) => {
        console.info(`Succeeded in creating timer. timerId: ${timerId}`);
      }).catch((error) => {
        // Capture the permission denial error.
        console.info(`Failed to create timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch(e) {
      // Capture the parameter verification error.
      console.info(`Failed to create timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}
```

startTimer callback mode:

**Example**

```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;
    try {
      systemTimer.startTimer(timerId, triggerTime, (error) => {
        if (error) {
          // Capture the permission denial error.
          console.error(`Failed to start timer. message: ${error.message}, code: ${error.code}`);
        }
        });
    } catch (e) {
      // Capture the parameter verification error.
      console.info(`Failed to start timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}
```

startTimer promise mode:

**Example**

```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;
    try {
      systemTimer.startTimer(timerId, triggerTime).then((data) => {
        console.log(`Succeeded in startting timer. Data:` + data);
      }).catch((error) => {
        // Capture the permission denial error.
        console.info(`Failed to start timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch (e) {
      // Capture the parameter verification error.
      console.info(`Failed to start timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}
```

stopTimer callback mode:

**Example**

```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);
    try {
      systemTimer.stopTimer(timerId, triggerTime, (error) => {
        if (error) {
          // Capture the permission denial error.
          console.error(`Failed to stop timer. message: ${error.message}, code: ${error.code}`);
        }
        });
    } catch (e) {
      // Capture the parameter verification error.
      console.info(`Failed to stop timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}git 
```

stopTimer promise mode:

**Example**

```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);
    try {
      systemTimer.stopTimer(timerId, triggerTime).then((data) => {
        console.log(`Succeeded in stop timer. Data:` + data);
      }).catch((error) => {
        // Capture the permission denial error.
        console.info(`Failed to stop timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch (e) {
      // Capture the parameter verification error.
      console.info(`Failed to stop timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}
```

destroyTimer callback mode:

**Example**

```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);
    try {
      systemTimer.destroyTimer(timerId, triggerTime, (error) => {
        if (error) {
          // Capture the permission denial error.
          console.error(`Failed to destroy timer. message: ${error.message}, code: ${error.code}`);
        }
        });
    } catch (e) {
      // Capture the parameter verification error.
      console.info(`Failed to destroy timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}
```

destroyTimer promise mode:

**Example**

```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);
    try {
      systemTimer.destroyTimer(timerId, triggerTime).then((data) => {
        console.log(`Succeeded in destroy timer. Data:` + data);
      }).catch((error) => {
        // Capture the permission denial error.
        console.info(`Failed to destroy timer. message: ${error.message}, code: ${error.code}`);
      });
    } catch (e) {
      // Capture the parameter verification error.
      console.info(`Failed to destroy timer. message: ${e.message}, code: ${e.code}`);
    }
  }
}
```

## cl.time.2 API Error Change

Errors thrown by timer APIs of the time subsystem: **201** (permission denied), **202** (non-system application), and **401** (invalid parameters).

**Change Impacts**

Applications developed based on earlier versions can still use the APIs. When new APIs are used, errors must be captured and processed.

**Key API/Component Changes**

Before change:
  - The API throws an error message with error code **-1**.

After change:
  - The API throws an error message with an error code. Error code **201** indicates that the permission is denied, error code **202** indicates that the application is not a system application, and error code **401** indicates that the parameters are invalid.

Deprecated APIs can be replaced with new ones with same names.

| Original API          | New API              |
| ---------------- | -------------------- |
| @ohos.systemTime | @ohos.systemDateTime |

**Adaptation Guide**

Refer to the code below to capture errors when **systemTime** APIs are called in applications. In the examples, the **setTime** API is invoked.

In callback mode:

**Example**

```js
import systemDateTime from @ohos.systemDateTime
// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
  systemDateTime.setTime(time, (error) => {
    // Capture permission denial and non-system-application errors.
    if (error) {
      console.info(`Failed to setting time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in setting time.`);
  })
} catch(e) {
  // Capture the parameter verification error.
  console.info(`Failed to set time. message: ${e.message}, code: ${e.code}`);
}
```

In promise mode:

**Example**

```js
import systemDateTime from @ohos.systemDateTime
// 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) => {
    // Capture permission denial and non-system-application errors.
    console.info(`Failed to setting time. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
   // Capture the parameter verification error.
  console.info(`Failed to set time. message: ${e.message}, code: ${e.code}`);
}
```