2021-05-08 09:30:57 +00:00
|
|
|
import { parse, toDate, getHours } from 'date-fns';
|
|
|
|
import { format, utcToZonedTime } from 'date-fns-tz';
|
2020-09-12 17:21:35 +00:00
|
|
|
import humanizeDuration from 'humanize-duration';
|
2020-06-05 09:22:14 +00:00
|
|
|
|
2020-08-25 17:52:07 +00:00
|
|
|
export function formatPercent(value: number, decimals = 3): string {
|
|
|
|
return value ? `${(value * 100).toFixed(decimals)}%` : '';
|
2020-06-02 11:05:16 +00:00
|
|
|
}
|
2020-06-04 17:47:44 +00:00
|
|
|
|
2021-02-03 06:53:38 +00:00
|
|
|
export function formatPrice(value: number, decimals = 8): string {
|
|
|
|
return value ? value.toFixed(decimals) : '';
|
2020-06-04 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 18:32:37 +00:00
|
|
|
export function dateFromString(datestring: string, format: string): Date {
|
2021-05-08 09:30:57 +00:00
|
|
|
return parse(datestring, format, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let timezone = 'UTC';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set global timezone to use by conversion functions
|
|
|
|
* @param tz Timezone to set
|
|
|
|
*/
|
|
|
|
export function setTimezone(tz: string) {
|
|
|
|
timezone = tz;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param ts Convert timestamp or Date to datetime (in correct timezone)
|
|
|
|
* @returns Date object (in timezone)
|
|
|
|
*/
|
2021-05-08 09:34:05 +00:00
|
|
|
function convertToDate(ts: number | Date): Date {
|
2021-05-08 09:30:57 +00:00
|
|
|
const date = toDate(ts);
|
|
|
|
const currentTz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
|
|
if (timezone === 'UTC') {
|
|
|
|
return utcToZonedTime(date, currentTz);
|
|
|
|
}
|
|
|
|
return date;
|
2020-12-05 18:32:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 05:51:03 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param ts Timestamp as number or date (in utc!!)
|
|
|
|
*/
|
2020-07-13 19:40:05 +00:00
|
|
|
export function timestampms(ts: number | Date): string {
|
2021-05-08 09:34:05 +00:00
|
|
|
return format(convertToDate(ts), 'yyyy-MM-dd HH:mm:ss');
|
2020-06-05 09:22:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 15:25:14 +00:00
|
|
|
/**
|
2021-05-08 09:30:57 +00:00
|
|
|
* Converts timestamp or Date object to yyyy-MM-dd format.
|
2020-07-11 15:25:14 +00:00
|
|
|
* @param ts
|
|
|
|
*/
|
|
|
|
export function timestampToDateString(ts: number | Date): string {
|
2021-05-08 09:34:05 +00:00
|
|
|
return format(convertToDate(ts), 'yyyy-MM-dd');
|
2020-07-11 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 15:25:14 +00:00
|
|
|
/**
|
2021-05-08 09:30:57 +00:00
|
|
|
* Converts a String of the format yyyy-MM-dd to YYYYMMDD. To be used as timerange.
|
|
|
|
* @param datestring Input string (in the format yyyy-MM-dd)
|
2020-07-11 15:25:14 +00:00
|
|
|
*/
|
|
|
|
export function dateStringToTimeRange(datestring: string): string {
|
|
|
|
return datestring.replace(/-/g, '');
|
2020-07-11 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 09:30:57 +00:00
|
|
|
export function timestampHour(ts: number): number {
|
2021-05-08 09:34:05 +00:00
|
|
|
return getHours(convertToDate(ts));
|
2020-08-17 19:15:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:21:35 +00:00
|
|
|
/**
|
|
|
|
* Get humanized Duration from seconds
|
|
|
|
* @param duration Duration in seconds
|
|
|
|
*/
|
|
|
|
export function humanizeDurationFromSeconds(duration: number): string {
|
|
|
|
return humanizeDuration(duration * 1000);
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:47:44 +00:00
|
|
|
export default {
|
|
|
|
formatPrice,
|
|
|
|
formatPercent,
|
2020-06-05 09:22:14 +00:00
|
|
|
timestampms,
|
2020-07-11 15:15:13 +00:00
|
|
|
timestampToDateString,
|
|
|
|
dateStringToTimeRange,
|
2021-05-08 09:30:57 +00:00
|
|
|
setTimezone,
|
2020-06-04 17:47:44 +00:00
|
|
|
};
|