ifc-expressions
Version:
Parsing and evaluation of IFC expressions
76 lines (75 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IfcDateValue = 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");
class IfcDateValue {
constructor(value) {
var parsed = IfcDateValue.parseIfcDate(value);
this.utcDateValue = parsed.utcDate;
this.originalTimeZoneHours = parsed.originalTimeZoneHours;
this.originalTimeZoneMinutes = parsed.originalTimeZoneMinutes;
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 IfcDate: " + value);
}
let [wholeString, year, month, day, timeZoneWhole, timeZoneSign, timeZoneHours, timeZoneMinutes,] = match;
const utcDate = new Date(0);
utcDate.setUTCFullYear(Number.parseInt(year));
utcDate.setUTCMonth(Number.parseInt(month) - 1);
utcDate.setUTCDate(Number.parseInt(day));
const originalZoneSign = (0, IfcExpressionUtils_js_1.isNullish)(timeZoneSign)
? 0
: timeZoneSign === "+"
? -1
: +1; //why? Beause +1 means 1 hour ahead of UTC, so we have to subtract 1 to get UTC
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: utcDate,
originalTimeZoneHours,
originalTimeZoneMinutes,
isLocal,
};
}
static of(value) {
return new IfcDateValue(value);
}
getValue() {
return this;
}
getType() {
return Types_js_1.Type.IFC_DATE;
}
static isIfcDateValueType(arg) {
return (arg instanceof IfcDateValue ||
(!(0, IfcExpressionUtils_js_1.isNullish)(arg.stringRepresentation) &&
!(0, IfcExpressionUtils_js_1.isNullish)(arg.stringRepresentation.match(IfcDateValue.regex))));
}
static isValidStringRepresentation(str) {
return this.regex.test(str);
}
equals(other) {
return (IfcDateValue.isIfcDateValueType(other) &&
this.utcDateValue.getTime() === other.utcDateValue.getTime());
}
toString() {
return this.stringRepresentation;
}
compareTo(other) {
return this.utcDateValue.getTime() - other.utcDateValue.getTime();
}
}
exports.IfcDateValue = IfcDateValue;
IfcDateValue.regex = /^([+\-]?(?:[1-9]\d*)?\d{4}(?<!0000))-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(Z|([+\-])(0[0-9]|1[0-2])(?::?([0-5][0-9]))?)?$/;
//# sourceMappingURL=IfcDateValue.js.map