minted_chronology 1.1.0 copy "minted_chronology: ^1.1.0" to clipboard
minted_chronology: ^1.1.0 copied to clipboard

Well-modelled Dart value types for calendar dates and durations: Date, Year, Month, DayOfMonth, Weekday and Iso8601Duration. Parse, don't validate. Part of the minted family.

example/minted_chronology_example.dart

// This example prints to stdout so it runs standalone via `dart run`.
// ignore_for_file: avoid_print

import 'package:minted_chronology/minted_chronology.dart';

void main() {
  // The calendar date `DateTime` doesn't model: no time, no zone. Impossible dates are refused, not
  // rolled over.
  // #region date
  final date = Date.tryParse('2026-07-07')!;
  print(date.iso8601); // 2026-07-07
  print(date.month.daysIn(2026)); // 31  (the month is a Month, and knows its length)
  print(date.tryAddDays(30)); // Date(2026-08-06)  (null past the 0000-9999 bound)
  print(date.isBefore(Date.of(2027).getOrThrow())); // true
  print(Date.tryParse('2026-13-01')); // null (no 13th month)
  // The modelled door: only the day can still be wrong, since a month's length needs the other 2.
  final year = Year.tryFrom(2026)!;
  print(
    Date.from(year, MonthConstants.july, DayOfMonthConstants.d7),
  ); // ParseSuccess(Date(2026-07-07))
  print(Date.from(year, MonthConstants.april, DayOfMonthConstants.d31).reasonOrNull?.message);
  // day 31 is outside 1-30 for 2026-04
  // #endregion

  // Holds components, because a month has no length until anchored to a date.
  // #region iso8601Duration
  final span = Iso8601Duration.tryParse('P1Y2M3DT4H')!;
  print(span.iso8601); // P1Y2M3DT4H
  print(span.months); // 2
  print(
    span.toDuration(from: Date.of(2026, 1, 31).getOrThrow()),
  ); // 10252:00:00.000000  (427 days and 4 hours)
  print(
    Iso8601Duration.tryParse('P1M')!.toDuration(from: Date.of(2026, 2).getOrThrow()),
  ); // 672:00:00 (28 days)
  print(Iso8601Duration.tryParse('PT1M')!.iso8601); // PT1M  (a minute; P1M is a month)
  print(Iso8601Duration.parse('P1Y2W').reasonOrNull?.message);
  // the week form PnW cannot carry a "Y" component too
  // #endregion
}
0
likes
160
points
725
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Well-modelled Dart value types for calendar dates and durations: Date, Year, Month, DayOfMonth, Weekday and Iso8601Duration. Parse, don't validate. Part of the minted family.

Homepage
Repository (GitHub)
View/report issues

Topics

#validation #type-safety #dates #domain-driven-design #standards

License

BSD-3-Clause (license)

Dependencies

collection, meta, minted, minted_constraints

More

Packages that depend on minted_chronology