Features: - Backend: Node.js/Express mit MySQL/MariaDB - Frontend: Vue.js 3 mit Composition API - UTC-Zeithandling für korrekte Zeiterfassung - Timewish-basierte Überstundenberechnung - Wochenübersicht mit Urlaubs-/Krankheits-/Feiertagshandling - Bereinigtes Arbeitsende (Generell/Woche) - Überstunden-Offset für historische Daten - Fixed Layout mit scrollbarem Content - Kompakte UI mit grünem Theme
157 lines
5.6 KiB
JavaScript
157 lines
5.6 KiB
JavaScript
/**
|
|
* Timezone-Fix für Worklog-Einträge
|
|
*
|
|
* Problem: Die alte Anwendung hat lokale Zeiten direkt in die DB geschrieben
|
|
* Die DB-Spalte ist DATETIME (ohne Timezone-Info), aber Sequelize interpretiert sie als UTC
|
|
*
|
|
* Beispiel:
|
|
* - Tatsächliche Aktion: 08:07 Uhr lokale Zeit (MEZ/MESZ)
|
|
* - In DB gespeichert: 2025-10-14 08:07:18 (als DATETIME ohne TZ)
|
|
* - Sequelize liest: 2025-10-14T08:07:18.000Z (interpretiert als UTC)
|
|
* - JavaScript zeigt: 10:07 lokal (UTC + 2h MESZ) ❌
|
|
*
|
|
* Lösung: Konvertiere DATETIME zu echtem UTC basierend auf Sommer-/Winterzeit
|
|
*/
|
|
|
|
const mysql = require('mysql2/promise');
|
|
const path = require('path');
|
|
require('dotenv').config({ path: path.join(__dirname, '.env') });
|
|
|
|
/**
|
|
* Prüft, ob ein Datum in der Sommerzeit liegt
|
|
* Sommerzeit in Europa: letzter Sonntag im März (02:00) bis letzter Sonntag im Oktober (03:00)
|
|
*/
|
|
function isSummertime(date) {
|
|
const year = date.getFullYear();
|
|
|
|
// Finde letzten Sonntag im März
|
|
const marchEnd = new Date(year, 2, 31);
|
|
const marchSunday = 31 - marchEnd.getDay();
|
|
const summerStart = new Date(year, 2, marchSunday, 2, 0, 0);
|
|
|
|
// Finde letzten Sonntag im Oktober
|
|
const octoberEnd = new Date(year, 9, 31);
|
|
const octoberSunday = 31 - octoberEnd.getDay();
|
|
const summerEnd = new Date(year, 9, octoberSunday, 3, 0, 0);
|
|
|
|
return date >= summerStart && date < summerEnd;
|
|
}
|
|
|
|
async function fixTimezones() {
|
|
let connection;
|
|
|
|
try {
|
|
connection = await mysql.createConnection({
|
|
host: process.env.DB_HOST || 'localhost',
|
|
user: process.env.DB_USER || 'root',
|
|
password: process.env.DB_PASSWORD || '',
|
|
database: process.env.DB_NAME || 'stechuhr2',
|
|
timezone: '+00:00' // UTC
|
|
});
|
|
|
|
console.log('✅ Datenbankverbindung hergestellt\n');
|
|
|
|
// Hole alle Worklog-Einträge
|
|
const [rows] = await connection.execute(
|
|
'SELECT id, tstamp FROM worklog ORDER BY id ASC'
|
|
);
|
|
|
|
console.log(`📊 Gefunden: ${rows.length} Worklog-Einträge\n`);
|
|
|
|
const updates = [];
|
|
|
|
for (const row of rows) {
|
|
// row.tstamp ist ein JavaScript Date-Objekt
|
|
// MySQL hat die lokale Zeit gespeichert (z.B. 08:07:18)
|
|
// Sequelize interpretiert das als UTC
|
|
|
|
// Wir erstellen ein neues Date-Objekt aus den Komponenten
|
|
const storedDate = new Date(row.tstamp);
|
|
|
|
// Extrahiere die Komponenten (das sind die "falschen" UTC-Werte)
|
|
const year = storedDate.getUTCFullYear();
|
|
const month = storedDate.getUTCMonth();
|
|
const day = storedDate.getUTCDate();
|
|
const hours = storedDate.getUTCHours();
|
|
const minutes = storedDate.getUTCMinutes();
|
|
const seconds = storedDate.getUTCSeconds();
|
|
|
|
// Diese Komponenten sind eigentlich die lokale Zeit
|
|
// Erstelle ein Datum mit diesen Werten als lokale Zeit
|
|
const localDate = new Date(year, month, day, hours, minutes, seconds);
|
|
|
|
// Prüfe ob Sommer- oder Winterzeit
|
|
const offset = isSummertime(localDate) ? 2 : 1;
|
|
|
|
// Konvertiere zu UTC: Lokale Zeit - Offset
|
|
const utcDate = new Date(localDate.getTime() - (offset * 60 * 60 * 1000));
|
|
|
|
updates.push({
|
|
id: row.id,
|
|
old: `${year}-${(month+1).toString().padStart(2,'0')}-${day.toString().padStart(2,'0')} ${hours.toString().padStart(2,'0')}:${minutes.toString().padStart(2,'0')}:${seconds.toString().padStart(2,'0')}`,
|
|
new: utcDate.toISOString().replace('T', ' ').replace('.000Z', ''),
|
|
offset: offset
|
|
});
|
|
}
|
|
|
|
// Zeige Statistiken
|
|
const summerCount = updates.filter(u => u.offset === 2).length;
|
|
const winterCount = updates.filter(u => u.offset === 1).length;
|
|
|
|
console.log(`📋 Analyse-Ergebnis:`);
|
|
console.log(` - Sommerzeit (UTC+2): ${summerCount} Einträge`);
|
|
console.log(` - Winterzeit (UTC+1): ${winterCount} Einträge`);
|
|
console.log(` - Gesamt zu korrigieren: ${updates.length}`);
|
|
|
|
// Zeige erste 10 Beispiele
|
|
console.log(`\n📝 Beispiele (erste 10):`);
|
|
updates.slice(0, 10).forEach(u => {
|
|
console.log(` ID ${u.id}: ${u.old} (lokal) → ${u.new} (UTC, offset: -${u.offset}h)`);
|
|
});
|
|
|
|
console.log(`\n⚠️ WARNUNG: Diese Operation ändert ${updates.length} Einträge in der Datenbank!`);
|
|
console.log(`\n📝 Backup erstellen:`);
|
|
console.log(` mysqldump -u ${process.env.DB_USER} -p ${process.env.DB_NAME} worklog > worklog_backup_$(date +%Y%m%d_%H%M%S).sql`);
|
|
console.log(`\nFühre das Script mit dem Parameter 'apply' aus, um die Änderungen anzuwenden:`);
|
|
console.log(` node backend/fix-timezone.js apply\n`);
|
|
|
|
// Nur anwenden, wenn 'apply' als Parameter übergeben wurde
|
|
if (process.argv.includes('apply')) {
|
|
console.log('🔄 Wende Korrekturen an...\n');
|
|
|
|
let updatedCount = 0;
|
|
for (const update of updates) {
|
|
await connection.execute(
|
|
'UPDATE worklog SET tstamp = ? WHERE id = ?',
|
|
[update.new, update.id]
|
|
);
|
|
updatedCount++;
|
|
|
|
if (updatedCount % 100 === 0) {
|
|
console.log(` ${updatedCount}/${updates.length} aktualisiert...`);
|
|
}
|
|
}
|
|
|
|
console.log(`\n✅ ${updatedCount} Einträge erfolgreich aktualisiert!`);
|
|
console.log(`\n🔄 Bitte starte den Backend-Server neu, um die Änderungen zu sehen.`);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ Fehler:', error);
|
|
process.exit(1);
|
|
} finally {
|
|
if (connection) {
|
|
await connection.end();
|
|
console.log('\n🔌 Datenbankverbindung geschlossen');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Script ausführen
|
|
fixTimezones().then(() => {
|
|
process.exit(0);
|
|
}).catch(err => {
|
|
console.error('Fataler Fehler:', err);
|
|
process.exit(1);
|
|
});
|