fix: round seconds to minutes in Duration (#34020)
This commit is contained in:
parent
391f08e63b
commit
2955e65525
2 changed files with 19 additions and 3 deletions
|
|
@ -49,6 +49,24 @@ context("Utils", () => {
|
||||||
seconds: 0,
|
seconds: 0,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
run_util("seconds_to_duration", 60 * 60, { hide_seconds: 1 }).then((duration) => {
|
||||||
|
expect(duration).to.deep.equal({
|
||||||
|
days: 0,
|
||||||
|
hours: 1,
|
||||||
|
minutes: 0,
|
||||||
|
seconds: 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
run_util("seconds_to_duration", 15 * 60, { hide_seconds: 1 }).then((duration) => {
|
||||||
|
expect(duration).to.deep.equal({
|
||||||
|
days: 0,
|
||||||
|
hours: 0,
|
||||||
|
minutes: 15,
|
||||||
|
seconds: 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should parse days, hours, minutes and seconds", () => {
|
it("should parse days, hours, minutes and seconds", () => {
|
||||||
|
|
|
||||||
|
|
@ -1144,8 +1144,6 @@ Object.assign(frappe.utils, {
|
||||||
|
|
||||||
seconds_to_duration(seconds, duration_options) {
|
seconds_to_duration(seconds, duration_options) {
|
||||||
const floor = seconds > 0 ? Math.floor : Math.ceil;
|
const floor = seconds > 0 ? Math.floor : Math.ceil;
|
||||||
const round_base_60 = (seconds) => floor(seconds / 60 + (seconds > 0 ? 0.5 : -0.5));
|
|
||||||
|
|
||||||
const total_duration = {
|
const total_duration = {
|
||||||
days: floor(seconds / 86400), // 60 * 60 * 24
|
days: floor(seconds / 86400), // 60 * 60 * 24
|
||||||
hours: floor((seconds % 86400) / 3600),
|
hours: floor((seconds % 86400) / 3600),
|
||||||
|
|
@ -1159,7 +1157,7 @@ Object.assign(frappe.utils, {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (duration_options && duration_options.hide_seconds) {
|
if (duration_options && duration_options.hide_seconds) {
|
||||||
total_duration.minutes += round_base_60(total_duration.seconds);
|
total_duration.minutes += Math.round(total_duration.seconds / 60);
|
||||||
total_duration.seconds = 0;
|
total_duration.seconds = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue