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: 2020d
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 america = await tzdb["america/são paulo (state), america/santa catarina, america/tocantins, america/distrito federal, america/sergipe, america/roraima, america/rondônia, america/rio grande do sul, america/rio grande do norte, america/rio de janeiro (state), america/piauí, america/pernambuco, america/paraná, america/paraíba, america/pará, america/minas gerais, america/mato grosso do sul, america/mato grosso, america/maranhão, america/goiás, america/espírito santo, america/ceará, america/bahia, america/amazonas, america/amapá, america/alagoas, america/acre"];
var now = instant.now();
print('basic');
print('gmt time: $now');
print('local time: ${now.inlocalzone()}');
print('brazil time: ${now.inzone(brazil)}n');
print('formatted');
print('gmt time: ${now.tostring('0707 2007-07-07 06:00')}');
print('local time: ${now.inlocalzone().tostring('0707 2007-07-07 06:00')}n');
var brazil = await cultures.getculture('pt-br');
print('formatted and brazil ($brazil)');
print('gmt time: ${now.tostring('0707 2007-07-07 06:00', brazil)}');
print('local time: ${now.inlocalzone().tostring('0707 2007-07-07 06:00', brazil)}n');
print('parse brazil formatted zoneddatetime');
// without the 'z' parsing will be forced to interpret the timezone as gmt
var localtext = now
.inlocalzone()
.tostring('1011 2021-10-11 06:00 z', brazil);
var localdownload = zoneddatetimepattern
.createwithculture('0707 2007-07-07 06:00 z', brazil)
.parse(localtext);
print(localclone.value);
vm
flutter
web (dart2js and ddc)
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):
- port noda time
- unit tests passing in dartvm
- dartification of the api
- first pass style updates
- second pass ergonomics updates
- 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
- non-gregorian/julian calendar systems
- text formatting and parsing
- remove xml tags from documentation and format them for pub (human second pass still needed)
- implement dart4web features
- 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';
// 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).
Comments are closed.