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: aec67a18a05a63e30c7884d4421f4dbe99f60ec3 (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
import dateformat from 'dateformat';
import { __ } from '~/locale';

/**
 * 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 = {
  /**
   * Format used by users to input dates
   *
   * Note: Should be a format that can be parsed by Date.parse.
   */
  inputFormat: 'yyyy-mm-dd HH:MM:ss',
  /**
   * Format used to strip timezone from inputs
   */
  stripTimezoneFormat: "yyyy-mm-dd'T'HH:MM:ss'Z'",
};

/**
 * Returns true if the date can be parsed succesfully after
 * being typed by a user.
 *
 * It allows some ambiguity so validation is not strict.
 *
 * @param {string} value - Value as typed by the user
 * @returns true if the value can be parsed as a valid date, false otherwise
 */
export const isValidInputString = (value) => {
  try {
    // dateformat throws error that can be caught.
    // This is better than using `new Date()`
    if (value && value.trim()) {
      dateformat(value, 'isoDateTime');
      return true;
    }
    return false;
  } catch (e) {
    return false;
  }
};

/**
 * Convert the input in time picker component to an ISO date.
 *
 * @param {string} value
 * @param {Boolean} utc - If true, it forces the date to by
 * formatted using UTC format, ignoring the local time.
 * @returns {Date}
 */
export const inputStringToIsoDate = (value, utc = false) => {
  let date = new Date(value);
  if (utc) {
    // Forces date to be interpreted as UTC by stripping the timezone
    // by formatting to a string with 'Z' and skipping timezone
    date = dateformat(date, dateFormats.stripTimezoneFormat);
  }
  return dateformat(date, 'isoUtcDateTime');
};

/**
 * Converts a iso date string to a formatted string for the Time picker component.
 *
 * @param {String} ISO Formatted date
 * @returns {string}
 */
export const isoDateToInputString = (date, utc = false) =>
  dateformat(date, dateFormats.inputFormat, utc);