@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
98 lines • 3.62 kB
JavaScript
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
export class NeedleTime {
constructor(hour, minute, second) {
this.hour = hour || 0;
this.minute = minute || 0;
this.second = second || 0;
}
toString(is24Hour, showSeconds) {
const seconds = showSeconds
? `:${String(this.second).padStart(2, '0')}`
: '';
if (is24Hour) {
return `${String(this.hour).padStart(2, '0')}:${String(this.minute).padStart(2, '0')}${seconds}`;
}
else {
const period = this.hour < 12 ? 'AM' : 'PM';
const adjustedHours = this.hour % 12 || 12;
return `${String(adjustedHours).padStart(2, '0')}:${String(this.minute).padStart(2, '0')}${seconds} ${period}`;
}
}
static fromString(timeString) {
// Remove any whitespace and convert to lowercase
const cleanTime = timeString.toLowerCase().replace(/\s/g, '');
// Extract AM/PM if present
const isPM = cleanTime.includes('pm');
const hasAMPM = cleanTime.includes('am') || isPM;
const timeWithoutAmPm = cleanTime.replace(/[ap]m/g, '');
// Extract hours, minutes and (optionally) seconds
let hours;
let minutes;
let seconds;
if (timeWithoutAmPm.includes(':')) {
// Format: hh:mm or hh:mm:ss
const [hoursStr, minutesStr, secondsStr] = timeWithoutAmPm.split(':');
hours = parseInt(hoursStr, 10);
minutes = parseInt(minutesStr, 10);
seconds =
secondsStr !== undefined && secondsStr !== ''
? parseInt(secondsStr, 10)
: 0;
}
else {
// Format: hhmm or hhmmss
const hoursStr = timeWithoutAmPm.slice(0, 2);
const minutesStr = timeWithoutAmPm.slice(2, 4);
const secondsStr = timeWithoutAmPm.slice(4, 6);
hours = parseInt(hoursStr, 10);
minutes = parseInt(minutesStr, 10);
seconds = secondsStr !== '' ? parseInt(secondsStr, 10) : 0;
}
// Adjust hours for PM
if (isPM && hours < 12) {
hours += 12;
}
// Adjust hours for AM 12 only if we're dealing with AM/PM format
if (hasAMPM && !isPM && hours === 12) {
hours = 0;
}
return new NeedleTime(hours, minutes, seconds);
}
hourString(is24Hour) {
if (is24Hour) {
return String(this.hour).padStart(2, '0');
}
else {
return String(this.hour % 12 || 12).padStart(2, '0');
}
}
minuteString() {
return String(this.minute).padStart(2, '0');
}
secondString() {
return String(this.second).padStart(2, '0');
}
periodString() {
return this.hour < 12 ? 'AM' : 'PM';
}
}
//# sourceMappingURL=needle-time.js.map