Welcome to mirror list, hosted at ThFree Co, Russian Federation.

date_time_picker_lib.js « date_time_picker « components « monitoring « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 604b17baab93c2f3264c41aebf0911f62adf72ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import dateformat from 'dateformat';
import { __ } from '~/locale';
import { secondsToMilliseconds } from '~/lib/utils/datetime_utility';

/**
 * Valid strings for this regex are
 * 2019-10-01 and 2019-10-01 01:02:03
 */
const dateTimePickerRegex = /^(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])(?: (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?$/;

export const defaultTimeWindows = {
  thirtyMinutes: {
    label: __('30 minutes'),
    seconds: 60 * 30,
  },
  threeHours: {
    label: __('3 hours'),
    seconds: 60 * 60 * 3,
  },
  eightHours: {
    label: __('8 hours'),
    seconds: 60 * 60 * 8,
    default: true,
  },
  oneDay: {
    label: __('1 day'),
    seconds: 60 * 60 * 24 * 1,
  },
  threeDays: {
    label: __('3 days'),
    seconds: 60 * 60 * 24 * 3,
  },
};

export const dateFormats = {
  ISODate: "yyyy-mm-dd'T'HH:MM:ss'Z'",
  stringDate: 'yyyy-mm-dd HH:MM:ss',
};

/**
 * The URL params start and end need to be validated
 * before passing them down to other components.
 *
 * @param {string} dateString
 * @returns true if the string is a valid date, false otherwise
 */
export const isValidDate = dateString => {
  try {
    // dateformat throws error that can be caught.
    // This is better than using `new Date()`
    if (dateString && dateString.trim()) {
      dateformat(dateString, 'isoDateTime');
      return true;
    }
    return false;
  } catch (e) {
    return false;
  }
};

export const getTimeRange = (timeWindowKey, timeWindows = defaultTimeWindows) => {
  let difference;
  if (timeWindows[timeWindowKey]) {
    difference = timeWindows[timeWindowKey].seconds;
  } else {
    const [defaultEntry] = Object.entries(timeWindows).filter(
      ([, timeWindow]) => timeWindow.default,
    );
    // find default time window
    difference = defaultEntry[1].seconds;
  }

  const end = Math.floor(Date.now() / 1000); // convert milliseconds to seconds
  const start = end - difference;

  return {
    start: new Date(secondsToMilliseconds(start)).toISOString(),
    end: new Date(secondsToMilliseconds(end)).toISOString(),
  };
};

export const getTimeWindowKey = ({ start, end }, timeWindows = defaultTimeWindows) =>
  Object.entries(timeWindows).reduce((acc, [timeWindowKey, timeWindow]) => {
    if (new Date(end) - new Date(start) === secondsToMilliseconds(timeWindow.seconds)) {
      return timeWindowKey;
    }
    return acc;
  }, null);

/**
 * Convert the input in Time picker component to ISO date.
 *
 * @param {string} val
 * @returns {string}
 */
export const stringToISODate = val =>
  dateformat(new Date(val.replace(/-/g, '/')), dateFormats.ISODate, true);

/**
 * Convert the ISO date received from the URL to string
 * for the Time picker component.
 *
 * @param {Date} date
 * @returns {string}
 */
export const ISODateToString = date => dateformat(date, dateFormats.stringDate);

export const truncateZerosInDateTime = datetime => datetime.replace(' 00:00:00', '');

export const isDateTimePickerInputValid = val => dateTimePickerRegex.test(val);

export default {};