The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/core/internationalization/dates.ts

36 lines
1.2 KiB

import '@formatjs/intl-durationformat/polyfill';
import deepEqual from 'fast-deep-equal';
import memoize from 'micro-memoize';
import { getI18next } from './index';
const deepMemoize: typeof memoize = (fn) => memoize(fn, { isEqual: deepEqual });
const createDateTimeFormatter = deepMemoize((language: string, options: Intl.DateTimeFormatOptions) => {
return new Intl.DateTimeFormat(language, options);
});
const createDurationFormatter = deepMemoize((language: string, options: Intl.DurationFormatOptions) => {
return new Intl.DurationFormat(language, options);
});
export const formatDate = deepMemoize(
(value: number | Date | string, format: Intl.DateTimeFormatOptions = {}): string => {
if (typeof value === 'string') {
return formatDate(new Date(value), format);
}
const i18n = getI18next();
const dateFormatter = createDateTimeFormatter(i18n.language, format);
return dateFormatter.format(value);
}
);
export const formatDuration = deepMemoize(
(duration: Intl.DurationInput, options: Intl.DurationFormatOptions = {}): string => {
const i18n = getI18next();
const dateFormatter = createDurationFormatter(i18n.language, options);
return dateFormatter.format(duration);
}
);