mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-11-22 06:23:01 +00:00
68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
function human_duration_unit(milliseconds) {
|
|
let seconds = Math.floor(milliseconds / 1000);
|
|
if(seconds <= 60) return "second";
|
|
if(seconds <= 60*60) return "minute";
|
|
if(seconds <= 60*60*24) return "hour";
|
|
if(seconds <= 60*60*24*45) return "day";
|
|
if(seconds <= 60*60*24*365) return "month";
|
|
return "year";
|
|
}
|
|
|
|
/**
|
|
* Calculates the time since a particular datetime and returns a
|
|
* human-readable result.
|
|
* @source Ported from PHP in Pepperminty Wiki https://github.com/sbrl/Pepperminty-Wiki/blob/712e954/core/05-functions.php#L55-L89
|
|
* @param {Date} datetime The datetime to convert.
|
|
* @return {string} The time since the given timestamp as a human-readable string.
|
|
*/
|
|
function human_time_since(datetime) {
|
|
return human_time((new Date() - datetime) / 1000);
|
|
}
|
|
/**
|
|
* Renders a given number of seconds as something that humans can understand more easily.
|
|
* @source Ported from PHP in Pepperminty Wiki https://github.com/sbrl/Pepperminty-Wiki/blob/712e954/core/05-functions.php#L55-L89
|
|
* @param {int} seconds The number of seconds to render.
|
|
* @return {string} The rendered time.
|
|
*/
|
|
function human_time(seconds)
|
|
{
|
|
if(seconds < 0) return "the future";
|
|
|
|
let tokens = new Map([
|
|
[ 31536000, 'year' ],
|
|
[ 2592000, 'month' ],
|
|
[ 604800, 'week' ],
|
|
[ 86400, 'day' ],
|
|
[ 3600, 'hour' ],
|
|
[ 60, 'minute' ],
|
|
[ 1, 'second' ]
|
|
]);
|
|
for(let unit of tokens) {
|
|
if (seconds < unit[0]) continue;
|
|
numberOfUnits = Math.floor(seconds / unit[0]);
|
|
return `${numberOfUnits} ${unit[1]}${((numberOfUnits > 1)?'s':'')} ago`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Rounds a Date to match the specified number of minutes.
|
|
* Useful when making API requests to improve caching.
|
|
* @param {Date} date The date to round.
|
|
* @param {int} interval_minutes The number of minutes to round it to. Should be less than 60.
|
|
* @return {void} Doesn't return anything - it mutates the original Date object instead.
|
|
*/
|
|
function round_date_interval(date, interval_minutes) {
|
|
date.setSeconds(0);
|
|
date.setMilliseconds(0);
|
|
date.setMinutes(
|
|
Math.floor(date.getMinutes() / interval_minutes) * interval_minutes
|
|
);
|
|
}
|
|
|
|
export {
|
|
human_duration_unit,
|
|
human_time_since, human_time,
|
|
round_date_interval
|
|
};
|