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

date_time_picker_lib.js « date_time_picker « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 673d981cf0754afe3dc0a6c0d757bfcfeeaae4f0 (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
import dateformat from 'dateformat';
import { __ } from '~/locale';

/**
 * 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]))?$/;

/**
 * Default time ranges for the date picker.
 * @see app/assets/javascripts/lib/utils/datetime_range.js
 */
export const defaultTimeRanges = [
  {
    duration: { seconds: 60 * 30 },
    label: __('30 minutes'),
  },
  {
    duration: { seconds: 60 * 60 * 3 },
    label: __('3 hours'),
  },
  {
    duration: { seconds: 60 * 60 * 8 },
    label: __('8 hours'),
    default: true,
  },
  {
    duration: { seconds: 60 * 60 * 24 * 1 },
    label: __('1 day'),
  },
];

export const defaultTimeRange = defaultTimeRanges.find(tr => tr.default);

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;
  }
};

/**
 * 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 {};