Skip to content

Adds day functionality to to_durations#47

Open
JonahKr wants to merge 5 commits into
custom-cards:masterfrom
JonahKr:to_duration_days
Open

Adds day functionality to to_durations#47
JonahKr wants to merge 5 commits into
custom-cards:masterfrom
JonahKr:to_duration_days

Conversation

@JonahKr

@JonahKr JonahKr commented Oct 16, 2021

Copy link
Copy Markdown
Collaborator

Closes #26

@JonahKr JonahKr changed the title Adds day functionality to to_durations #26 Adds day functionality to to_durations Oct 16, 2021
@JonahKr JonahKr requested a review from iantrich October 23, 2021 12:42
@JonahKr

JonahKr commented Oct 23, 2021

Copy link
Copy Markdown
Collaborator Author

I'm not really sure about my changes since they change the file to a different version from the home assistant one.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds support for displaying days in the duration formatting function secondsToDuration. Previously, long-running equipment durations would display as excessive hours (e.g., "216:23:51" for over 9 days). With this change, durations of one day or more will now display in the format "DD:HH:MM:SS".

Changes:

  • Modified the secondsToDuration function to calculate and display days component in durations
  • Changed the string construction logic to build from seconds upwards, conditionally adding higher time units
  • Renamed the parameter from d to duration for better clarity

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/datetime/seconds_to_duration.ts Outdated
Comment on lines +15 to +17
let string = "" + leftPad(s);
if (duration >= 60) {
string = `${leftPad(m)}:` + string

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.
Comment thread src/datetime/seconds_to_duration.ts Outdated
Comment thread src/datetime/seconds_to_duration.ts Outdated
Comment on lines +7 to +11
adjDuration -= d*86400;
const h = Math.floor(adjDuration / 3600);
adjDuration -= h*3600;
const m = Math.floor(adjDuration / 60);
adjDuration -= m*60;

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.

Missing space around the multiplication operator. Should be m * 60 for consistency with typical JavaScript/TypeScript spacing conventions.

Suggested change
adjDuration -= d*86400;
const h = Math.floor(adjDuration / 3600);
adjDuration -= h*3600;
const m = Math.floor(adjDuration / 60);
adjDuration -= m*60;
adjDuration -= d * 86400;
const h = Math.floor(adjDuration / 3600);
adjDuration -= h * 3600;
const m = Math.floor(adjDuration / 60);
adjDuration -= m * 60;

Copilot uses AI. Check for mistakes.
Comment thread src/datetime/seconds_to_duration.ts Outdated
Comment on lines +6 to +20
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
if (duration >= 3600) {
string = `${leftPad(h)}:` + string
if (duration >= 86400) {

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.
Comment thread src/datetime/seconds_to_duration.ts Outdated
Comment on lines +7 to +11
adjDuration -= d*86400;
const h = Math.floor(adjDuration / 3600);
adjDuration -= h*3600;
const m = Math.floor(adjDuration / 60);
adjDuration -= m*60;

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.

Missing space around the multiplication operator. Should be h * 3600 for consistency with typical JavaScript/TypeScript spacing conventions.

Suggested change
adjDuration -= d*86400;
const h = Math.floor(adjDuration / 3600);
adjDuration -= h*3600;
const m = Math.floor(adjDuration / 60);
adjDuration -= m*60;
adjDuration -= d * 86400;
const h = Math.floor(adjDuration / 3600);
adjDuration -= h * 3600;
const m = Math.floor(adjDuration / 60);
adjDuration -= m * 60;

Copilot uses AI. Check for mistakes.
Comment thread src/datetime/seconds_to_duration.ts Outdated
if (duration >= 3600) {
string = `${leftPad(h)}:` + string
if (duration >= 86400) {
string = `${leftPad(d)}:` + string

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 leftPad function returns different types based on the input value (string for values less than 10, number for 10 or greater). When concatenating leftPad(d) into the string template on this line, it will work due to JavaScript's type coercion, but this creates a type inconsistency. Consider wrapping leftPad(d) with String() for explicit type conversion, or ensure leftPad always returns a string type.

Copilot uses AI. Check for mistakes.
Comment thread src/datetime/seconds_to_duration.ts Outdated
Comment on lines +17 to +21
string = `${leftPad(m)}:` + string
if (duration >= 3600) {
string = `${leftPad(h)}:` + string
if (duration >= 86400) {
string = `${leftPad(d)}:` + string

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.

Line 17 is missing a semicolon at the end, which is inconsistent with the coding style used elsewhere in this function and file (see lines 7, 9, 11, 12, 15, 19, 21, 25).

Suggested change
string = `${leftPad(m)}:` + string
if (duration >= 3600) {
string = `${leftPad(h)}:` + string
if (duration >= 86400) {
string = `${leftPad(d)}:` + string
string = `${leftPad(m)}:` + string;
if (duration >= 3600) {
string = `${leftPad(h)}:` + string;
if (duration >= 86400) {
string = `${leftPad(d)}:` + string;

Copilot uses AI. Check for mistakes.
iantrich and others added 4 commits February 21, 2026 12:15
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@iantrich

Copy link
Copy Markdown
Member

@JonahKr I know it has been a long time, but finally back to it. Love the addition. If you have time to address the test issues and add some tests for the new functionality, that would be great. If I don't hear from you in a week, I'll pick it up and run with it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for days in duration formating

3 participants