toString method

  1. @override
String toString({
  1. bool showDate = true,
  2. bool showTime = false,
  3. bool showHour = true,
  4. bool showMinute = true,
  5. bool showSecond = true,
  6. bool persianDigits = false,
  7. JalaliEra era = JalaliEra.solarHijri,
})
override

Returns the date as yyyy/MM/dd, optionally followed by the time.

The time is HH:mm:ss, zero-padded. showHour, showMinute, and showSecond drop the matching part and only apply when showTime is true.

Throws ArgumentError when both showDate and showTime are false, or when showTime is true and every time part is disabled. Throws StateError when showTime is true but this instance was created from a Jalali date and therefore carries no time of day.

With persianDigits, the digits are written as ۰ to ۹. The year is numbered in era, Solar Hijri by default.

Implementation

@override
String toString({
  bool showDate = true,
  bool showTime = false,
  bool showHour = true,
  bool showMinute = true,
  bool showSecond = true,
  bool persianDigits = false,
  JalaliEra era = JalaliEra.solarHijri,
}) {
  if (!showDate && !showTime) {
    throw ArgumentError('At least one of showDate or showTime must be true.');
  }
  if (showTime && !showHour && !showMinute && !showSecond) {
    throw ArgumentError(
      'showTime requires at least one of showHour, showMinute, or showSecond.',
    );
  }
  final parts = <String>[];
  if (showDate) {
    parts.add('${year + era.offset}/${twoDigits(month)}/${twoDigits(day)}');
  }
  if (showTime) {
    final dateTime = _dateTime;
    if (dateTime == null) {
      throw StateError(
        'Cannot show time for a PersianDate created without a Gregorian DateTime.',
      );
    }
    parts.add(
      [
        if (showHour) twoDigits(dateTime.hour),
        if (showMinute) twoDigits(dateTime.minute),
        if (showSecond) twoDigits(dateTime.second),
      ].join(':'),
    );
  }
  final text = parts.join(' ');
  return persianDigits ? toPersianDigits(text) : text;
}