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