Debian Codegraph @

node/@patternfly/react-core/src/components/TimePicker/TimePickerUtils.tsx

raw
1 export const amSuffix = ' AM';
2 export const pmSuffix = ' PM';
3
4 export const makeTimeOptions = (
5 stepMinutes: number,
6 hour12: boolean,
7 delimiter: string,
8 minTime: string,
9 maxTime: string,
10 includeSeconds: boolean
11 ) => {
12 const res = [];
13 const iter = new Date(new Date().setHours(0, 0, 0, 0));
14 const iterDay = iter.getDay();
15 while (iter.getDay() === iterDay) {
16 let hour: string | number = iter.getHours();
17 let suffix = amSuffix;
18 if (hour12) {
19 if (hour === 0) {
20 hour = 12; // 12am
21 } else if (hour >= 12) {
22 suffix = pmSuffix;
23 }
24 if (hour > 12) {
25 hour %= 12;
26 }
27 }
28 hour = hour12 ? hour.toString() : hour.toString().padStart(2, '0');
29 const minutes = iter.getMinutes().toString().padStart(2, '0');
30 const timeOption = `${hour}${delimiter}${minutes}${hour12 ? suffix : ''}`;
31 // time option is valid if within min/max constraints
32 if (isWithinMinMax(minTime, maxTime, timeOption, delimiter, includeSeconds)) {
33 res.push(timeOption);
34 }
35 iter.setMinutes(iter.getMinutes() + stepMinutes);
36 }
37 return res;
38 };
39
40 export const parseTime = (
41 time: string | Date,
42 timeRegex: RegExp,
43 delimiter: string,
44 is12Hour: boolean,
45 includeSeconds: boolean
46 ): string => {
47 const date = new Date(time);
48
49 // if default time is a ISO 8601 formatted date string, we parse it to hh:mm(am/pm) format
50 if (!isNaN(date.getDate()) && (time instanceof Date || time.includes('T'))) {
51 const hours = is12Hour
52 ? `${date.getHours() > 12 ? date.getHours() - 12 : date.getHours()}`
53 : `${date.getHours()}`.padStart(2, '0');
54 const minutes = `${date.getMinutes()}`.padStart(2, '0');
55 const seconds = includeSeconds ? `${date.getSeconds()}`.padStart(2, '0') : '';
56 const secondsWithDelimiter = seconds ? `${delimiter}${seconds}` : '';
57 let ampm = '';
58
59 if (is12Hour && date.getHours() > 11) {
60 ampm = pmSuffix;
61 } else if (is12Hour) {
62 ampm = amSuffix;
63 }
64
65 return `${hours}${delimiter}${minutes}${secondsWithDelimiter}${ampm}`;
66 } else if (typeof time === 'string') {
67 time = time.trim();
68 if (time !== '' && validateTime(time, timeRegex, delimiter, is12Hour)) {
69 const [, hours, minutes, seconds, suffix = ''] = timeRegex.exec(time);
70 const secondsWithDelimiter = includeSeconds ? `${delimiter}${seconds ?? '00'}` : '';
71 let ampm = '';
72
73 // Format AM/PM according to design
74 if (is12Hour) {
75 const uppercaseSuffix = suffix.toUpperCase();
76
77 if (uppercaseSuffix === amSuffix.toUpperCase().trim()) {
78 ampm = amSuffix;
79 } else if (uppercaseSuffix === pmSuffix.toUpperCase().trim()) {
80 ampm = pmSuffix;
81 } else {
82 // if this 12 hour time is missing am/pm but otherwise valid,
83 // append am/pm depending on time of day
84 ampm = new Date().getHours() > 11 ? pmSuffix : amSuffix;
85 }
86 }
87
88 return `${hours}${delimiter}${minutes}${secondsWithDelimiter}${ampm}`;
89 }
90 }
91 return time.toString();
92 };
93
94 export const validateTime = (time: string, timeRegex: RegExp, delimiter: string, is12Hour: boolean) => {
95 // ISO 8601 format is valid
96 const date = new Date(time);
97 if (!isNaN(date.getDate()) && time.includes('T')) {
98 return true;
99 }
100 // hours only valid if they are [0-23] or [1-12]
101 const hours = parseInt(time.split(delimiter)[0]);
102 const validHours = hours >= (is12Hour ? 1 : 0) && hours <= (is12Hour ? 12 : 23);
103 // minutes verified by timeRegex
104
105 // empty string is valid
106 return time === '' || (timeRegex.test(time) && validHours);
107 };
108
109 export const getHours = (time: string, timeRegex: RegExp) => {
110 const parts = time.match(timeRegex);
111 if (parts && parts.length) {
112 if (/pm/i.test(parts[4])) {
113 return parseInt(parts[1]) === 12 ? parseInt(parts[1]) : parseInt(parts[1]) + 12;
114 }
115 if (/am/i.test(parts[4])) {
116 return parseInt(parts[1]) === 12 ? 0 : parseInt(parts[1]);
117 }
118 return parseInt(parts[1]);
119 }
120 return null;
121 };
122
123 export const getMinutes = (time: string, timeRegex: RegExp) => {
124 const parts = time.match(timeRegex);
125 return parts && parts.length ? parseInt(parts[2]) : null;
126 };
127
128 export const getSeconds = (time: string, timeRegex: RegExp) => {
129 const seconds = time.match(timeRegex)?.[3];
130
131 return seconds ? parseInt(seconds) : null;
132 };
133
134 export const isWithinMinMax = (
135 minTime: string,
136 maxTime: string,
137 time: string,
138 delimiter: string,
139 includeSeconds?: boolean
140 ) => {
141 // do not throw error if empty string
142 if (time.trim() === '') {
143 return true;
144 }
145
146 // correctly format as 24hr times (12:30AM => 00:30, 1:15 => 01:15)
147 const min24HourTime = convertTo24Hour(minTime, delimiter, includeSeconds);
148 const selected24HourTime = convertTo24Hour(time, delimiter, includeSeconds);
149 const max24HourTime = convertTo24Hour(maxTime, delimiter, includeSeconds);
150
151 // simple string comparison for 24hr times
152 return min24HourTime <= selected24HourTime && selected24HourTime <= max24HourTime;
153 };
154
155 const convertTo24Hour = (time: string, delimiter: string, includeSeconds: boolean): string => {
156 const timeReg = new RegExp(`^\\s*(\\d\\d?)${delimiter}([0-5]\\d)${delimiter}?([0-5]\\d)?\\s*([AaPp][Mm])?\\s*$`);
157 const regMatches = timeReg.exec(time);
158 if (!regMatches || !regMatches.length) {
159 return;
160 }
161 let hours = regMatches[1].padStart(2, '0');
162 const minutes = regMatches[2];
163 let seconds = regMatches[3] ? `${delimiter}${regMatches[3]}` : '';
164
165 // When seconds is empty and 'includeSeconds' is enabled, append 0 seconds.
166 if (!seconds && includeSeconds) {
167 seconds = `${delimiter}00`;
168 }
169
170 const suffix = regMatches[4] || '';
171 if (suffix.toUpperCase() === 'PM' && hours !== '12') {
172 hours = `${parseInt(hours) + 12}`;
173 } else if (suffix.toUpperCase() === 'AM' && hours === '12') {
174 hours = '00';
175 }
176
177 return `${hours}${delimiter}${minutes}${seconds}`;
178 };