Refactor TimefixService to create a local Date object from input and convert it to a UTC string for database storage, ensuring accurate time representation.

This commit is contained in:
Torsten Schulz (local)
2025-10-20 08:53:14 +02:00
parent 2c7a34e50c
commit 12a83cd7ec

View File

@@ -282,9 +282,21 @@ class TimefixService {
}
// Erstelle die korrigierte Zeit als Date-Objekt
// WICHTIG: Frontend sendet lokale Zeit, DB erwartet UTC
const [newHours, newMinutes] = newTime.split(':').map(Number);
const [newYear, newMonth, newDay] = newDate.split('-').map(Number);
const correctedDateTime = new Date(newYear, newMonth - 1, newDay, newHours, newMinutes, 0);
// Erstelle lokales Date-Objekt
const localDateTime = new Date(newYear, newMonth - 1, newDay, newHours, newMinutes, 0);
// Konvertiere zu UTC-String für die Datenbank (Format: YYYY-MM-DD HH:MM:SS)
const utcYear = localDateTime.getUTCFullYear();
const utcMonth = String(localDateTime.getUTCMonth() + 1).padStart(2, '0');
const utcDay = String(localDateTime.getUTCDate()).padStart(2, '0');
const utcHours = String(localDateTime.getUTCHours()).padStart(2, '0');
const utcMinutes = String(localDateTime.getUTCMinutes()).padStart(2, '0');
const utcSeconds = String(localDateTime.getUTCSeconds()).padStart(2, '0');
const correctedDateTime = `${utcYear}-${utcMonth}-${utcDay} ${utcHours}:${utcMinutes}:${utcSeconds}`;
// Prüfe ob bereits ein Timefix existiert
const existingTimefix = await Timefix.findOne({