UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

97 lines (96 loc) 4.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IfcTimeValue = void 0; const Types_js_1 = require("../type/Types.js"); const IfcExpressionUtils_js_1 = require("../util/IfcExpressionUtils.js"); const DateFormatException_js_1 = require("../error/value/DateFormatException.js"); const decimal_js_1 = __importDefault(require("decimal.js")); class IfcTimeValue { constructor(value) { var parsed = IfcTimeValue.parseIfcDate(value); this.utcDateValue = parsed.utcDate; this.originalTimeZoneHours = parsed.originalTimeZoneHours; this.originalTimeZoneMinutes = parsed.originalTimeZoneMinutes; this.secondFraction = parsed.secondFraction; this.isLocal = parsed.isLocal; this.stringRepresentation = value; } static parseIfcDate(value) { let match = value.match(this.regex); if ((0, IfcExpressionUtils_js_1.isNullish)(match)) { throw new DateFormatException_js_1.DateFormatException("Not an IfcTime: " + value); } let [wholeString, hours, minutes, seconds, fractionSeconds, timeZoneWhole, timeZoneSign, timeZoneHours, timeZoneMinutes,] = match; const utcDate = new Date(0); utcDate.setUTCHours(Number.parseInt(hours)); utcDate.setUTCMinutes(Number.parseInt(minutes)); utcDate.setUTCSeconds(Number.parseInt(seconds)); const secondFraction = (0, IfcExpressionUtils_js_1.isNullish)(fractionSeconds) ? new decimal_js_1.default("0") : new decimal_js_1.default("0" + fractionSeconds); //arbitrary precision fractions const originalZoneSign = (0, IfcExpressionUtils_js_1.isNullish)(timeZoneSign) ? 0 : timeZoneSign === "+" ? 1 : -1; const originalTimeZoneHours = originalZoneSign * ((0, IfcExpressionUtils_js_1.isNullish)(timeZoneHours) ? 0 : Number.parseInt(timeZoneHours)); const originalTimeZoneMinutes = originalZoneSign * ((0, IfcExpressionUtils_js_1.isNullish)(timeZoneMinutes) ? 0 : Number.parseInt(timeZoneMinutes)); const isLocal = (0, IfcExpressionUtils_js_1.isNullish)(timeZoneWhole); utcDate.setUTCHours(utcDate.getUTCHours() - originalTimeZoneHours); utcDate.setUTCMinutes(utcDate.getUTCMinutes() - originalTimeZoneMinutes); return { utcDate, secondFraction, originalTimeZoneHours, originalTimeZoneMinutes, isLocal, }; } static of(value) { return new IfcTimeValue(value); } getValue() { return this; } getType() { return Types_js_1.Type.IFC_TIME; } static isIfcTimeValueType(arg) { return (arg instanceof IfcTimeValue || (!(0, IfcExpressionUtils_js_1.isNullish)(arg.stringRepresentation) && !(0, IfcExpressionUtils_js_1.isNullish)(arg.stringRepresentation.match(IfcTimeValue.regex)))); } static isValidStringRepresentation(str) { return this.regex.test(str); } equals(other) { return (IfcTimeValue.isIfcTimeValueType(other) && this.utcDateValue.getTime() === other.utcDateValue.getTime() && this.secondFraction.eq(other.secondFraction)); } toString() { return this.stringRepresentation; } compareTo(other) { var diff = this.utcDateValue.getTime() - other.utcDateValue.getTime(); if (diff != 0) { return diff; } var diffD = this.secondFraction.minus(other.secondFraction); if (diffD.isZero()) { return 0; } if (diffD.lt(0)) { return -1; } return 1; } } exports.IfcTimeValue = IfcTimeValue; IfcTimeValue.regex = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|(?<=23:59:)60)(\.\d+)?(Z|([+\-])(0[0-9]|1[0-2])(?::?([0-5][0-9]))?)?$/; //# sourceMappingURL=IfcTimeValue.js.map