RandomDTOptions constructor
Initialize RandomDTOptions
-
allowPastDates- Whether to allow resulting DateTime unit's possible values lists to include past dates. -
startYearandendYearare not attributes, but they are used to initialize_years. By default,startYearis 1970 withallowPastDatestrue else the current year. AndendYearisstartYear+ 5 by default. -
months,days,hours,minutes,seconds,millisecondsandmicrosecondsare 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);
}