RandomDTOptions constructor

RandomDTOptions({
  1. bool allowPastDates = false,
  2. int futureYearLimit = 5,
  3. int? startYear,
  4. int? endYear,
  5. List<int>? months,
  6. List<int>? days,
  7. List<int>? hours,
  8. List<int>? minutes,
  9. List<int>? seconds,
  10. List<int>? milliseconds,
  11. List<int>? microseconds,
})

Initialize RandomDTOptions

  • allowPastDates - Whether to allow resulting DateTime unit's possible values lists to include past dates.

  • startYear and endYear are not attributes, but they are used to initialize _years. By default, startYear is 1970 with allowPastDates true else the current year. And endYear is startYear + 5 by default.

  • months, days, hours, minutes, seconds, milliseconds and microseconds are the possible values lists for the corresponding units. If not given the defaults are chosen.

The defaults for these are what you expect, a list of 1 to 12 for months. 0 to 59 for hours, seconds and so on.

For days although, the default is a list of 1 to 31 and it is filtered using the current month and year for different months and leap year later on in the getValidDays method.

Implementation

RandomDTOptions({
  this.allowPastDates = false,
  this.futureYearLimit = 5,
  int? startYear,
  int? endYear,
  List<int>? months,
  List<int>? days,
  List<int>? hours,
  List<int>? minutes,
  List<int>? seconds,
  List<int>? milliseconds,
  List<int>? microseconds,
}) : _now = DateTime.now() {
  startYear ??= allowPastDates ? 1970 : _now.year;
  endYear ??= _now.year + futureYearLimit;
  if (!allowPastDates && endYear < _now.year) {
    throw ArgumentError(
      // ignore: lines_longer_than_80_chars
      'End year [$endYear] cannot be less than current year if allowPastDates is false',
    );
  }

  _years = List<int>.generate(
    endYear - startYear + 1,
    (int i) => i + (startYear ?? (allowPastDates ? 1970 : _now.year)),
  );
  _months = months ?? <int>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
  _days = days ?? List<int>.generate(31, (int index) => index + 1);
  _hours = hours ?? List<int>.generate(24, (int index) => index);
  _minutes = minutes ?? List<int>.generate(60, (int index) => index);
  _seconds = seconds ?? List<int>.generate(60, (int index) => index);
  _milliseconds =
      milliseconds ?? List<int>.generate(1000, (int index) => index);
  _microseconds =
      microseconds ?? List<int>.generate(1000, (int index) => index);
}