extended-nmea
Version:
A TypeScript library for parsing NMEA0183-like sentences with support for custom and proprietary sentences.
69 lines • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeOnly = void 0;
class TimeOnly {
/**
* Creates a higher level representation of a 24-hour based time measurement.
*
* @param milliseconds The millisecond part of the time to represent.
* @param seconds The "seconds" part of the time to represent.
* @param minutes The "minutes" part of the time to represent.
* @param hours The "hours" part of the time to represent.
*/
constructor(milliseconds, seconds = 0, minutes = 0, hours = 0) {
if (typeof milliseconds !== 'number' || typeof seconds !== 'number' || typeof minutes !== 'number' || typeof hours !== 'number')
throw new TypeError(`Cannot create TimeOnly instance with values other than numbers.`);
const total = ((((hours * 60) + minutes) * 60) + seconds) * 1000 + milliseconds;
if (total < TimeOnly.MIN_TOTAL_MILLIS)
throw new RangeError(`Cannot create TimeOnly instance with less than TimeOnly.MIN_TOTAL_MILLIS milliseconds.`);
if (total >= TimeOnly.MAX_TOTAL_MILLIS)
throw new RangeError(`Cannot create TimeOnly instance with more than TimeOnly.MAX_TOTAL_MILLIS milliseconds.`);
this.totalMilliseconds = total;
}
/**
* The integer "hours" part. Ranges from 0 to 23.
*/
get hours() {
return Math.floor(this.totalHours % 24);
}
/**
* The integer "minutes" part. Ranges from 0 to 59.
*/
get minutes() {
return Math.floor(this.totalMinutes % 60);
}
/**
* The integer "seconds" part. Ranges from 0 to 59.
*/
get seconds() {
return Math.floor(this.totalSeconds % 60);
}
/**
* The integer "milliseconds" part. Ranges from 0 to 999;
*/
get milliseconds() {
return this.totalMilliseconds % 1000;
}
/**
* The total amount of time in hours. Ranges from 0 to (24 - Number.MIN_SAFE_INTEGER).
*/
get totalHours() {
return this.totalMinutes / 60;
}
/**
* The total amount of time in minutes. Ranges from 0 to (1440 - Number.MIN_SAFE_INTEGER).
*/
get totalMinutes() {
return this.totalSeconds / 60;
}
/**
* The total amount of time in seconds. Ranges from 0 to (86400 - Number.MIN_SAFE_INTEGER).
*/
get totalSeconds() {
return this.totalMilliseconds / 1000;
}
}
exports.TimeOnly = TimeOnly;
TimeOnly.MIN_TOTAL_MILLIS = 0;
TimeOnly.MAX_TOTAL_MILLIS = 86400000;
//# sourceMappingURL=TimeOnly.js.map