dateTime.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { dateTime, dateTimeFormat } from '@grafana/data';
  2. import { describeTimeRange } from '@grafana/data/src/datetime/rangeutil';
  3. import { ReportTime, ReportTimeRange } from '../../types';
  4. /**
  5. * Combine date, time and timezone into a single DateTime string and set the timezone to UTC
  6. * @param date
  7. * @param time
  8. * @param timeZone
  9. */
  10. export const createDate = (date: Date | string, time: ReportTime = { hour: 0, minute: 0 }, timeZone?: string) => {
  11. const timeString = `${time.hour}:${time.minute}:00`;
  12. const formattedDate = typeof date === 'string' ? getDate(date) : date;
  13. const dateObj = new Date(`${dateTime(formattedDate).format('YYYY/MM/DD')} ${timeString}`);
  14. const offset = dateTimeFormat(dateObj.getTime(), {
  15. timeZone,
  16. format: 'Z',
  17. });
  18. return removeTimeZone(dateTime(dateObj).format()) + offset;
  19. };
  20. const removeTimeZone = (date: string) => {
  21. if (date.includes('Z')) {
  22. return date.replace('Z', '');
  23. }
  24. return date.slice(0, -6);
  25. };
  26. /**
  27. * Extract date part from datetime string
  28. * @param date
  29. */
  30. export const getDate = (date?: string) => {
  31. if (!date) {
  32. return '';
  33. }
  34. return dateTime(date.split('T')[0]).toDate();
  35. };
  36. /**
  37. * Pad time with 0, if it has one digit
  38. */
  39. export const padTime = (time: number | string) => {
  40. if (String(time).length === 1) {
  41. return `0${time}`;
  42. }
  43. return String(time);
  44. };
  45. /**
  46. * Extract time as {hour, minute} from datetime string
  47. * @param date
  48. */
  49. export const getTime = (date?: string) => {
  50. if (!date) {
  51. return { hour: 0, minute: 0 };
  52. }
  53. const match = date.match(/T(\d+):(\d+)/);
  54. if (match) {
  55. const [, hour, minute] = match;
  56. return { hour: parseInt(hour, 10), minute: parseInt(minute, 10) };
  57. }
  58. return { hour: 0, minute: 0 };
  59. };
  60. /**
  61. * Check if date is in the past
  62. * @param date
  63. */
  64. export const isPast = (date?: string) => {
  65. if (!date) {
  66. return false;
  67. }
  68. return dateTime(date).isBefore(dateTime());
  69. };
  70. export const getFormatted = (date?: string) => {
  71. if (!date) {
  72. return { time: undefined, date: undefined };
  73. }
  74. return {
  75. date: dateTimeFormat(date, { format: 'MMMM DD, YYYY' }),
  76. time: dateTimeFormat(date, { format: 'HH:mm' }),
  77. };
  78. };
  79. export const getTimeRangeDisplay = (timeRange: ReportTimeRange) => {
  80. if (!timeRange.from || !timeRange.to) {
  81. return 'Dashboard time range';
  82. }
  83. const from = parseInt(timeRange.from, 10);
  84. const to = parseInt(timeRange.to, 10);
  85. if (!isNaN(from) && !isNaN(to)) {
  86. return describeTimeRange({ from: dateTime(from), to: dateTime(to) });
  87. }
  88. return describeTimeRange(timeRange);
  89. };