Timing and Time

Introduction

The timing and time module provides APIs for managing the system time.

Figure 1 Subsystem architecture

Directory Structure

/base/time/time_service
├── etc                      # Process configuration files
├── figures                  # Architecture diagram
├── interfaces               # APIs for external systems and applications
│   └── innerkits            # APIs between services
│   └── kits                 # APIs
├── profile                  # System service configuration files
└── services                 # Service implementation

Usage

Available JS APIs

Table 1 Major functions of systemTime

Interface name

describe

function setTime(time : number) : Promise<boolean>

Set the system time (1970-01-01 to the present in milliseconds), Promise method

function setTime(time : number, callback : AsyncCallback<boolean>) : void

Set the system time (1970-01-01 to the present in milliseconds), callback mode

function setDate(date: Date, callback: AsyncCallback<boolean>): void;

Set the system time (Date format), Promise method

function setDate(date: Date): Promise<boolean>

Set system time (Date format), callback method

function setTimezone(timezone: string, callback: AsyncCallback<boolean>): void

Set the system time zone, callback method

function setTimezone(timezone: string): Promise<boolean>

Set the system time zone, Promise method

表 2 Major functions of systemTimer

Interface name

describe

function createTimer(options: TimerOptions, callback: AsyncCallback<number>): void

Create timer, callback method

function createTimer(options: TimerOptions): Promise<number>

Create timer, promise method

function startTimer(timer: number, triggerTime: number, callback: AsyncCallback<boolean>): void

Start the timer, callback mode

function startTimer(timer: number, triggerTime: number): Promise<boolean>

Start the timer, promise mode

function stopTimer(timer: number, callback: AsyncCallback<boolean>): void

Stop the timer, callback mode

function stopTimer(timer: number): Promise<boolean>

Stop the timer, promise mode

function destroyTimer(timer: number, callback: AsyncCallback<boolean>): void

Destroy the timer, callback method

function destroyTimer(timer: number): Promise<boolean>

Destroy the timer, the promise method

表 3 parameter TimerOptions description of systemTimer

name

type

illustrate

type

number

TIMER_TYPE_REALTIME: Set as the system startup time timer, otherwise it is the walltime timer; TIMER_TYPE_WAKEUP: Set to wake-up timer, otherwise it is non-wake-up; const TIMER_TYPE_EXACT: Set as a precision timer, otherwise it is a non-precision timer; const TIMER_TYPE_IDLE: Set to IDLE mode timer, otherwise it is non-IDLE mode timer (not supported yet)

repeat

boolean

true Is a cyclic timer, false is a single timer

interval

number

If it is a cyclic timer, the repeat value should be greater than 5000 milliseconds, and the non-repeated timer is set to 0

wantAgent

wantAgent

Set the wantagent to notify, and notify when the timer expires

callback

=> void

Set the callback function, which will be triggered after the timer expires

Sample Code

Example fo using systemTime

// Import the module.
import systemTime from '@ohos.systemTime';

// Set the system time asynchronously with a Promise.
var time = 1611081385000;   
systemTime.setTime(time)    
      .then((value) => {        
          console.log(`success to systemTime.setTime: ${value}`);   
       }).catch((err) => {        
          console.error(`failed to systemTime.setTime because ${err.message}`)  
    });

// Set the system time asynchronously with a callback.   
var time = 1611081385000;   
systemTime.setTime(time, (err, value) => {   
     if (err) {        
          console.error(`failed to systemTime.setTime because ${err.message}`);   
           return;   
         }    
       console.log(`success to systemTime.setTime: ${value}`);   
    });

Example fo using systemTimer

// Import the module import systemTimer from '@ohos.systemTimer';

console.log("start")
var options:TimerOptions{
type:TIMER_TYPE_REALTIME,
repeat:false,
interval:Number.MAX_VALUE/2,
persistent:false
}

console.log("create timer")
let timerId = systemTimer.Timer(options)
console.log("start timer")
let startTimerRes = systemTimer.startTimer(timerId, 100000)
console.log("stop timer")
let stopTimerRes = systemTimer.stopTimer(timerId)
console.log("destroy timer")
let destroyTimerRes = systemTimer.destroyTimer(timerId)
console.log('end');


## Repositories Involved<a name="section1371113476307"></a>

**Misc services subsystem**

[time\_time\_service](https://gitee.com/openharmony/time_time_service/tree/master/)

S
Description
Timing and time module | 时间和定时组件
Readme 1.5 MiB
Languages
C++ 70.2%
JavaScript 29.6%
C 0.2%