搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

所在榜单