搜索插件、代码仓与 APP 分类

插件详情v0.9.17

time_machine

适用于 Flutter、Web 和服务器的日期和时间库,支持时区、日历、文化、格式化和解析。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2万
喜欢数
152
Pub 分数
140

安装插件

flutter pub add time_machine

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

time_machine 是一个全面的日期和时间库,适用于 Flutter、Web 和服务器应用程序,提供对时区、多种日历、文化格式化和解析的强大支持。它能够在不同平台上准确且一致地处理日期,确保在任何环境下都能可靠运行。该库以开发者体验为核心设计,简化了复杂的日期操作,同时保持高性能和高精度。

适合场景

  • 处理时区感知的日期
  • 解析和格式化国际日期字符串
  • 支持非格里高利历
  • 构建日历或调度应用程序
  • 跨平台日期一致性
  • 国际化日期显示

主要特点

  • 完整的时区支持
  • 多日历兼容性
  • 文化感知格式化
  • 强大的解析工具
  • 平台无关的 API
  • 不可变日期对象

适合对象

  • 需要本地化的全局应用程序
  • 具有复杂调度需求的应用程序
  • 需要精确时间计算的开发者
  • 面向多个平台的项目
  • 注重代码一致性的团队

使用建议

  • 新用户需要一定的学习曲线
  • 可能会略微增加应用大小
  • 与替代方案相比,社区文档有限
  • 不直接兼容原生日期 API
  • 最适合对准确性和灵活性要求较高的项目

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

logo-dtm

The Dart Time Machine is a date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.

Time Machine provides an alternative date and time API over Dart Core. For comparision:

Dart Core API

  • Duration - an amount of time with microsecond precision
  • DateTime - a unique point on the utc_timeline or a point in localtime with microsecond or millisecond precision

Time Machine API

  • Time - an amount of time with nanosecond precision
  • Instant - a unique point on the utc_timeline
  • LocalTime - the time on the clock
  • LocalDate - the date on the calendar
  • LocalDateTime - a location on the clock and calendar
  • Period - amount of time on the clock and calendar
  • Offset - the timezone offset from the utc_timeline
  • DateTimeZone - a mapping between the utc_timeline, and clock and calendar locations
  • ZonedDateTime - a unique point on the utc_timeline and a location on the clock and calendar
  • Culture - formatting and parsing rules specific to a locale

Time Machine's Goals

  • Flexibility - multiple representations of time to fit different use cases
  • Consistency - works the same across all platforms
  • Testable - easy to test your date and time dependent code
  • Clarity - clear, concise, and intuitive
  • Easy - the library should do the hard things for you

The last two/three? are generic library goals.

Time Machine is a port of Noda Time; use it for all your .NET needs.

Current TZDB Version: 2022a

Example Code:
// Sets up timezone and culture information
await TimeMachine.initialize();
print('Hello, ${DateTimeZone.local} from the Dart Time Machine!\n');

var tzdb = await DateTimeZoneProviders.tzdb;
var paris = await tzdb["Europe/Paris"];

var now = Instant.now();

print('Basic');
print('UTC Time: $now');
print('Local Time: ${now.inLocalZone()}');
print('Paris Time: ${now.inZone(paris)}\n');

print('Formatted');
print('UTC Time: ${now.toString('dddd yyyy-MM-dd HH:mm')}');
print('Local Time: ${now.inLocalZone().toString('dddd yyyy-MM-dd HH:mm')}\n');

var french = await Cultures.getCulture('fr-FR');
print('Formatted and French ($french)');
print('UTC Time: ${now.toString('dddd yyyy-MM-dd HH:mm', french)}');
print('Local Time: ${now.inLocalZone().toString('dddd yyyy-MM-dd HH:mm', french)}\n');

print('Parse French Formatted ZonedDateTime');

// without the 'z' parsing will be forced to interpret the timezone as UTC
var localText = now
    .inLocalZone()
    .toString('dddd yyyy-MM-dd HH:mm z', french);

