搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.0

ntp

從(NTP)網路時間協定伺服器取得精確的時間或目前的 DateTime 物件。

AndroidiOSmacOSWindowsLinux
近 30 天下載
17.7萬
喜歡數
261
Pub 分數
150

安裝套件

flutter pub add ntp

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

ntp 包提供了一種從網路時間協定(NTP)伺服器取得準確系統時間的簡單方式。它讓應用程式能夠取得與全球時間標準同步的精確 DateTime 值,這在日誌記錄、排程或跨裝置同步等對時間敏感的操作中特別有用。該包支援 Android、iOS、macOS、Windows 和 Linux 等多個平台,確保在不同環境中的行為一致。它使用標準 NTP 協定,並提供易於使用的 API 來取得目前時間,而不需依賴本機裝置時鐘。

適用情境

  • 同步應用時間戳與全球時間
  • 提高基於時間的日誌記錄的準確性
  • 啟用可靠的任務排程
  • 驗證設備時鐘漂移
  • 支援需要精確時間的分散式系統

主要特色

  • 從 NTP 伺服器取得即時資料
  • 跨平台相容性
  • 簡單的同步與非同步 API
  • 高精度時間取得
  • 最小依賴

適合對象

  • 需要高精度時間的應用程式
  • 時間敏感的服務
  • 物聯網和後端系統
  • 需要同步時鐘的行動和桌面應用程式
  • 時間驗證的開發工具

使用建議

  • 需要網路連接才能使用
  • 可能因網路狀況而產生延遲
  • 不適用於離線使用
  • 伺服器可用性影響可靠性
  • 時區處理必須另行管理

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

pub package CodeFactor

NTP

Plugin that allows you to get precise time from Network Time Protocol (NTP). It implements whole NTP protocol in dart.

This is useful for time-based events since DateTime.now() returns the time of the device. Users sometimes change their internal clock and using DateTime.now() can give wrong result. You can just get clock offset [NTP.getNtpTime] and apply it manually to DateTime.now() object when needed (just add offset as milliseconds duration), or you can get already formatted [DateTime] object from [NTP.now].

By default lookup address for NTP is: time.google.com

For example on how to use look in github library repository example/ folder.

How it works

Using int offset from getNtpTime()

  • default localTime is DateTime.now()
  • default lookUpAddress is 'time.google.com'
  • default port is 123
  DateTime startDate = new DateTime.now().toLocal();
  int offset = await NTP.getNtpOffset(localTime: startDate);
  print('NTP DateTime offset align: ${startDate.add(new Duration(milliseconds: offset))}');

Using DateTime from now

  DateTime startDate = await NTP.now();
  print('NTP DateTime: ${startDate}');
NTP Functions
  Future<int> getNtpOffset({
    String lookUpAddress: 'time.google.com',
    int port: 123,
    DateTime localTime,
    Duration timeout,
  });
  Future<DateTime> now();

所屬分類

原始碼儲存庫

使用此套件的專案