# ntpclient **Repository Path**: jojobiid/ntpclient ## Basic Information - **Project Name**: ntpclient - **Description**: ntpclientohdemo - **Primary Language**: Unknown - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-02-02 - **Last Updated**: 2024-02-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ntpclient ## 简介 > ohos_ntpclient是一个用于网络时间协议(NTP)服务器同步事件的TypeScript库。它允许你的应用程序通过与NTP服务器通信来获取准确的事件信息,以确保你的设备具有准确的系统时钟,或确保能获取到准确时间信息用来调试。 > 其中本软件/ntpclient/src/main/ets/index.ts目录下的代码是开源软件 [ntpclient](https://github.com/ffflorian/node-packages/tree/main/packages/ntpclient) 的OepnHarmonyAPI实现; ## 下载安装 ```shell ohpm install @ohos/ntpclient ``` OpenHarmony ohpm环境配置等更多内容,请参考 [如何安装OpenHarmony ohpm包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md) 。 ## 使用说明 1、引入依赖 ``` import NTPClient from '@ohos/ntp-client'; ``` 2、在module.json5中添加权限 ``` "requestPermissions": [ { "name": "ohos.permission.INTERNET" } ] ``` 3、在事件或钩子函数中创建NTPClient实例化对象,调用getNetworkTime方法,不传参为默认值 ``` new NTPClient({ server: NTP服务器(默认为pool.ntp.org), port: 远端端口号(默认为123), replyTimeout: 等待响应时长(ms)(默认为10000ms) }) .getNetworkTime() .then(date => { this.ntpResult = JSON.stringify(date) }) .catch((err: string) => { this.timeoutResult = JSON.stringify('timeout' + err) }) ``` ## 接口说明 ### NTPClient | 接口名 | 参数 | 返回值 | 说明 | | -------------- | ------------------------------------- | ------------- | ----------------------- | | NTPClient | {server,port,replyTimeout}?:NTPConfig | new NTPClient | 创建NTPClient实例化对象 | | getNetworkTime | ntpReplyTimeout?: number | Promise | 获取NTP时间 | ## 约束与限制 在下述版本验证通过: - DevEco Studio NEXT Developer Preview1(4.1.3.500), SDK: API11 Release(4.1.0) ## 目录结构 ```` |---- ntpclient | |---- entry # 示例代码文件夹 | |---- library # mail库文件夹 | |---- src | |---- main | |---- ets | |---- index.ts # NTPClient | |---- index.ets # 对外接口 | |---- README.md # 安装使用方法 ```` ## 贡献代码 使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-tpc/ntpclient/issues)给我们,当然,我们也非常欢迎你给我们发 [PR](https://gitee.com/openharmony-tpc/ntpclient/pulls)。 ## 开源协议 本项目基于 [GNU GENERAL PUBLIC LICENSE Version 3](https://gitee.com/openharmony-tpc/ntpclient/tree/master/LICENSE) ,请自由地享受和参与开源。