UNPKG

cql-execution

Version:

An execution framework for the Clinical Quality Language (CQL)

465 lines (428 loc) 16.1 kB
// Generated by CoffeeScript 1.9.3 (function() { var DateTime, Uncertainty; Uncertainty = require('./uncertainty').Uncertainty; module.exports.DateTime = DateTime = (function() { DateTime.Unit = { YEAR: 'year', MONTH: 'month', DAY: 'day', HOUR: 'hour', MINUTE: 'minute', SECOND: 'second', MILLISECOND: 'millisecond' }; DateTime.FIELDS = [DateTime.Unit.YEAR, DateTime.Unit.MONTH, DateTime.Unit.DAY, DateTime.Unit.HOUR, DateTime.Unit.MINUTE, DateTime.Unit.SECOND, DateTime.Unit.MILLISECOND]; DateTime.parse = function(string) { var arg, args, match, num, regex; match = regex = /(\d{4})(-(\d{2}))?(-(\d{2}))?(T((\d{2})(\:(\d{2})(\:(\d{2})(\.(\d+))?)?)?)?(([+-])(\d{2})(\:?(\d{2}))?)?)?/.exec(string); if (match[0] === string) { args = [match[1], match[3], match[5], match[8], match[10], match[12], match[14]]; if (args[6] != null) { args[6] = (args[6] + "00").substring(0, 3); } args = (function() { var i, len, results; results = []; for (i = 0, len = args.length; i < len; i++) { arg = args[i]; results.push(arg != null ? parseInt(arg, 10) : void 0); } return results; })(); if (match[17] != null) { num = parseInt(match[17], 10) + (match[19] != null ? parseInt(match[19], 10) / 60 : 0); args.push(match[16] === '+' ? num : num * -1); } return (function(func, args, ctor) { ctor.prototype = func.prototype; var child = new ctor, result = func.apply(child, args); return Object(result) === result ? result : child; })(DateTime, args, function(){}); } else { return null; } }; DateTime.fromDate = function(date, timezoneOffset) { if (timezoneOffset != null) { date = new Date(date.getTime() + (timezoneOffset * 60 * 60 * 1000)); return new DateTime(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds(), timezoneOffset); } else { return new DateTime(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()); } }; function DateTime(year, month, day, hour, minute, second, millisecond, timezoneOffset1) { this.year = year != null ? year : null; this.month = month != null ? month : null; this.day = day != null ? day : null; this.hour = hour != null ? hour : null; this.minute = minute != null ? minute : null; this.second = second != null ? second : null; this.millisecond = millisecond != null ? millisecond : null; this.timezoneOffset = timezoneOffset1; if (this.timezoneOffset == null) { this.timezoneOffset = (new Date()).getTimezoneOffset() / 60 * -1; } } DateTime.prototype.copy = function() { return new DateTime(this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond, this.timezoneOffset); }; DateTime.prototype.successor = function() { if (this.millisecond != null) { return this.add(1, DateTime.Unit.MILLISECOND); } else if (this.second != null) { return this.add(1, DateTime.Unit.SECOND); } else if (this.minute != null) { return this.add(1, DateTime.Unit.MINUTE); } else if (this.hour != null) { return this.add(1, DateTime.Unit.HOUR); } else if (this.day != null) { return this.add(1, DateTime.Unit.DAY); } else if (this.month != null) { return this.add(1, DateTime.Unit.MONTH); } else if (this.year != null) { return this.add(1, DateTime.Unit.YEAR); } }; DateTime.prototype.predecessor = function() { if (this.millisecond != null) { return this.add(-1, DateTime.Unit.MILLISECOND); } else if (this.second != null) { return this.add(-1, DateTime.Unit.SECOND); } else if (this.minute != null) { return this.add(-1, DateTime.Unit.MINUTE); } else if (this.hour != null) { return this.add(-1, DateTime.Unit.HOUR); } else if (this.day != null) { return this.add(-1, DateTime.Unit.DAY); } else if (this.month != null) { return this.add(-1, DateTime.Unit.MONTH); } else if (this.year != null) { return this.add(-1, DateTime.Unit.YEAR); } }; DateTime.prototype.convertToTimezoneOffset = function(timezoneOffset) { if (timezoneOffset == null) { timezoneOffset = 0; } return DateTime.fromDate(this.toJSDate(), timezoneOffset); }; DateTime.prototype.sameAs = function(other, precision) { var diff; if (precision == null) { precision = DateTime.Unit.MILLISECOND; } if (!(other instanceof DateTime)) { null; } diff = this.durationBetween(other, precision); switch (false) { case !(diff.low === 0 && diff.high === 0): return true; case !(diff.low <= 0 && diff.high >= 0): return null; default: return false; } }; DateTime.prototype.equals = function(other) { return this.sameAs(other, DateTime.Unit.MILLISECOND); }; DateTime.prototype.sameOrBefore = function(other, precision) { var diff; if (precision == null) { precision = DateTime.Unit.MILLISECOND; } if (!(other instanceof DateTime)) { return false; } diff = this.durationBetween(other, precision); switch (false) { case !(diff.low >= 0 && diff.high >= 0): return true; case !(diff.low < 0 && diff.high < 0): return false; default: return null; } }; DateTime.prototype.sameOrAfter = function(other, precision) { var diff; if (precision == null) { precision = DateTime.Unit.MILLISECOND; } if (!(other instanceof DateTime)) { return false; } diff = this.durationBetween(other, precision); switch (false) { case !(diff.low <= 0 && diff.high <= 0): return true; case !(diff.low > 0 && diff.high > 0): return false; default: return null; } }; DateTime.prototype.before = function(other, precision) { var diff; if (precision == null) { precision = DateTime.Unit.MILLISECOND; } if (!(other instanceof DateTime)) { return false; } diff = this.durationBetween(other, precision); switch (false) { case !(diff.low > 0 && diff.high > 0): return true; case !(diff.low <= 0 && diff.high <= 0): return false; default: return null; } }; DateTime.prototype.after = function(other, precision) { var diff; if (precision == null) { precision = DateTime.Unit.MILLISECOND; } if (!(other instanceof DateTime)) { return false; } diff = this.durationBetween(other, precision); switch (false) { case !(diff.low < 0 && diff.high < 0): return true; case !(diff.low >= 0 && diff.high >= 0): return false; default: return null; } }; DateTime.prototype.add = function(offset, field) { var i, len, normalized, ref, result; result = this.copy(); if (result[field] != null) { result[field] = result[field] + offset; normalized = DateTime.fromDate(result.toJSDate(), this.timezoneOffset); ref = DateTime.FIELDS; for (i = 0, len = ref.length; i < len; i++) { field = ref[i]; if (result[field] != null) { result[field] = normalized[field]; } } } return result; }; DateTime.prototype.durationBetween = function(other, unitField) { var a, b; if (!(other instanceof DateTime)) { return null; } if (this.timezoneOffset !== other.timezoneOffset) { other = other.convertToTimezoneOffset(this.timezoneOffset); } a = this.toUncertainty(true); b = other.toUncertainty(true); return new Uncertainty(this._durationBetweenDates(a.high, b.low, unitField), this._durationBetweenDates(a.low, b.high, unitField)); }; DateTime.prototype._durationBetweenDates = function(a, b, unitField) { var msDiff, ref; ref = [a, b].map(function(x) { switch (unitField) { case DateTime.Unit.DAY: return new Date(x.getFullYear(), x.getMonth(), x.getDate()); case DateTime.Unit.HOUR: return new Date(x.getFullYear(), x.getMonth(), x.getDate(), x.getHours()); case DateTime.Unit.MINUTE: return new Date(x.getFullYear(), x.getMonth(), x.getDate(), x.getHours(), x.getMinutes()); case DateTime.Unit.SECOND: return new Date(x.getFullYear(), x.getMonth(), x.getDate(), x.getHours(), x.getMinutes(), x.getSeconds()); case DateTime.Unit.MILLISECOND: return new Date(x.getFullYear(), x.getMonth(), x.getDate(), x.getHours(), x.getMinutes(), x.getSeconds(), x.getMilliseconds()); default: return x; } }), a = ref[0], b = ref[1]; msDiff = b.getTime() - a.getTime(); switch (unitField) { case DateTime.Unit.YEAR: return b.getFullYear() - a.getFullYear(); case DateTime.Unit.MONTH: return b.getMonth() - a.getMonth() + (12 * (b.getFullYear() - a.getFullYear())); case DateTime.Unit.DAY: return Math.floor(msDiff / (24 * 60 * 60 * 1000)); case DateTime.Unit.HOUR: return Math.floor(msDiff / (60 * 60 * 1000)); case DateTime.Unit.MINUTE: return Math.floor(msDiff / (60 * 1000)); case DateTime.Unit.SECOND: return Math.floor(msDiff / 1000); case DateTime.Unit.MILLISECOND: return msDiff; default: return null; } }; DateTime.prototype.isPrecise = function() { return DateTime.FIELDS.every((function(_this) { return function(field) { return _this[field] != null; }; })(this)); }; DateTime.prototype.isImprecise = function() { return !this.isPrecise(); }; DateTime.prototype.isMorePrecise = function(other) { var field, i, len, ref; ref = DateTime.FIELDS; for (i = 0, len = ref.length; i < len; i++) { field = ref[i]; if ((other[field] != null) && (this[field] == null)) { return false; } } return !this.isSamePrecision(other); }; DateTime.prototype.isLessPrecise = function(other) { return !this.isSamePrecision(other) && !this.isMorePrecise(other); }; DateTime.prototype.isSamePrecision = function(other) { var field, i, len, ref; ref = DateTime.FIELDS; for (i = 0, len = ref.length; i < len; i++) { field = ref[i]; if ((this[field] != null) && (other[field] == null)) { return false; } if ((this[field] == null) && (other[field] != null)) { return false; } } return true; }; DateTime.prototype.getPrecision = function() { var result; result = null; if (this.year != null) { result = DateTime.Unit.YEAR; } else { return result; } if (this.month != null) { result = DateTime.Unit.MONTH; } else { return result; } if (this.day != null) { result = DateTime.Unit.DAY; } else { return result; } if (this.hour != null) { result = DateTime.Unit.HOUR; } else { return result; } if (this.minute != null) { result = DateTime.Unit.MINUTE; } else { return result; } if (this.second != null) { result = DateTime.Unit.SECOND; } else { return result; } if (this.millisecond != null) { return result = DateTime.Unit.MILLISECOND; } else { return result; } }; DateTime.prototype.toUncertainty = function(ignoreTimezone) { var high, low, ref, ref1, ref2, ref3, ref4, ref5, ref6; if (ignoreTimezone == null) { ignoreTimezone = false; } low = this.toJSDate(ignoreTimezone); high = (new DateTime(this.year, (ref = this.month) != null ? ref : 12, (ref1 = this.day) != null ? ref1 : (new Date(this.year, (ref2 = this.month) != null ? ref2 : 12, 0)).getDate(), (ref3 = this.hour) != null ? ref3 : 23, (ref4 = this.minute) != null ? ref4 : 59, (ref5 = this.second) != null ? ref5 : 59, (ref6 = this.millisecond) != null ? ref6 : 999, this.timezoneOffset)).toJSDate(ignoreTimezone); return new Uncertainty(low, high); }; DateTime.prototype.toJSDate = function(ignoreTimezone) { var d, h, mi, mo, ms, ref, ref1, ref2, ref3, ref4, ref5, s, y; if (ignoreTimezone == null) { ignoreTimezone = false; } ref5 = [this.year, (this.month != null ? this.month - 1 : 0), (ref = this.day) != null ? ref : 1, (ref1 = this.hour) != null ? ref1 : 0, (ref2 = this.minute) != null ? ref2 : 0, (ref3 = this.second) != null ? ref3 : 0, (ref4 = this.millisecond) != null ? ref4 : 0], y = ref5[0], mo = ref5[1], d = ref5[2], h = ref5[3], mi = ref5[4], s = ref5[5], ms = ref5[6]; if ((this.timezoneOffset != null) && !ignoreTimezone) { return new Date(Date.UTC(y, mo, d, h, mi, s, ms) - (this.timezoneOffset * 60 * 60 * 1000)); } else { return new Date(y, mo, d, h, mi, s, ms); } }; DateTime.prototype._pad = function(num) { return String("0" + x).slice(-2); }; DateTime.prototype.toString = function() { var offsetHours, offsetMin, str; str = ''; if (this.year != null) { str += this.year; if (this.month != null) { str += '-' + this._pad(this.month); if (this.day != null) { str += '-' + this._pad(this.day); if (this.hour != null) { str += 'T' + this._pad(this.hour); if (this.minute != null) { str += ':' + this._pad(this.minute); if (this.second != null) { str += ':' + this._pad(this.second); if (this.millisecond != null) { str += '.' + this._pad(this.millisecond); } } } } } } } if (str.indexOf('T') !== -1 && (this.timezoneOffset != null)) { str += this.timezoneOffset < 0 ? '-' : '+'; offsetHours = Math.floor(Math.abs(this.timezoneOffset)); str += this._pad(offsetHours); offsetMin = (Math.abs(this.timezoneOffset) - offsetHours) * 60; str += this._pad(offsetMin); } return str; }; DateTime.prototype.getDate = function() { return this.reducedPrecision(DateTime.Unit.DAY); }; DateTime.prototype.getTime = function() { return new DateTime(1900, 1, 1, this.hour, this.minute, this.second, this.millisecond, this.timezoneOffset); }; DateTime.prototype.reducedPrecision = function(unitField) { var field, fieldIndex, fieldsToRemove, i, len, reduced; if (unitField == null) { unitField = DateTime.Unit.MILLISECOND; } reduced = this.copy(); if (unitField !== DateTime.Unit.MILLISECOND) { fieldIndex = DateTime.FIELDS.indexOf(unitField); fieldsToRemove = DateTime.FIELDS.slice(fieldIndex + 1); for (i = 0, len = fieldsToRemove.length; i < len; i++) { field = fieldsToRemove[i]; reduced[field] = null; } } return reduced; }; return DateTime; })(); }).call(this); //# sourceMappingURL=datetime.js.map