core/src/common/time.ts

174 lines
4.1 KiB
TypeScript

/**
* Convert seconds to HH:MM:SS format
* @param input seconds
* @returns string in HH:MM:SS format
*/
export function toHHMMSS(input: string | number): string {
const secNum = parseInt(input.toString(), 10);
let hours: string | number = Math.floor(secNum / 3600);
let minutes: string | number = Math.floor((secNum - (hours * 3600)) / 60);
let seconds: string | number = secNum - (hours * 3600) - (minutes * 60);
if (hours < 10) {
hours = '0' + hours;
}
if (minutes < 10) {
minutes = '0' + minutes;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
let time = '';
if (parseInt(hours.toString(), 10) > 0) {
time = hours + ':' + minutes + ':' + seconds;
} else {
time = minutes + ':' + seconds;
}
return time;
}
/**
* Add a zero in front of single-digit numbers
*/
function zf(v: number): string {
return v < 9 ? '0' + v : '' + v;
}
/**
* Convert seconds into years days hours minutes seconds
* @param timems time in seconds
* @returns y d h m s string
*/
export function readableTime(timems: number): string {
const time = Math.floor(timems);
if (time < 60) {
return zf(Math.floor(time)) + 's';
} else if (time < 3600) {
return zf(Math.floor(time / 60)) +
'm ' + zf(Math.floor(time % 60)) + 's';
} else if (time < 86400) {
return zf(Math.floor(time / 3600)) +
'h ' + zf(Math.floor((time % 3600) / 60)) +
'm ' + zf(Math.floor((time % 3600) % 60)) + 's';
} else if (time < 31536000) {
return (Math.floor(time / 86400)) +
'd ' + zf(Math.floor((time % 86400) / 3600)) +
'h ' + zf(Math.floor((time % 3600) / 60)) +
'm ' + zf(Math.floor((time % 3600) % 60)) + 's';
} else {
return (Math.floor(time / 31536000)) +
'y ' + zf(Math.floor((time % 31536000) / 86400)) +
'd ' + zf(Math.floor((time % 86400) / 3600)) +
'h ' + zf(Math.floor((time % 3600) / 60)) +
'm ' + zf(Math.floor((time % 3600) % 60)) + 's';
}
}
/**
* Convert y d h m s string to seconds
* @param input y d h m s string
* @returns seconds
*/
export function parseTimeToSeconds(input: string): number {
let seconds = 0;
let match;
const secMinute = 1 * 60;
const secHour = secMinute * 60;
const secDay = secHour * 24;
const secWeek = secDay * 7;
const secYear = secDay * 365;
match = input.match('([0-9]+)y');
if (match != null) {
seconds += +match[1] * secYear;
}
match = input.match('([0-9]+)w');
if (match != null) {
seconds += +match[1] * secWeek;
}
match = input.match('([0-9]+)d');
if (match != null) {
seconds += +match[1] * secDay;
}
match = input.match('([0-9]+)h');
if (match != null) {
seconds += +match[1] * secHour;
}
match = input.match('([0-9]+)m');
if (match != null) {
seconds += +match[1] * secMinute;
}
match = input.match('([0-9]+)s');
if (match != null) {
seconds += +match[1];
}
return seconds;
}
/**
* Add a comma to separate thousands in number
* @param input number (example: 1000000)
* @returns string (example: 1,000,000)
*/
export function thousandsSeparator(input: number | string): string {
const nStr = input.toString();
const x = nStr.split('.');
let x1 = x[0];
const x2 = x.length > 1 ? '.' + x[1] : '';
const rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
/**
* Time since a unix timestamp
* @param date unix timestamp
* @returns x years, months, days, hours, minutes, seconds
*/
export function timeSince(date: number): string {
const seconds = Math.floor((Date.now() - date) / 1000);
let interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + ' years';
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + ' months';
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + ' days';
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + ' hours';
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + ' minutes';
}
return Math.floor(seconds) + ' seconds';
}