diff --git a/lib/Services/date_service.dart b/lib/Services/date_service.dart new file mode 100644 index 0000000..4caba1f --- /dev/null +++ b/lib/Services/date_service.dart @@ -0,0 +1,45 @@ +import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; + +/// Eine Service-Klasse um mit Datums-Werten umzugehen +class DateService { + /// Die Standard-Formatierung von einem Datum + static final DateFormat dateFormat = DateFormat('dd.MM.yyyy'); + + /// Wandelt einen String in eine DateTimeRange um + static DateTimeRange? stringToDateTimeRange(final String? value) { + if (value == null || value.trim().isEmpty) { + return null; + } + + final List parts = value.split(' - '); + if (parts.length != 2) { + return null; + } + + try { + final DateTime start = DateService.dateFormat.parseStrict( + parts[0].trim(), + ); + final DateTime end = DateService.dateFormat.parseStrict(parts[1].trim()); + + if (end.isBefore(start)) { + return null; + } + + return DateTimeRange(start: start, end: end); + } on FormatException catch (_) { + return null; + } + } + + /// Wandelt eine DateTimeRange in einen String um + static String? dateTimeRangeToString(final DateTimeRange? value) { + if (value != null) { + return '${dateFormat.format(value.start)}' + ' - ' + '${dateFormat.format(value.end)}'; + } + return null; + } +}