Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/datetime/seconds_to_duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

const leftPad = (num: number) => (num < 10 ? `0${num}` : num);

export default function secondsToDuration(d: number) {
const h = Math.floor(d / 3600);
const m = Math.floor((d % 3600) / 60);
const s = Math.floor((d % 3600) % 60);
export function secondsToDuration(duration: number) {
if (duration < 1) return null;
let adjDuration = duration;
const d = Math.floor(adjDuration / 86400);
adjDuration -= d * 86400;
const h = Math.floor(adjDuration / 3600);
adjDuration -= h * 3600;
const m = Math.floor(adjDuration / 60);
adjDuration -= m * 60;
const s = Math.floor(adjDuration);

if (h > 0) {
return `${h}:${leftPad(m)}:${leftPad(s)}`;
// Constructing string from seconds upwards
let string = "" + leftPad(s);
if (duration >= 60) {
string = `${leftPad(m)}:` + string
Comment on lines +17 to +19

Copilot AI Feb 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new implementation changes behavior for durations less than 60 seconds. Previously, single-digit seconds would be displayed without zero-padding (e.g., "5" for 5 seconds), but now they will be zero-padded (e.g., "05"). This is a breaking change that may affect existing users. Consider whether this is intentional, and if not, avoid using leftPad for the initial seconds value when no higher time units are present.

Suggested change
let string = "" + leftPad(s);
if (duration >= 60) {
string = `${leftPad(m)}:` + string
let string = "" + s;
if (duration >= 60) {
string = `${leftPad(m)}:${leftPad(s)}`;

Copilot uses AI. Check for mistakes.
if (duration >= 3600) {
string = `${leftPad(h)}:` + string;
if (duration >= 86400) {
Comment on lines +8 to +22

Copilot AI Feb 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 86400 (seconds in a day) is used multiple times without explanation. Consider defining it as a named constant (e.g., const SECONDS_PER_DAY = 86400) at the top of the file for better code clarity and maintainability.

Copilot uses AI. Check for mistakes.
string = `${leftPad(d)}:` + string;
}
}
}
if (m > 0) {
return `${m}:${leftPad(s)}`;
}
if (s > 0) {
return "" + s;
}
return null;
return string;
}
Loading