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

套件詳情v2.1.6

time

類型安全的 DateTime 和 Duration 計算,由擴展功能提供支援。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
107萬
喜歡數
387
Pub 分數
160

安裝套件

flutter pub add time

套件介紹

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

time 套件在 Dart 中提供類型安全的 DateTime 與 Duration 計算,透過直覺的擴充功能提升可靠性與可讀性。它透過提供乾淨流暢的 API 簡化常見的日期與時間操作,避免手動日期處理所導致的執行時錯誤。此套件以安全性與開發者體驗為設計核心,支援所有主要平台,包含行動裝置、Web 與桌面端。對於需要精確時間處理的應用程式(例如排程、計時器或事件追蹤)而言,此套件極為適合。

適用情境

  • 排程事件
  • 計算時間差
  • 建構倒計時器
  • 處理重複任務
  • 管理時區
  • 追蹤時長

主要特色

  • 類型安全的作業
  • 豐富的擴展方法
  • 跨平台支援
  • 編譯時期的安全性
  • 直觀的語法
  • 不可變持續時間處理

適合對象

  • 需要可靠時間邏輯的開發人員
  • 具有複雜排程的應用程式
  • 基於時間的分析
  • 即時應用
  • 時間操縱的教育工具

使用建議

  • 需要了解 Dart 擴展
  • 可能稍微增加建構大小
  • 不適用於底層系統時間存取
  • 僅限高階時間計算
  • 無內建時區轉換

原始 README

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

⏰ Time

Build codecov pub package

With shiny extensions, if you have ever written something like this, then look no further:

final DateTime fourHoursFromNow = DateTime.now() + Duration(hours: 4);

🎖 Installation

dependencies:
  time: "^2.1.6"
⚡ Import
import 'package:time/time.dart';

🎮 Usage

final Duration tenMinutes = 10.minutes;
final Duration oneHourThirtyMinutes = 1.5.hours;
final DateTime afterTenMinutes = DateTime.now() + 10.minutes;
final Duration tenMinutesAndSome = 10.minutes + 15.seconds;
final int tenMinutesInSeconds = 10.minutes.inSeconds;
final DateTime tenMinutesFromNow = 10.minutes.fromNow;

You can perform all basic arithmetic operations on Duration as you always have been:

final Duration interval = 10.minutes + 15.seconds - 3.minutes + 2.hours;
final Duration doubled = interval * 2;

You can also use these operations on DateTime:

final DateTime oneHourAfter = DateTime() + 1.hours;

Duration is easily convertible as it always has been:

final int twoMinutesInSeconds = 2.minutes.inSeconds;

You can also convert Duration to DateTime, if needed:

final DateTime timeInFuture = 5.minutes.fromNow;
final DateTime timeInPast = 5.minutes.ago;

Iterate through a DateTime range:

final DateTime start = DateTime(2019, 12, 2);
final DateTime end = start + 1.weeks;
final DateTime tuesday = start.to(end).firstWhere((date) => date.weekday == DateTime.tuesday);

Granular comparison between DateTime fields:

final DateTime specificDate = DateTime(2021, 01, 01);
final DateTime otherDate = DateTime(2021, 02, 01);

print(specificDate.isAtSameYearAs(otherDate)); // true
print(specificDate.isAtSameMonthAs(otherDate)); // false
print(specificDate.isAtSameDayAs(otherDate)); // false

You can also delay code execution:

void doSomething() async {
  await 5.seconds.delay;
  // Do the other things
}

You can also use the popular copyWith:

final initial = DateTime(2019, 2, 4, 24, 50, 45, 1, 1);
final expected = initial.copyWith(
  year: 2021,
  month: 10,
  day: 28,
  hour: 12,
  minute: 45,
  second: 10,
  millisecond: 0,
  microsecond: 12,
);

Visit the API Reference to find out all that is available.

🐛 Bugs/Requests

If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github and I'll look into it. Pull request are also welcome.

👏 Inspiration

  • Swift library of the same name - Time.
  • Kotlin library of the same name - Time.

⭐ License

MIT License

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

所在榜單