DurationStyle class abstract final

Reads and writes durations as text, in the two forms configuration files use: the short form 10s and ISO-8601 PT10S.

The default conversion service calls it whenever a string becomes a Duration (a @Value parameter, a @ConfigurationProperties field, getPropertyAs<Duration>), and formatIso when a Duration becomes a string. An application calls it directly only to parse or print durations in its own code, such as a timeout typed by an operator.

  • Short form: a whole number with an optional unit ns, us, ms, s, m, h or d in any case; no unit means milliseconds. For example 500ms, 10s, -2h.
  • ISO-8601: PT10S, P1DT2H30M, PT0.5S, negative as -PT5S or P-1D, as java.time.Duration.parse reads them.
DurationStyle.parse('500ms'); // Duration(milliseconds: 500)
DurationStyle.parse('P1DT2H'); // Duration(days: 1, hours: 2)
DurationStyle.format(const Duration(minutes: 90)); // '90m'
DurationStyle.formatIso(const Duration(minutes: 90)); // 'PT1H30M'

Spring (Java):

Duration d = DurationStyle.detectAndParse("500ms");
Duration iso = DurationStyle.detectAndParse("P1DT2H");
String text = DurationStyle.SIMPLE.print(Duration.ofMinutes(90));
String isoText = DurationStyle.ISO8601.print(Duration.ofMinutes(90));

Differences: Dart's Duration has microsecond precision, so ns values are truncated to microseconds. ISO-8601 years and months are not supported, as in java.time.Duration.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

format(Duration value) → String
Writes value in the short form, with the largest unit that divides it exactly, for logs and messages people read.
formatIso(Duration value) → String
Writes value in ISO-8601, exactly as java.time.Duration.toString (and Spring Boot's DurationStyle.ISO8601) does, so the text can go to a Java service or back into a config file.
parse(String value, {ChronoUnit unit = ChronoUnit.millis}) → Duration
Reads value in either form and throws a FormatException for anything else.