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

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

iso_duration_parser #

pub package tests

A package which parses ISO 8061 Duration PnYnMnDTnHnMnS format.

Usage #

A simple usage example:

import 'package:iso_duration_parser/iso_duration_parser.dart';

void main() {
  final dur = IsoDuration.parse('PT12H30M50.4S');
  dur.hours; // 12.0
  dur.minutes; // 30
  dur.seconds; // 50.4
  dur.toSeconds(); // 45054.5

  final dur2 = IsoDuration.parse('PT36H');
  dur2.days; // 0
  dur2.hours; // 36.0
  dur2.minutes; // 0
  dur2.seconds; // 0
  dur2.toSeconds(); // 129600.0

  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:
  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
  • 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.

10
likes
140
pub points
88%
popularity

Publisher

unverified uploader

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