getTimes function

List<DateTime> getTimes(
  1. Config config,
  2. ArgResults? gRes
)

get times input, order: define -> env -> yaml -> argResults

Implementation

List<DateTime> getTimes(Config config, ArgResults? gRes) {
  List<DateTime> times = [];
  String? timeGe = config.optionalString('time_ge');
  String? timeLe = config.optionalString('time_le');
  timeGe ??= gRes?.option('time_ge');
  timeLe ??= gRes?.option('time_le');

  if (timeLe case String timeLe_ when timeLe_.isNotEmpty) {
    if (timeLe_.contains('ago')) {
      final parsedTime = TimeAgoParser.parse(timeLe_);
      if (parsedTime == null || parsedTime.unit == TimeUnit.unknown) {
        throw UsageException('err: invalid time_le (time ago)', '');
      }
      timeLe_ = parsedTime.toDateTime().toIso8601String();
    }
    final dtSizeLe = DateTime.tryParse(timeLe_);
    if (dtSizeLe == null) throw UsageException('err: invalid time_le', '');
    final epoch = DateTime(1970); // Unix Epoch (1970-01-01 00:00:00 UTC
    times = [epoch, dtSizeLe.toLocal()];
  }
  if (timeGe case String timeGe_ when timeGe_.isNotEmpty) {
    if (timeGe_.contains('ago')) {
      final parsedTime = TimeAgoParser.parse(timeGe_);
      if (parsedTime == null || parsedTime.unit == TimeUnit.unknown) {
        throw UsageException('err: invalid time_ge (time ago)', '');
      }
      timeGe_ = parsedTime.toDateTime().toIso8601String();
    }
    final dtSizeGe = DateTime.tryParse(timeGe_);
    if (dtSizeGe == null) throw UsageException('err: invalid time_ge', '');
    times.insert(0, dtSizeGe.toLocal());
  }
  if (times.length > 1 && times.first.isAfter(times.last)) {
    throw UsageException('err: invalid time_ge and time_le', '');
  }
  return times;
}