Files
tidefront/apps/web/src/lib/time.ts
T
db9979 d7dc12a4b2 Pakete 3 und 5: PWA-Oberfläche, Docker Compose, Sicherung
- apps/web: React, Vite, Tailwind, TanStack Query, PixiJS-Hexkarte; Basis, Karte, Truppen,
  Mission planen, Kampfbericht, Börse; untere Navigation mit fünf Zielen; WebSocket-Aktualisierung;
  Countdowns mit Server-Zeitversatz; Manifest und Service Worker
- Dockerfile: ein Image für api, worker, migrate, Oberfläche im api-Container
- docker-compose.yml: api, worker, migrate, postgres, valkey, backup mit Healthchecks, ein Port (8080)
- scripts/backup.sh: tägliche Sicherung, Wiederherstellung und Probe-Wiederherstellung
- README mit Startanleitung, Bauauftrag mit Stand 2
2026-11-26 19:22:22 +01:00

44 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useState } from "react";
let offsetMs = 0;
/** Offset = serverNow − Date.now() beim Empfang. */
export function noteServerTime(serverNow: number): void {
offsetMs = serverNow - Date.now();
}
export function serverNow(): number {
return Date.now() + offsetMs;
}
/** Tick einmal je Sekunde; liefert die aktuelle Serverzeit. */
export function useNow(intervalMs = 1000): number {
const [now, setNow] = useState(serverNow());
useEffect(() => {
const t = setInterval(() => setNow(serverNow()), intervalMs);
return () => clearInterval(t);
}, [intervalMs]);
return now;
}
export function fmtDuration(ms: number): string {
if (ms <= 0) return "gleich";
const s = Math.ceil(ms / 1000);
const d = Math.floor(s / 86400);
const h = Math.floor((s % 86400) / 3600);
const m = Math.floor((s % 3600) / 60);
const sec = s % 60;
if (d > 0) return `${d} T ${h} h`;
if (h > 0) return `${h} h ${String(m).padStart(2, "0")} min`;
if (m > 0) return `${m}:${String(sec).padStart(2, "0")} min`;
return `${sec} s`;
}
export function fmtSeconds(sec: number): string {
return fmtDuration(sec * 1000);
}
export function fmtTime(ms: number): string {
return new Date(ms - offsetMs).toLocaleString("de-DE", { day: "2-digit", month: "2-digit", hour: "2-digit", minute: "2-digit" });
}