From a937d34df204888cca51aaf335dcf2ea6b48b833 Mon Sep 17 00:00:00 2001 From: DragonSlayer_14 Date: Thu, 1 Jan 2026 23:19:29 +0100 Subject: [PATCH] =?UTF-8?q?Feat:=20F=C3=BCgt=20eine=20Kasse=20mit=20Funkti?= =?UTF-8?q?onen=20f=C3=BCr=20den=20Umgang=20mit=20Datum-Werten?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Services/date_service.dart | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/Services/date_service.dart 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; + } +}