All files / src/lib watch-history.ts

100% Statements 20/20
100% Branches 8/8
100% Functions 2/2
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31          6x     6x 36x 18x 18x 18x 7x             6x 35x 35x 35x 35x 35x 35x 33x 31x    
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import { formatHHMM } from "@/lib/epg-window";
 
const DAY_MS = 86_400_000;
 
/** Total watch time, such as 45s, 12m or 2h 5m. */
export const formatDuration = (seconds: number): string => {
	if (seconds < 60) return `${seconds}s`;
	const h = Math.floor(seconds / 3600);
	const m = Math.floor((seconds % 3600) / 60);
	if (h === 0) return `${m}m`;
	return `${h}h ${m}m`;
};
 
/**
 * When an entry was last watched. Today shows the time, yesterday says so,
 * and anything older shows the month and day.
 */
export const formatRelativeDate = (unixSeconds: number): string => {
	const date = new Date(unixSeconds * 1000);
	const now = new Date();
	const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	const yesterday = new Date(today.getTime() - DAY_MS);
	const entryDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
	if (entryDay.getTime() === today.getTime()) return `Today ${formatHHMM(unixSeconds)}`;
	if (entryDay.getTime() === yesterday.getTime()) return "Yesterday";
	return date.toLocaleDateString([], { month: "short", day: "numeric" });
};