@microsoft/kiota-abstractions
Version:
Core abstractions for kiota generated libraries in TypeScript and JavaScript
106 lines • 4.2 kB
JavaScript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { formatSegment } from "./dateOnly.js";
/*
* Represents a time only. ISO 8601.
*/
export class TimeOnly {
/**
* Creates a new TimeOnly from the given parameters.
* @param root0 The hours, minutes, seconds, and milliseconds
* @param root0.hours The hours
* @param root0.minutes The minutes
* @param root0.seconds The seconds
* @param root0.picoseconds The milliseconds
* @returns The new TimeOnly
* @throws An error if the milliseconds are invalid
* @throws An error if the seconds are invalid
* @throws An error if the minutes are invalid
* @throws An error if the hours are invalid
* @throws An error if the milliseconds are invalid
*/
constructor({ hours = 0, minutes = 0, seconds = 0, picoseconds = 0 }) {
if (hours < 0 || hours > 23) {
throw new Error("Hour must be between 0 and 23");
}
if (minutes < 0 || minutes > 59) {
throw new Error("Minute must be between 0 and 59");
}
if (seconds < 0 || seconds > 59) {
throw new Error("Second must be between 0 and 59");
}
if (picoseconds < 0 || picoseconds > 9999999) {
throw new Error("Millisecond must be between 0 and 9999999");
}
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.picoseconds = picoseconds;
}
/**
* Creates a new TimeOnly from the given date.
* @param date The date
* @returns The new TimeOnly
* @throws An error if the date is invalid
*/
static fromDate(date) {
if (!date) {
throw new Error("Date cannot be undefined");
}
return new TimeOnly({
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds(),
picoseconds: date.getMilliseconds() * 10000,
});
}
/**
* Parses a string into a TimeOnly. The string can be of the ISO 8601 time only format or a number representing the ticks of a Date.
* @param value The value to parse
* @returns The parsed TimeOnly.
* @throws An error if the value is invalid
*/
static parse(value) {
var _a, _b, _c, _d;
if (!value || value.length === 0) {
return undefined;
}
const ticks = Date.parse(value);
if (isNaN(ticks)) {
// not using name groups as it's an ES2018 feature
const exec = /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(?:[.](\d{1,12}))?$/gi.exec(value);
if (exec) {
const hours = parseInt((_a = exec[1]) !== null && _a !== void 0 ? _a : "", 10);
const minutes = parseInt((_b = exec[2]) !== null && _b !== void 0 ? _b : "", 10);
const seconds = parseInt((_c = exec[3]) !== null && _c !== void 0 ? _c : "", 10);
const milliseconds = parseInt((_d = exec[4]) !== null && _d !== void 0 ? _d : "0", 10);
return new TimeOnly({
hours,
minutes,
seconds,
picoseconds: milliseconds,
});
}
else {
throw new Error("Value is not a valid time-only representation");
}
}
else {
const date = new Date(ticks);
return this.fromDate(date);
}
}
/**
* Returns a string representation of the time in the format HH:MM:SS.SSSSSSS
* @returns The time in the format HH:MM:SS.SSSSSSS
* @throws An error if the time is invalid
*/
toString() {
return `${formatSegment(this.hours, 2)}:${formatSegment(this.minutes, 2)}:${formatSegment(this.seconds, 2)}.${formatSegment(this.picoseconds, 7)}`;
}
}
//# sourceMappingURL=timeOnly.js.map