Dart API Referencedart:coreDuration

Duration class

A Duration represents a time span. A duration can be negative.

class Duration implements Comparable<Duration> {
 static const int MICROSECONDS_PER_MILLISECOND = 1000;
 static const int MILLISECONDS_PER_SECOND = 1000;
 static const int SECONDS_PER_MINUTE = 60;
 static const int MINUTES_PER_HOUR = 60;
 static const int HOURS_PER_DAY = 24;

 static const int MICROSECONDS_PER_SECOND =
     MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND;
 static const int MICROSECONDS_PER_MINUTE =
     MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE;
 static const int MICROSECONDS_PER_HOUR =
     MICROSECONDS_PER_MINUTE * MINUTES_PER_HOUR;
 static const int MICROSECONDS_PER_DAY =
     MICROSECONDS_PER_HOUR * HOURS_PER_DAY;


 static const int MILLISECONDS_PER_MINUTE =
     MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
 static const int MILLISECONDS_PER_HOUR =
     MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR;
 static const int MILLISECONDS_PER_DAY =
     MILLISECONDS_PER_HOUR * HOURS_PER_DAY;

 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;

 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;

 static const Duration ZERO = const Duration(seconds: 0);

 /**
  * This [Duration] in microseconds.
  */
 final int _duration;

 /**
  * The duration is the sum of all individual parts. This means that individual
  * parts don't need to be less than the next-bigger unit. For example [hours]
  * is allowed to have a value greater than 23.
  *
  * All individual parts are allowed to be negative.
  * All arguments are by default 0.
  */
 const Duration({int days: 0,
                 int hours: 0,
                 int minutes: 0,
                 int seconds: 0,
                 int milliseconds: 0,
                 int microseconds: 0})
     : _duration = days * MICROSECONDS_PER_DAY +
                   hours * MICROSECONDS_PER_HOUR +
                   minutes * MICROSECONDS_PER_MINUTE +
                   seconds * MICROSECONDS_PER_SECOND +
                   milliseconds * MICROSECONDS_PER_MILLISECOND +
                   microseconds;

 /**
  * Returns the sum of this [Duration] and [other]  as a new [Duration].
  */
 Duration operator +(Duration other) {
   return new Duration(microseconds: _duration + other._duration);
 }

 /**
  * Returns the difference of this [Duration] and [other] as a new
  * [Duration].
  */
 Duration operator -(Duration other) {
   return new Duration(microseconds: _duration - other._duration);
 }

 /**
  * Multiplies this [Duration] by the given [factor] and returns the result
  * as a new [Duration].
  */
 Duration operator *(int factor) {
   return new Duration(microseconds: _duration * factor);
 }

 /**
  * Divides this [Duration] by the given [quotient] and returns the truncated
  * result as a new [Duration].
  *
  * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`.
  */
 Duration operator ~/(int quotient) {
   // By doing the check here instead of relying on "~/" below we get the
   // exception even with dart2js.
   if (quotient == 0) throw new IntegerDivisionByZeroException();
   return new Duration(microseconds: _duration ~/ quotient);
 }

 bool operator <(Duration other) => this._duration < other._duration;

 bool operator >(Duration other) => this._duration > other._duration;

 bool operator <=(Duration other) => this._duration <= other._duration;

 bool operator >=(Duration other) => this._duration >= other._duration;

 /**
  * This [Duration] in days. Incomplete days are discarded
  */
 int get inDays => _duration ~/ Duration.MICROSECONDS_PER_DAY;

 /**
  * This [Duration] in hours. Incomplete hours are discarded.
  *
  * The returned value can be greater than 23.
  */
 int get inHours => _duration ~/ Duration.MICROSECONDS_PER_HOUR;

 /**
  * This [Duration] in minutes. Incomplete minutes are discarded.
  *
  * The returned value can be greater than 59.
  */
 int get inMinutes => _duration ~/ Duration.MICROSECONDS_PER_MINUTE;

 /**
  * This [Duration] in seconds. Incomplete seconds are discarded.
  *
  * The returned value can be greater than 59.
  */
 int get inSeconds => _duration ~/ Duration.MICROSECONDS_PER_SECOND;

 /**
  * This [Duration] in milliseconds. Incomplete milliseconds are discarded.
  *
  * The returned value can be greater than 999.
  */
 int get inMilliseconds => _duration ~/ Duration.MICROSECONDS_PER_MILLISECOND;

 /**
  * This [Duration] in microseconds.
  */
 int get inMicroseconds => _duration;

 bool operator ==(other) {
   if (other is !Duration) return false;
   return _duration == other._duration;
 }

 int get hashCode => _duration.hashCode;

 int compareTo(Duration other) => _duration.compareTo(other._duration);

 String toString() {
   String sixDigits(int n) {
     if (n >= 100000) return "$n";
     if (n >= 10000) return "0$n";
     if (n >= 1000) return "00$n";
     if (n >= 100) return "000$n";
     if (n > 10) return "0000$n";
     return "00000$n";
   }
   String twoDigits(int n) {
     if (n >= 10) return "$n";
     return "0$n";
   }

   if (inMicroseconds < 0) {
     Duration duration =
         new Duration(microseconds: -inMicroseconds);
     return "-$duration";
   }
   String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR));
   String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE));
   String sixDigitUs =
       sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND));
   return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
 }
}

Implements

Comparable<Duration>

Static Properties

const int HOURS_PER_DAY #

static const int HOURS_PER_DAY = 24

const int MICROSECONDS_PER_DAY #

static const int MICROSECONDS_PER_DAY =
   MICROSECONDS_PER_HOUR * HOURS_PER_DAY

const int MICROSECONDS_PER_HOUR #

static const int MICROSECONDS_PER_HOUR =
   MICROSECONDS_PER_MINUTE * MINUTES_PER_HOUR

