Add duration unit format (#2483)
* chore: 🤖 add @formatjs/intl-durationformat to addons/core * refactor: 💡 replace old duration format with new package * chore: 🤖 remove intl-unofficial-duration-unit-format * Add missing copyright headers * test: 💍 add more test cases --------- Co-authored-by: cameronperera <cameronperera@users.noreply.github.com>
This commit is contained in:
1 parent
e395b1196c
commit
9648e1fa3b
8 files changed
+157
-24
No files matched your search
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* SPDX-License-Identifier: BUSL-1.1
|
||||
*/
|
||||
|
||||
/**
|
||||
* Takes in the number of seconds and converts to an object with corresponding
|
||||
* values for each time duration. For example:
|
||||
* { weeks: 0, days: 0, hours: 22, minutes: 15, seconds: 11 }
|
||||
* @param {number} value
|
||||
* @returns {object}
|
||||
*/
|
||||
export const secondsToDuration = (value) => {
|
||||
const second = 1;
|
||||
const minute = second * 60;
|
||||
const hour = minute * 60;
|
||||
const day = hour * 24;
|
||||
const week = day * 7;
|
||||
|
||||
const weeks = Math.max(Math.floor(value / week), 0);
|
||||
const days = Math.max(Math.floor((value % week) / day), 0);
|
||||
const hours = Math.max(Math.floor((value % day) / hour), 0);
|
||||
const minutes = Math.max(Math.floor((value % hour) / minute), 0);
|
||||
const seconds = Math.max(Math.floor((value % minute) / second), 0);
|
||||
|
||||
return { weeks, days, hours, minutes, seconds };
|
||||
};
|
||||
Reference in new issue
Block a user