@ultipa-graph/ultipa-node-sdk
Version:
NodeJS SDK for ultipa-server 4.0
120 lines • 4.92 kB
JavaScript
;
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);
}
/******************************************/
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 = 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();
}
}
exports.UltipaDatetime = UltipaDatetime;
//# sourceMappingURL=ultipa.datetime.js.map