String formatMedicationPeriod(
  Object? startValue,
  Object? endValue, {
  DateTime? now,
}) {
  final start = DateTime.tryParse(startValue?.toString() ?? '');
  final end = DateTime.tryParse(endValue?.toString() ?? '');
  if (start == null && end == null) return '长期';
  final reference = now ?? DateTime.now();
  if (start == null) return '-- - ${_compactMedicationDate(end!, reference)}';
  final startText = _compactMedicationDate(start, reference, other: end);
  if (end == null) return '$startText - 长期';
  final endText = _compactMedicationDate(end, reference, other: start);
  return '$startText - $endText';
}

String _compactMedicationDate(
  DateTime date,
  DateTime reference, {
  DateTime? other,
}) {
  final showYear =
      date.year != reference.year || (other != null && other.year != date.year);
  final month = date.month.toString().padLeft(2, '0');
  final day = date.day.toString().padLeft(2, '0');
  return showYear ? '${date.year}.$month.$day' : '$month.$day';
}

String formatMedicationFormDate(DateTime value) {
  final month = value.month.toString().padLeft(2, '0');
  final day = value.day.toString().padLeft(2, '0');
  return '${value.year}-$month-$day';
}
