iso_duration_parser 1.1.1 copy "iso_duration_parser: ^1.1.1" to clipboard
iso_duration_parser: ^1.1.1 copied to clipboard

A package which parses ISO 8061 Duration (PnYnMnDTnHnMnS format).

iso_duration_parser #

pub package tests codecov

A package which parses ISO 8061 Duration PnYnMnDTnHnMnS format.

It is a duration format where each value represents an individual duration part.

Usage #

A simple usage example:

void main() {
  final dur = IsoDuration.parse('PT36H');
  dur.days; // 0
  dur.hours; // 36.0
  dur.minutes; // 0
  dur.seconds; // 0
  dur.toSeconds(); // 129600.0
  dur.toIso(); // 'PT36H'
  final dateTime = DateTime(2021, 1, 20, 8); // 2021-01-20 08:00:00.000
  dur.withDate(dateTime); // 2021-01-21 20:00:00.000

  final durFormat = IsoDuration.parse('PT4H5M');
  durFormat.format('Call me in {hh}h {m}m'); // Call me in 04h 5m

  final dur2 = IsoDuration.parse('PT12H30M50.4S');
  dur2.hours; // 12.0
  dur2.minutes; // 30
  dur2.seconds; // 50.4
  dur2.toSeconds(); // 45054.5
  dur2.toIso(); // 'PT12H30M50.4S'

  final dur3 = IsoDuration.parse('P5Y'); // IsoDuration{years: 5, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0};
  //dur3.toSeconds(); // assertion error, years and months must be equal to 0

  IsoDuration.parse('P3Y6M4DT12H30M5S'); // IsoDuration{years: 3, months: 6, weeks: 0, days: 4, hours: 12, minutes: 30, seconds: 5};

  // can parse decimal (accepts both comma and dots):
  IsoDuration.parse('PT8M40.215S'); // IsoDuration{years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 8, seconds: 40.215};

  try {
    IsoDuration.parse('P 50M');
  } on FormatException {
    // invalid input in parse throws FormatException
  }
  IsoDuration.tryParse('P 50M'); // invalid input in tryParse returns null
}

Duration ISO 8061 #

Duration Data Type

The duration data type is used to specify a time interval.

This format is often used as response value in REST APIs (YouTube video duration or length of the flight).

The time interval is specified in the following form PnYnMnDTnHnMnS where:

  • P indicates the period (required)
  • nY indicates the number of years
  • nM indicates the number of months
  • nW indicates the number of weeks
  • nD indicates the number of days
  • T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds)
  • nH indicates the number of hours
  • nM indicates the number of minutes
  • nS indicates the number of seconds

... read more

Features and bugs #

Please file feature requests and bugs at the issue tracker or feel free to raise a PR to the linked issue.

11
likes
160
points
13.6k
downloads

Publisher

unverified uploader

Weekly Downloads

A package which parses ISO 8061 Duration (PnYnMnDTnHnMnS format).

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on iso_duration_parser