Refactor TimefixService to simplify local time extraction from timestamps and improve UTC handling, ensuring consistent time representation without redundant conversions.
This commit is contained in:
@@ -62,21 +62,10 @@ class TimefixService {
|
||||
// Verwende korrigierte Werte aus Timefix
|
||||
const tf = timefixForEntry[0];
|
||||
|
||||
// WICHTIG: DB speichert UTC, Frontend erwartet lokale Zeit
|
||||
if (typeof tf.fix_date_time === 'string') {
|
||||
// Parse als UTC und konvertiere zu lokaler Zeit
|
||||
const [datePart, timePart] = tf.fix_date_time.split(' ');
|
||||
const [h, m, s] = timePart.split(':').map(Number);
|
||||
|
||||
// Erstelle UTC-Date aus den Komponenten
|
||||
const [year, month, day] = datePart.split('-').map(Number);
|
||||
const utcDate = new Date(Date.UTC(year, month - 1, day, h, m, s));
|
||||
|
||||
// Extrahiere lokale Stunden/Minuten
|
||||
hours = utcDate.getHours();
|
||||
minutes = utcDate.getMinutes();
|
||||
[hours, minutes] = timePart.split(':').map(Number);
|
||||
} else if (tf.fix_date_time instanceof Date) {
|
||||
// Date-Objekt: Verwende lokale Zeit
|
||||
hours = tf.fix_date_time.getHours();
|
||||
minutes = tf.fix_date_time.getMinutes();
|
||||
}
|
||||
@@ -84,23 +73,12 @@ class TimefixService {
|
||||
action = tf.fix_type;
|
||||
} else {
|
||||
// Keine Korrektur - verwende Original-Werte aus Worklog
|
||||
// WICHTIG: DB speichert UTC, Frontend erwartet lokale Zeit
|
||||
if (typeof entry.tstamp === 'string') {
|
||||
// Parse als UTC und konvertiere zu lokaler Zeit
|
||||
const [datePart, timePart] = entry.tstamp.split(' ');
|
||||
const [h, m, s] = timePart.split(':').map(Number);
|
||||
|
||||
// Erstelle UTC-Date aus den Komponenten
|
||||
const [year, month, day] = datePart.split('-').map(Number);
|
||||
const utcDate = new Date(Date.UTC(year, month - 1, day, h, m, s));
|
||||
|
||||
// Extrahiere lokale Stunden/Minuten
|
||||
hours = utcDate.getHours();
|
||||
minutes = utcDate.getMinutes();
|
||||
[hours, minutes] = timePart.split(':').map(Number);
|
||||
} else if (entry.tstamp instanceof Date) {
|
||||
// Date-Objekt: Verwende lokale Zeit
|
||||
hours = entry.tstamp.getHours();
|
||||
minutes = entry.tstamp.getMinutes();
|
||||
hours = entry.tstamp.getUTCHours();
|
||||
minutes = entry.tstamp.getUTCMinutes();
|
||||
}
|
||||
|
||||
// Parse state
|
||||
@@ -282,21 +260,9 @@ 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);
|
||||
|
||||
// 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}`;
|
||||
const correctedDateTime = new Date(newYear, newMonth - 1, newDay, newHours, newMinutes, 0);
|
||||
|
||||
// Prüfe ob bereits ein Timefix existiert
|
||||
const existingTimefix = await Timefix.findOne({
|
||||
|
||||
Reference in New Issue
Block a user