add method

dynamic add(
  1. dynamic lhs
)

Implementation

dynamic add(dynamic lhs) {
  if (!(unit == 'year' ||
      unit == 'month' ||
      unit == 'week' ||
      unit == 'day' ||
      unit == 'hour' ||
      unit == 'minute' ||
      unit == 'second' ||
      unit == 'milisecond')) {
    throw FhirPathEvaluationException(
      'Date & Time additions must be done with the proper units.\n'
      '$lhs + $amount $unit was attempted, this is invalid',
      cause: lhs,
      operation: '+',
      arguments: [lhs, this],
    );
  }
  final yearAmount = (unit == 'year'
          ? amount
          : unit == 'month'
              ? (amount / 12).truncate()
              : 0)
      .toInt();
  final monthAmount = (unit == 'month' ? amount.remainder(12) : 0).toInt();
  final dayAmount = (unit == 'week'
          ? amount * 7
          : unit == 'day'
              ? amount
              : 0)
      .toInt();
  final hourAmount = (unit == 'hour' ? amount : 0).toInt();
  final minuteAmount = (unit == 'minute' ? amount : 0).toInt();
  final secondAmount = (unit == 'second' ? amount : 0).toInt();
  final millisecondAmount = (unit == 'millisecond' ? amount : 0).toInt();
  if ((lhs is primitives.FhirDate &&
          (hourAmount != 0 ||
              minuteAmount != 0 ||
              secondAmount != 0 ||
              millisecondAmount != 0)) ||
      (lhs is primitives.FhirTime &&
          (yearAmount != 0 || monthAmount != 0 || dayAmount != 0))) {
    throw FhirPathEvaluationException(
      'Date & Time additions must be done with the proper units.\n'
      '$lhs + $amount $unit was attempted, this is invalid',
      cause: lhs,
      operation: '+',
      arguments: [lhs, this],
    );
  }
  if (lhs is primitives.FhirDate && lhs.isValid && lhs.value != null) {
    final newDate = DateTime.utc(lhs.value!.year + yearAmount,
        lhs.value!.month + monthAmount, lhs.value!.day + dayAmount);
    if (lhs.precision == primitives.DatePrecision.YYYY) {
      return primitives.FhirDate(newDate.toString().substring(0, 4));
    } else if (lhs.precision == primitives.DatePrecision.YYYYMM) {
      return primitives.FhirDate(newDate.toString().substring(0, 7));
    } else {
      return primitives.FhirDate(newDate.toString().substring(0, 10));
    }
  } else if (lhs is primitives.FhirTime && lhs.isValid && lhs.value != null) {
    final timeList = lhs.value!.split(':');
    final duration = Duration(
      hours: int.tryParse(timeList.first) ?? 0 + hourAmount,
      minutes: (timeList.length > 1 ? int.tryParse(timeList[1]) ?? 0 : 0) +
          minuteAmount,
      seconds: (timeList.length > 2
              ? int.tryParse(timeList[2].split('.').first) ?? 0
              : 0) +
          secondAmount,
      milliseconds: (timeList.length > 2 && timeList[2].split('.').length > 1
              ? int.tryParse(timeList[2].split('.').last) ?? 0
              : 0) +
          millisecondAmount,
    );
    final durationList = duration.toString().split(':');
    durationList.first =
        int.parse(durationList.first).remainder(24).toString();
    return primitives.FhirTime(durationList.join(':'));
  } else if (lhs is primitives.FhirDateTime &&
      lhs.isValid &&
      lhs.value != null) {
    final oldDateTime = lhs.value!;
    final newDateTime = DateTime.utc(
      oldDateTime.year + yearAmount,
      oldDateTime.month + monthAmount,
      oldDateTime.day + dayAmount,
      oldDateTime.hour + hourAmount,
      oldDateTime.minute + minuteAmount,
      oldDateTime.second + secondAmount,
      oldDateTime.millisecond + millisecondAmount,
    );
    if (lhs.precision == primitives.DateTimePrecision.YYYY) {
      return primitives.FhirDateTime(
          newDateTime.toIso8601String().substring(0, 4));
    } else if (lhs.precision == primitives.DateTimePrecision.YYYYMM) {
      return primitives.FhirDateTime(
          newDateTime.toIso8601String().substring(0, 7));
    } else if (lhs.precision == primitives.DateTimePrecision.YYYYMMDD) {
      return primitives.FhirDateTime(
          newDateTime.toIso8601String().substring(0, 10));
    } else {
      return primitives.FhirDateTime(newDateTime);
    }
  }
}