UNPKG

@ultipa-graph/ultipa-driver

Version:

NodeJS SDK for ultipa-server 5.2

160 lines 6.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UltipaDatetime = void 0; const moment_timezone_1 = __importDefault(require("moment-timezone")); class UltipaDatetime { static datetimeInt2datetimeStr(intString) { let datetime_int = BigInt(intString); if (datetime_int < 0) { return ""; } // let datetime_int = BigInt(timeValue) const p_fix0 = (s, size) => { let ss = '000000' + s; return ss.substring(ss.length - size); }; let year_month = parseInt(String(datetime_int >> BigInt(46))) & 0x1ffff; let year = Math.floor(year_month / 13); let month = Math.floor(year_month % 13); let day = (parseInt(String(datetime_int >> BigInt(41))) & 0x1f) || 0; let hour = (parseInt(String(datetime_int >> BigInt(36))) & 0x1f) || 0; let minute = (parseInt(String(datetime_int >> BigInt(30))) & 0x3f) || 0; let second = (parseInt(String(datetime_int >> BigInt(24))) & 0x3f) || 0; let microsecond = parseInt(String(datetime_int & BigInt(0xffffff))) || 0; let time = `${p_fix0(year, 4)}-${p_fix0(month, 2)}-${p_fix0(day, 2)} ${p_fix0(hour, 2)}:${p_fix0(minute, 2)}:${p_fix0(second, 2)}`; if (microsecond > 0) { time = time + `.${microsecond}`; } return time; } // datetimeInt2datetimeStr(1849030063702867029n) static datetimeStr2datetimeString(time) { if (!time) return "0"; if (typeof time == "number") { time = time + ""; } time = time.replace(/(^\s*)|(\s*$)/g, ''); let datetime_int = BigInt(0); let error_message = `datetime Wrong Format: ${time}`; try { let date = time.match(/^(\d{4,5})-(\d{1,2})-(\d{1,2})(?:(?:\s([01]?\d|2[0-3]):([0-5]?\d))||'')(?:\:([0-5]?\d)||'')(?:\.(\d*)||'')$/); let year = Number(date[1]); let month = Number(date[2]); let day = Number(date[3]); let hour = Number(date[4]) || 0; let minute = Number(date[5]) || 0; let second = Number(date[6]) || 0; let microsecond = Number(date[7]) || 0; /****************--日期验证--********************/ let result = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); let smonth = [4, 6, 9, 11]; if (month === 2) { if (day > 29) { throw new Error(error_message); } if (!result && day > 28) { throw new Error(error_message); } } if (day > 31) { throw new Error(error_message); } let isSmonth = smonth.find((v) => { return v === month; }); if (isSmonth && day > 30) { throw new Error(error_message); } /******************************************/ let m = moment_timezone_1.default.tz(`${year}-${month}-${day} ${hour}:${minute}:${second}`, "YYYY-M-D H:m:s", "Africa/Abidjan"); year = m.year(); month = m.month() + 1; day = m.date(); hour = m.hour(); minute = m.minute(); second = m.second(); if (year >= 70 && 100 > year) { year += 1900; } else if (year < 70) { year += 2000; } let year_month = year * 13 + month; datetime_int |= BigInt(BigInt(year_month) << BigInt(46)); datetime_int |= BigInt(BigInt(day) << BigInt(41)); datetime_int |= BigInt((BigInt(hour)) << BigInt(36)); datetime_int |= BigInt((BigInt(minute)) << BigInt(30)); datetime_int |= BigInt((BigInt(second)) << BigInt(24)); datetime_int |= BigInt(microsecond); return String(datetime_int); } catch (error) { throw error_message; } } static getMoment(timestamp, timezone) { let m = null; if (!!timezone.timezone) { m = moment_timezone_1.default.tz(timestamp, timezone.timezone); } if (!m) { let offset = 0; if (!!timezone.timezoneOffset) { offset = parseInt(timezone.timezoneOffset); } if (isNaN(+timestamp)) { // 0时区 m = moment_timezone_1.default.tz(timestamp, "Africa/Abidjan").utcOffset(offset); } else { m = (0, moment_timezone_1.default)(timestamp).utcOffset(offset); } } return m; } static timestamp2Str(timestamp_seconds, timezone) { let m = this.getMoment(timestamp_seconds * 1000, timezone); return m.format("YYYY-MM-DD HH:mm:ss"); } static timestampStr2int(timestamp_str, timezone) { let m = this.getMoment(timestamp_str, timezone); return m.unix(); } /** * Parses a timezone offset string or number into seconds. * Supports formats: "+08:30", "-05:00", "+2", "-0300", "8.5", etc. */ static parseTimezoneOffsetToSeconds(offset) { if (typeof offset === 'number') { // Direct numeric input interpreted as hours return offset * 3600; } const trimmed = offset.trim(); // Match "+08:30" or "-05:00" let match = trimmed.match(/^([+-]?)(\d{1,2}):(\d{2})$/); if (match) { const sign = match[1] === '-' ? -1 : 1; const hours = parseInt(match[2], 10); const minutes = parseInt(match[3], 10); return sign * (hours * 3600 + minutes * 60); } // Match "+0800" or "-0530" match = trimmed.match(/^([+-]?)(\d{2})(\d{2})$/); if (match) { const sign = match[1] === '-' ? -1 : 1; const hours = parseInt(match[2], 10); const minutes = parseInt(match[3], 10); return sign * (hours * 3600 + minutes * 60); } // Match "+2", "-3.5", "8.5" (interpreted as hour float) const asNumber = parseFloat(trimmed); if (!isNaN(asNumber)) { return asNumber * 3600; } throw new Error(`Invalid timezoneOffset format: "${offset}"`); } } exports.UltipaDatetime = UltipaDatetime; //# sourceMappingURL=ultipa.datetime.js.map