const int MICROSECONDS_PER_MILLISECOND #

static const int MICROSECONDS_PER_MILLISECOND = 1000

const int MICROSECONDS_PER_MINUTE #

static const int MICROSECONDS_PER_MINUTE =
   MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE

const int MICROSECONDS_PER_SECOND #

static const int MICROSECONDS_PER_SECOND =
   MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND

const int MILLISECONDS_PER_DAY #

static const int MILLISECONDS_PER_DAY =
   MILLISECONDS_PER_HOUR * HOURS_PER_DAY

const int MILLISECONDS_PER_HOUR #

static const int MILLISECONDS_PER_HOUR =
   MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR

const int MILLISECONDS_PER_MINUTE #

static const int MILLISECONDS_PER_MINUTE =
   MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE

const int MILLISECONDS_PER_SECOND #

static const int MILLISECONDS_PER_SECOND = 1000

const int MINUTES_PER_DAY #

static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY

const int MINUTES_PER_HOUR #

static const int MINUTES_PER_HOUR = 60

const int SECONDS_PER_DAY #

static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY

const int SECONDS_PER_HOUR #

static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR

const int SECONDS_PER_MINUTE #

static const int SECONDS_PER_MINUTE = 60

const Duration ZERO #

static const Duration ZERO = const Duration(seconds: 0)

Constructors

const Duration({int days: 0, int hours: 0, int minutes: 0, int seconds: 0, int milliseconds: 0, int microseconds: 0}) #

The duration is the sum of all individual parts. This means that individual parts don't need to be less than the next-bigger unit. For example hours is allowed to have a value greater than 23.

All individual parts are allowed to be negative. All arguments are by default 0.

const Duration({int days: 0,
               int hours: 0,
               int minutes: 0,
               int seconds: 0,
               int milliseconds: 0,
               int microseconds: 0})
   : _duration = days * MICROSECONDS_PER_DAY +
                 hours * MICROSECONDS_PER_HOUR +
                 minutes * MICROSECONDS_PER_MINUTE +
                 seconds * MICROSECONDS_PER_SECOND +
                 milliseconds * MICROSECONDS_PER_MILLISECOND +
                 microseconds;

Properties

final int hashCode #

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

docs inherited from Object
int get hashCode => _duration.hashCode;

final int inDays #

This Duration in days. Incomplete days are discarded

int get inDays => _duration ~/ Duration.MICROSECONDS_PER_DAY;

final int inHours #

This Duration in hours. Incomplete hours are discarded.

The returned value can be greater than 23.

int get inHours => _duration ~/ Duration.MICROSECONDS_PER_HOUR;

final int inMicroseconds #

This Duration in microseconds.

int get inMicroseconds => _duration;

final int inMilliseconds #

This Duration in milliseconds. Incomplete milliseconds are discarded.

The returned value can be greater than 999.

int get inMilliseconds => _duration ~/ Duration.MICROSECONDS_PER_MILLISECOND;

final int inMinutes #

This Duration in minutes. Incomplete minutes are discarded.

The returned value can be greater than 59.

int get inMinutes => _duration ~/ Duration.MICROSECONDS_PER_MINUTE;

final int inSeconds #

This Duration in seconds. Incomplete seconds are discarded.

The returned value can be greater than 59.

int get inSeconds => _duration ~/ Duration.MICROSECONDS_PER_SECOND;

Operators

Duration operator +(Duration other) #

Returns the sum of this Duration and other as a new Duration.

Duration operator +(Duration other) {
 return new Duration(microseconds: _duration + other._duration);
}

Duration operator -(Duration other) #

Returns the difference of this Duration and other as a new Duration.

Duration operator -(Duration other) {
 return new Duration(microseconds: _duration - other._duration);
}

Duration operator *(int factor) #

Multiplies this Duration by the given factor and returns the result as a new Duration.

Duration operator *(int factor) {
 return new Duration(microseconds: _duration * factor);
}

Duration operator ~/(int quotient) #

Divides this Duration by the given quotient and returns the truncated result as a new Duration.

Throws an IntegerDivisionByZeroException if quotient is 0.

Duration operator ~/(int quotient) {
 // By doing the check here instead of relying on "~/" below we get the
 // exception even with dart2js.
 if (quotient == 0) throw new IntegerDivisionByZeroException();
 return new Duration(microseconds: _duration ~/ quotient);
}

bool operator <(Duration other) #

bool operator <(Duration other) => this._duration < other._duration;

bool operator <=(Duration other) #

bool operator <=(Duration other) => this._duration <= other._duration;

bool operator >(Duration other) #

bool operator >(Duration other) => this._duration > other._duration;

bool operator >=(Duration other) #

bool operator >=(Duration other) => this._duration >= other._duration;

bool operator ==(other) #

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator ==(other) {
 if (other is !Duration) return false;
 return _duration == other._duration;
}

Methods

int compareTo(Duration other) #

Compares this object to another Comparable

Returns a value like a Comparator when comparing this to other.

May throw an ArgumentError if other is of a type that is not comparable to this.

docs inherited from Comparable<Duration>
int compareTo(Duration other) => _duration.compareTo(other._duration);

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() {
 String sixDigits(int n) {
   if (n >= 100000) return "$n";
   if (n >= 10000) return "0$n";
   if (n >= 1000) return "00$n";
   if (n >= 100) return "000$n";
   if (n > 10) return "0000$n";
   return "00000$n";
 }
 String twoDigits(int n) {
   if (n >= 10) return "$n";
   return "0$n";
 }

 if (inMicroseconds < 0) {
   Duration duration =
       new Duration(microseconds: -inMicroseconds);
   return "-$duration";
 }
 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR));
 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE));
 String sixDigitUs =
     sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND));
 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
}