IsoDuration class

IsoDuration (also known as ISO 8061 Duration Data Type), is a duration format where each value represents an individual duration part.

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

Example:

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

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

Note: This IsoDuration is not like Dart's implementation of Duration.

Despite the similar name, Dart's Duration object does not implement "Durations" as specified by ISO 8601. In particular, this object does not keep track of the individually provided members (such as "days" or "hours"), but only uses these arguments to compute the length of the corresponding time interval (sum of all individual parts).

See also:

Constructors

IsoDuration({double years = 0, double months = 0, double weeks = 0, double days = 0, double hours = 0, double minutes = 0, double seconds = 0})
IsoDuration (also known as ISO 8061 Duration Data Type), is a duration format where each value represents an individual duration part.
const

Properties

days double
The duration part in days.
final
hasDecimals bool
Returns true if any property of the IsoDuration has decimals.
no setter
hashCode int
The hash code for this object.
no setteroverride
hours double
The duration part in hours.
final
isNegative bool
Returns true if the IsoDuration is negative.
no setter
isPrecise bool
Returns true if both years and months are equal to 0, meaning that toSeconds method can be accurately calculated.
no setter
isZero bool
Returns true if all values of IsoDuration object are equal to 0.
no setter
minutes double
The duration part in minutes.
final
months double
The duration part in months.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
seconds double
The duration part in seconds.
final
weeks double
The duration part in weeks.
final
years double
The duration part in years.
final

Methods

copyWith({double? years, double? months, double? weeks, double? days, double? hours, double? minutes, double? seconds}) IsoDuration
Creates a copy of this object with the given fields replaced with the new values.
format(String format) String
Returns formatted String with a specified format.
inverse() IsoDuration
Inverses IsoDuration.
isAfter(IsoDuration other) bool
Returns true if this IsoDuration this occurs after the other.
isAtSameMomentAs(IsoDuration other) bool
Returns true if this IsoDuration occurs at the same time as other.
isBefore(IsoDuration other) bool
Returns true if this IsoDuration occurs before the other.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toIso() String
This method returns a String value from the IsoDuration object in ISO 8601 - Duration (PnYnMnDTnHnMnS format).
toSeconds() double
Calculates total duration in seconds as a sum of all individual parts (except years and months) from the IsoDuration object.
toString() String
A string representation of this object.
override
withDate(DateTime dateTime) DateTime
Adds current IsoDuration to a specified dateTime (or subtracts if IsoDuration is negative).

Operators

operator ==(Object other) bool
The equality operator.
override

Static Methods

parse(String input) IsoDuration
Parses the ISO 8601 - Duration. If the operation was not successful then it throws FormatException.
tryParse(String input) IsoDuration?
Like parse but safely parses the ISO 8601 - Duration and returns null if the input is invalid.