34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
/**
|
|
* Converts milliseconds into a human-readable time string with localized units
|
|
* @param {number} milliseconds - The time in milliseconds to convert
|
|
* @param {Object} client - The client object containing translation functionality
|
|
* @param {string} lang - The language code for translations
|
|
* @returns {string} Formatted time string with localized units
|
|
*/
|
|
function fetchTime(milliseconds, client, lang) {
|
|
if (!Number.isFinite(milliseconds) || milliseconds < 0) {
|
|
throw new Error('Invalid milliseconds value provided');
|
|
}
|
|
|
|
const timeUnits = [
|
|
{ value: 31536000, key: 'years' },
|
|
{ value: 86400, key: 'days' },
|
|
{ value: 3600, key: 'hours' },
|
|
{ value: 60, key: 'minutes' },
|
|
{ value: 1, key: 'seconds' }
|
|
];
|
|
|
|
let remainingSeconds = Math.floor(milliseconds / 1000);
|
|
|
|
return timeUnits
|
|
.map(({ value, key }) => {
|
|
const unitValue = Math.floor(remainingSeconds / value);
|
|
remainingSeconds %= value;
|
|
return unitValue ? `${unitValue} ${client.translate.get(lang, `Functions.fetchTime.${key}`)},` : '';
|
|
})
|
|
.filter(Boolean)
|
|
.join(' ')
|
|
.trim();
|
|
}
|
|
|
|
module.exports = fetchTime;
|