https://github.com/mzdm/iso_duration_parser
A package which parses ISO 8061 Duration (PnYnMnDTnHnMnS format).
https://github.com/mzdm/iso_duration_parser
Last synced: 3 months ago
JSON representation
A package which parses ISO 8061 Duration (PnYnMnDTnHnMnS format).
- Host: GitHub
- URL: https://github.com/mzdm/iso_duration_parser
- Owner: mzdm
- License: mit
- Created: 2021-02-25T20:37:46.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-16T14:27:24.000Z (almost 4 years ago)
- Last Synced: 2025-01-02T02:43:10.966Z (5 months ago)
- Language: Dart
- Homepage: https://pub.dev/packages/iso_duration_parser
- Size: 34.2 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# iso_duration_parser
[](https://pub.dev/packages/iso_duration_parser)

[](https://codecov.io/gh/mzdm/iso_duration_parser)A package which parses [ISO 8061 Duration](https://en.wikipedia.org/wiki/ISO_8601#Durations) PnYnMnDTnHnMnS format.
It is a duration format where each value represents an **individual** duration part.
## Usage
A simple usage example:
```dart
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.000final durFormat = IsoDuration.parse('PT4H5M');
durFormat.format('Call me in {hh}h {m}m'); // Call me in 04h 5mfinal 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 0IsoDuration.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](https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-search/api-reference)).
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](https://www.w3schools.com/xml/schema_dtypes_date.asp)
## Features and bugs
Please file feature requests and bugs at the [issue tracker](https://github.com/mzdm/iso_duration_parser/issues) or feel free to raise a PR to the linked issue.