UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

90 lines (89 loc) 3.48 kB
import { Type } from "../type/Types.js"; import { isNullish } from "../util/IfcExpressionUtils.js"; import { DateFormatException } from "../error/value/DateFormatException.js"; import Decimal from "decimal.js"; export 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 (isNullish(match)) { throw new 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 = isNullish(fractionSeconds) ? new Decimal("0") : new Decimal("0" + fractionSeconds); //arbitrary precision fractions const originalZoneSign = isNullish(timeZoneSign) ? 0 : timeZoneSign === "+" ? 1 : -1; const originalTimeZoneHours = originalZoneSign * (isNullish(timeZoneHours) ? 0 : Number.parseInt(timeZoneHours)); const originalTimeZoneMinutes = originalZoneSign * (isNullish(timeZoneMinutes) ? 0 : Number.parseInt(timeZoneMinutes)); const isLocal = 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 Type.IFC_TIME; } static isIfcTimeValueType(arg) { return (arg instanceof IfcTimeValue || (!isNullish(arg.stringRepresentation) && !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; } } 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