@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
84 lines • 2.98 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) {
this.hour = hour || 0;
this.minute = minute || 0;
}
toString(is24Hour) {
if (is24Hour) {
return `${String(this.hour).padStart(2, '0')}:${String(this.minute).padStart(2, '0')}`;
}
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')} ${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 and minutes
let hours;
let minutes;
if (timeWithoutAmPm.includes(':')) {
// Format: hh:mm
const [hoursStr, minutesStr] = timeWithoutAmPm.split(':');
hours = parseInt(hoursStr, 10);
minutes = parseInt(minutesStr, 10);
}
else {
// Format: hhmm
const hoursStr = timeWithoutAmPm.slice(0, 2);
const minutesStr = timeWithoutAmPm.slice(2, 4);
hours = parseInt(hoursStr, 10);
minutes = parseInt(minutesStr, 10);
}
// 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);
}
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');
}
periodString() {
return this.hour < 12 ? 'AM' : 'PM';
}
}
//# sourceMappingURL=needle-time.js.map