toString method

  1. @override
String toString()
override

Texto no formato de saída padrão do PostgreSQL (IntervalStyle postgres), por exemplo 1 year 2 mons 3 days 04:05:06.789.

Implementation

@override
String toString() {
  final years = months ~/ 12;
  final mons = months.remainder(12);
  var time = microseconds;
  final hour = time ~/ _usecsPerHour;
  time -= hour * _usecsPerHour;
  final min = time ~/ _usecsPerMinute;
  time -= min * _usecsPerMinute;
  final sec = time ~/ _usecsPerSec;
  final fsec = time - sec * _usecsPerSec;

  final buf = StringBuffer();
  var isBefore = false;
  var isZero = true;
  void addPart(int value, String units) {
    if (value == 0) return;
    buf
      ..write(isZero ? '' : ' ')
      ..write(isBefore && value > 0 ? '+' : '')
      ..write(value)
      ..write(' ')
      ..write(units)
      ..write(value != 1 ? 's' : '');
    isBefore = value < 0;
    isZero = false;
  }

  addPart(years, 'year');
  addPart(mons, 'mon');
  addPart(days, 'day');
  if (isZero || hour != 0 || min != 0 || sec != 0 || fsec != 0) {
    final minus = hour < 0 || min < 0 || sec < 0 || fsec < 0;
    buf
      ..write(isZero ? '' : ' ')
      ..write(minus ? '-' : (isBefore ? '+' : ''))
      ..write(_two(hour.abs()))
      ..write(':')
      ..write(_two(min.abs()))
      ..write(':')
      ..write(_seconds(sec.abs(), fsec.abs()));
  }
  return buf.toString();
}