var localClone = ZonedDateTimePattern
    .createWithCulture('dddd yyyy-MM-dd HH:mm z', french)
    .parse(localText);

print(localClone.value);
VM

selection_116

Flutter

selection_117

Web (Dart2JS and DDC)

selection_118

All unit tests pass on DartVM and DartWeb (just Chrome at this time). Tests have been run on preview versions of Dart2, but the focus is on DartStable, and they are not run before every pub publish. The public API is stabilizing -- mostly focusing on taking C# idiomatic code and making it Dart idiomatic code, so I wouldn't expect any over zealous changes. This is a preview release -- but, I'd feel comfortable using it. (Author Stamp of Approval!)

Documentation was ported, but some things changed for Dart and the documentation is being slowly updated (and we need an additional automated formatting pass).

Don't use any functions annotated with @internal. As of v0.3 you should not find any, but if you do, let me know.

Todo (before v1):

  • [x] Port Noda Time
  • [x] Unit tests passing in DartVM
  • [ ] Dartification of the API
    • [X] First pass style updates
    • [X] Second pass ergonomics updates
    • [X] Synchronous TZDB timezone provider
    • [ ] Review all I/O and associated classes and their structure
    • [ ] Simplify the API and make the best use of named constructors
  • [X] Non-Gregorian/Julian calendar systems
  • [X] Text formatting and Parsing
  • [X] Remove XML tags from documentation and format them for pub (human second pass still needed)
  • [X] Implement Dart4Web features
  • [X] Unit tests passing in DartWeb
  • [ ] Fix DartDoc Formatting
  • [ ] Create simple website with examples (at minimal a good set of examples under the examples directory)

External data: Timezones (TZDB via Noda Time) and Culture (ICU via BCL) are produced by a C# tool that is not included in this repository. The goal is to port all this functionality to Dart, the initial tool was created for bootstrapping -- and guaranteeing that our data is exactly the same thing that Noda Time would see (to ease porting).

Future Todo:

  • [ ] Produce our own TSDB files
  • [ ] Produce our own Culture files
  • [ ] Benchmarking & Optimizing Library for Dart
Flutter Specific Notes

You'll need this entry in your pubspec.yaml.

# The following section is specific to Flutter.
flutter:
  assets:
    - packages/time_machine/data/cultures/cultures.bin
    - packages/time_machine/data/tzdb/tzdb.bin

Your initialization function will look like this:

import 'package:flutter/services.dart';

WidgetsFlutterBinding.ensureInitialized();

// TimeMachine discovers your TimeZone heuristically (it's actually pretty fast).
await TimeMachine.initialize({'rootBundle': rootBundle});

Once flutter gets Isolate.resolvePackageUri functionality, we'll be able to merge VM and the Flutter code paths and no asset entry and no special import will be required. It would look just like the VM example.

Or with: https://pub.dartlang.org/packages/flutter_native_timezone

import 'package:flutter/services.dart';

// you can get Timezone information directly from the native interface with flutter_native_timezone
await TimeMachine.initialize({
  'rootBundle': rootBundle,
  'timeZone': await Timezone.getLocalTimezone(),
});
DDC Specific Notes

toString on many of the classes will not propagate patternText and culture parameters. Instant and ZonedDateTime currently have toStringDDC functions available to remedy this.

This also works:

dynamic foo = new Foo();
var foo = new Foo() as dynamic;
(foo as dynamic).toString(patternText, culture);

We learned in Issue:33876 that dynamic code uses a different flow path. Wrapping your code as dynamic will allow toString() to work normally. It will unfortunately ruin your intellisense.

See Issue:33876 for more information. The fix exists, now we just wait for it to hit a live build.

toStringDDC instead of toStringFormatted to attempt to get a negative contagion coefficient. If you are writing on DartStable today and you need some extra string support because of this bug, let me know.

Update: Dart 2.0 stable did not launch with the fix. Stable release windows are 6 weeks. Hopefully we get the fix in the next release (second half of September).

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目