1 Star 0 Fork 171

Jeam.wang / time_time_service

forked from OpenHarmony / time_time_service 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 5.69 KB
一键复制 编辑 原始数据 按行查看 历史

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
├── framework/js/napi        # the js interface resolves to the napi interface
├── interfaces/inner_api     # external interface code provided by the component
├── services                 # time service realization
│   └── sa_profile           # module contains the config files of system services and processes
├── test                     # unit test of interface
└── utils                    # module contains log printing and constants for ordered commonEvent

Usage

Available JS APIs

Table 1 Major functions of systemTime

Interface name describe
setTime(time : number) : Promise Set the system time (1970-01-01 to the present in milliseconds), Promise method.
setTime(time : number, callback : AsyncCallback) : void Set the system time (1970-01-01 to the present in milliseconds), callback mode.
setDate(date: Date, callback: AsyncCallback): void; Set the system time (Date format), Promise method.
setDate(date: Date): Promise Set system time (Date format), callback method.
setTimezone(timezone: string, callback: AsyncCallback): void Set the system time zone, callback method.
setTimezone(timezone: string): Promise Set the system time zone, Promise method.

表 2 Major functions of systemTimer

Interface name describe
createTimer(options: TimerOptions, callback: AsyncCallback): void Create timer, callback method
createTimer(options: TimerOptions): Promise Create timer, promise method
startTimer(timer: number, triggerTime: number, callback: AsyncCallback): void Start the timer, callback mode
startTimer(timer: number, triggerTime: number): Promise Start the timer, promise mode
stopTimer(timer: number, callback: AsyncCallback): void Stop the timer, callback mode
stopTimer(timer: number): Promise Stop the timer, promise mode
destroyTimer(timer: number, callback: AsyncCallback): void Destroy the timer, callback method
destroyTimer(timer: number): Promise Destroy the timer, the promise method

表 3 parameter TimerOptions description of systemTimer

name type illustrate
type number Timer type.
If the value is 1, it is represented as the system startup time timer (the timer start time cannot be later than the currently set system time);
If the value is 2, it is indicated as a wake-up timer;
When the value is 4, it is represented as a precision timer;
If the value is 5, it is represented as an IDLE mode timer (not supported).
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

Time/Timezone subsystem

time_time_service

1
https://gitee.com/jeam-wang/time_time_service.git
git@gitee.com:jeam-wang/time_time_service.git
jeam-wang
time_time_service
time_time_service
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891