123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import { dateTime, dateTimeFormat } from '@grafana/data';
- import { describeTimeRange } from '@grafana/data/src/datetime/rangeutil';
- import { ReportTime, ReportTimeRange } from '../../types';
- /**
- * Combine date, time and timezone into a single DateTime string and set the timezone to UTC
- * @param date
- * @param time
- * @param timeZone
- */
- export const createDate = (date: Date | string, time: ReportTime = { hour: 0, minute: 0 }, timeZone?: string) => {
- const timeString = `${time.hour}:${time.minute}:00`;
- const formattedDate = typeof date === 'string' ? getDate(date) : date;
- const dateObj = new Date(`${dateTime(formattedDate).format('YYYY/MM/DD')} ${timeString}`);
- const offset = dateTimeFormat(dateObj.getTime(), {
- timeZone,
- format: 'Z',
- });
- return removeTimeZone(dateTime(dateObj).format()) + offset;
- };
- const removeTimeZone = (date: string) => {
- if (date.includes('Z')) {
- return date.replace('Z', '');
- }
- return date.slice(0, -6);
- };
- /**
- * Extract date part from datetime string
- * @param date
- */
- export const getDate = (date?: string) => {
- if (!date) {
- return '';
- }
- return dateTime(date.split('T')[0]).toDate();
- };
- /**
- * Pad time with 0, if it has one digit
- */
- export const padTime = (time: number | string) => {
- if (String(time).length === 1) {
- return `0${time}`;
- }
- return String(time);
- };
- /**
- * Extract time as {hour, minute} from datetime string
- * @param date
- */
- export const getTime = (date?: string) => {
- if (!date) {
- return { hour: 0, minute: 0 };
- }
- const match = date.match(/T(\d+):(\d+)/);
- if (match) {
- const [, hour, minute] = match;
- return { hour: parseInt(hour, 10), minute: parseInt(minute, 10) };
- }
- return { hour: 0, minute: 0 };
- };
- /**
- * Check if date is in the past
- * @param date
- */
- export const isPast = (date?: string) => {
- if (!date) {
- return false;
- }
- return dateTime(date).isBefore(dateTime());
- };
- export const getFormatted = (date?: string) => {
- if (!date) {
- return { time: undefined, date: undefined };
- }
- return {
- date: dateTimeFormat(date, { format: 'MMMM DD, YYYY' }),
- time: dateTimeFormat(date, { format: 'HH:mm' }),
- };
- };
- export const getTimeRangeDisplay = (timeRange: ReportTimeRange) => {
- if (!timeRange.from || !timeRange.to) {
- return 'Dashboard time range';
- }
- const from = parseInt(timeRange.from, 10);
- const to = parseInt(timeRange.to, 10);
- if (!isNaN(from) && !isNaN(to)) {
- return describeTimeRange({ from: dateTime(from), to: dateTime(to) });
- }
- return describeTimeRange(timeRange);
- };
|