Files
flutter/lib/database/converters/date_only_text_converter.dart
Roland Geider 2fbe9d59e1 Merge remote-tracking branch 'origin/master' into fix/midnight-workout-session-split--backend-2379
# Conflicts:
#	lib/features/routines/models/session.dart
#	lib/features/routines/providers/gym_state.dart
#	lib/features/routines/providers/workout_logs_repository.dart
#	lib/features/routines/widgets/forms/session.dart
#	lib/features/routines/widgets/gym_mode/session_page.dart
2026-08-10 17:16:55 +02:00

60 lines
2.4 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 - 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:drift/drift.dart';
/// Persists a Django `DateField` (a calendar day, no time, no zone) as a
/// `YYYY-MM-DD` string, the format the backend stores and syncs back. Without
/// it drift would write the local day as a full ISO timestamp, so a row written
/// here and the same row after a server round-trip would no longer compare
/// equal. Reading takes the leading day, so timestamps written before this
/// converter still map to the correct date.
///
/// Use only on columns backed by a Django `DateField`. For `DateTimeField`
/// columns use `UtcDateTimeConverter` instead.
class DateOnlyTextConverter extends TypeConverter<DateTime, String> {
const DateOnlyTextConverter();
@override
DateTime fromSql(String fromDb) {
final day = DateTime.parse(fromDb.substring(0, 10));
return DateTime.utc(day.year, day.month, day.day);
}
@override
String toSql(DateTime value) =>
'${value.year.toString().padLeft(4, '0')}-'
'${value.month.toString().padLeft(2, '0')}-'
'${value.day.toString().padLeft(2, '0')}';
}
/// Stores a moment as UTC ISO8601, the same wire format the server speaks.
///
/// Keeping every row in UTC with an identical layout means a plain string
/// comparison on the column is also a comparison in time, which the session
/// lookup in the log repository relies on.
class DateTimeTextConverter extends TypeConverter<DateTime, String> {
const DateTimeTextConverter();
@override
DateTime fromSql(String fromDb) => DateTime.parse(fromDb).toLocal();
@override
String toSql(DateTime value) => value.toUtc().toIso8601String();
}