UNPKG

@codianz/better-date

Version:
446 lines (445 loc) 16.1 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BDate = void 0; var day_of_week_1 = require("./day-of-week"); var hhmm_1 = require("./hhmm"); var utc_offset_1 = require("./utc-offset"); var DateTimeCore = /** @class */ (function () { function DateTimeCore(year, month, day, hour, minute, second, ms) { var _this = this; if (hour === void 0) { hour = 0; } if (minute === void 0) { minute = 0; } if (second === void 0) { second = 0; } if (ms === void 0) { ms = 0; } this.fragments = { YYYY: function () { return ("0000" + _this.year.toString()).slice(-4); }, MM: function () { return ("00" + _this.month.toString()).slice(-2); }, DD: function () { return ("00" + _this.day.toString()).slice(-2); }, hh: function () { return ("00" + _this.hour.toString()).slice(-2); }, mm: function () { return ("00" + _this.min.toString()).slice(-2); }, ss: function () { return ("00" + _this.sec.toString()).slice(-2); }, ms: function () { return ("000" + _this.msec.toString()).slice(-3); }, YY: function () { return _this.year.toString().slice(-2); }, M: function () { return _this.month.toString(); }, D: function () { return _this.day.toString(); }, h: function () { return _this.hour.toString(); }, m: function () { return _this.min.toString(); }, s: function () { return _this.sec.toString(); }, }; this.m_rawDate = new Date(Date.UTC(year, month - 1, day, hour, minute, second, ms)); } DateTimeCore.fromValues = function (year, month, day, hour, minute, second, ms) { if (hour === void 0) { hour = 0; } if (minute === void 0) { minute = 0; } if (second === void 0) { second = 0; } if (ms === void 0) { ms = 0; } return new DateTimeCore(year, month, day, hour, minute, second, ms); }; Object.defineProperty(DateTimeCore.prototype, "msec", { get: function () { return this.m_rawDate.getUTCMilliseconds(); }, set: function (n) { this.m_rawDate.setUTCMilliseconds(n); }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "sec", { get: function () { return this.m_rawDate.getUTCSeconds(); }, set: function (n) { this.m_rawDate.setUTCSeconds(n); }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "min", { get: function () { return this.m_rawDate.getUTCMinutes(); }, set: function (n) { this.m_rawDate.setUTCMinutes(n); }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "hour", { get: function () { return this.m_rawDate.getUTCHours(); }, set: function (n) { this.m_rawDate.setUTCHours(n); }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "dayOfWeek", { get: function () { var day = this.m_rawDate.getUTCDay(); if (!(0, day_of_week_1.isValidDayOfWeekNumber)(day)) throw new Error("Invalid day kind number."); return day; }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "day", { get: function () { return this.m_rawDate.getUTCDate(); }, set: function (n) { this.m_rawDate.setUTCDate(n); }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "month", { get: function () { return this.m_rawDate.getUTCMonth() + 1; }, set: function (n) { this.m_rawDate.setUTCMonth(n - 1); }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "year", { get: function () { return this.m_rawDate.getUTCFullYear(); }, set: function (n) { this.m_rawDate.setUTCFullYear(n); }, enumerable: false, configurable: true }); Object.defineProperty(DateTimeCore.prototype, "msecSinceEpoch", { get: function () { return this.m_rawDate.getTime(); }, enumerable: false, configurable: true }); /** * Format date-time string. * @param fmt A string that specifies formatting. * * * %% : char of '%' * * %YYYY : 4-digit year * * %YY : 2-digit year * * %MM : 2-digit month * * %DD : 2-digit day * * %hh : 2-digit hours * * %mm : 2-digit minutes * * %ss : 2-digit seconds * * %ms : 3-digit milliseconds * * %m : month * * %d : day * * %h : hours * * %m : minutes * * %s : seconds */ DateTimeCore.prototype.format = function (fmt) { var _this = this; return fmt.replace(/%(%|YYYY|YY|MM|M|DD|D|hh|h|mm|ms|ss|m|s)/g, function (match, p) { switch (p) { case "%": return "%"; case "YYYY": return _this.fragments.YYYY(); case "YY": return _this.fragments.YY(); case "MM": return _this.fragments.MM(); case "M": return _this.fragments.M(); case "DD": return _this.fragments.DD(); case "D": return _this.fragments.D(); case "hh": return _this.fragments.hh(); case "h": return _this.fragments.h(); case "mm": return _this.fragments.mm(); case "m": return _this.fragments.m(); case "ss": return _this.fragments.ss(); case "s": return _this.fragments.s(); case "ms": return _this.fragments.ms(); } return match; }); }; DateTimeCore.prototype.toString = function () { return this.format("%YYYY/%MM/%DD %hh:%mm:%ss"); }; /** * Get the difference in milliseconds. * @param other * @returns milliseconds */ DateTimeCore.prototype.diff = function (other) { return this.m_rawDate.getTime() - other.m_rawDate.getTime(); }; return DateTimeCore; }()); var BDate = /** @class */ (function (_super) { __extends(BDate, _super); function BDate(utcOffset, year, month, day, hour, minute, second, ms) { if (hour === void 0) { hour = 0; } if (minute === void 0) { minute = 0; } if (second === void 0) { second = 0; } if (ms === void 0) { ms = 0; } var _this = _super.call(this, year, month, day, hour, minute, second, ms) || this; _this.m_utcOffsetMinutes = (0, utc_offset_1.convertToUTCOffsetMinutes)(utcOffset); return _this; } BDate.isBDate = function (o) { return o instanceof BDate; }; BDate.prototype.clone = function () { return new BDate(this.m_utcOffsetMinutes, this.year, this.month, this.day, this.hour, this.min, this.sec, this.msec); }; BDate.now = function () { return BDate.fromJsDate(new Date()); }; BDate.from = function (anyValue) { if (BDate.isBDate(anyValue)) { return anyValue.clone(); } else if (typeof anyValue === "string") { var jsdate = new Date(anyValue); return BDate.fromJsDate(jsdate); } else { return BDate.fromJsDate(anyValue); } }; /** * Initialize it with a Javascript Date object. Use `getTimeZoneOffset()` for UTC offset. * @param jsDate Javascript Date object * @returns BDate */ BDate.fromJsDate = function (jsDate) { var offset = -jsDate.getTimezoneOffset(); if (!(0, utc_offset_1.isValidUTCOffsetMinutes)(offset)) { throw new Error("Invalid UTC offset."); } return new BDate(offset, jsDate.getFullYear(), jsDate.getMonth() + 1, jsDate.getDate(), jsDate.getHours(), jsDate.getMinutes(), jsDate.getSeconds(), jsDate.getMilliseconds()); }; /** * Initialize BDate as UTC in a Javascript Date object. * @param jsDate Javascript Date object * @returns BDate (UTC) */ BDate.fromJsDateAsUTC = function (jsDate) { return new BDate(0, jsDate.getUTCFullYear(), jsDate.getUTCMonth(), jsDate.getUTCDate(), jsDate.getUTCHours(), jsDate.getUTCMinutes(), jsDate.getUTCSeconds(), jsDate.getUTCMilliseconds()); }; /** * Initialize a Javascript Date object with an arbitrary UTC offset. * @param jsDate Javascript Date object * @param utcOffset UTC offset string or minutes ("+09:00", "-02:00", 540, -120, ...) * @returns BDate */ BDate.fromJsDateWithOffset = function (jsDate, utcOffset) { return BDate.fromJsDateAsUTC(jsDate).newUTCOffset((0, utc_offset_1.convertToUTCOffsetMinutes)(utcOffset)); }; /** * Initialize with UTC offset and local year, month, day, hour, minute, and second. * @param utcOffset UTC offset string or minutes ("+09:00", "-02:00", 540, -120, ...) * @param year * @param month * @param day * @param hours * @param minutes * @param seconds * @param ms * @returns */ BDate.fromLocalDateTimeValues = function (utcOffset, year, month, day, hour, minute, second, ms) { if (hour === void 0) { hour = 0; } if (minute === void 0) { minute = 0; } if (second === void 0) { second = 0; } if (ms === void 0) { ms = 0; } return new BDate(utcOffset, year, month, day, hour, minute, second, ms); }; /** * Initialize with UTC year, month, day, hour, minute, and second. * @param year * @param month * @param day * @param hours * @param minutes * @param seconds * @param ms * @returns BDate (UTC) */ BDate.fromUTCDateTimeValues = function (year, month, day, hour, minute, second, ms) { if (hour === void 0) { hour = 0; } if (minute === void 0) { minute = 0; } if (second === void 0) { second = 0; } if (ms === void 0) { ms = 0; } return new BDate(0, year, month, day, hour, minute, second, ms); }; BDate.prototype.toUTC = function () { var localDate = this.clone(); localDate.min -= this.m_utcOffsetMinutes; localDate.m_utcOffsetMinutes = 0; return localDate; }; BDate.prototype.toISOString = function () { return this.toUTC().format("%YYYY-%MM-%DDT%hh:%mm:%ss.%msZ"); }; BDate.prototype.toISOStringWithOffset = function () { return (this.format("%YYYY-%MM-%DDT%hh:%mm:%ss.%ms") + (0, utc_offset_1.utcOffsetMinutesToUTCOffset)(this.m_utcOffsetMinutes)); }; BDate.prototype.isUTC = function () { return this.m_utcOffsetMinutes === 0; }; Object.defineProperty(BDate.prototype, "utcOffsetMinutes", { get: function () { return this.m_utcOffsetMinutes; }, enumerable: false, configurable: true }); /** * Generate a LocalTime with a new UTC offset. * @param utcOffset UTC offset string or minutes ("+09:00", "-02:00", 540, -120, ...) * @returns BDate */ BDate.prototype.newUTCOffset = function (utcOffset) { var utcOffsetMinutes = (0, utc_offset_1.convertToUTCOffsetMinutes)(utcOffset); var localDate = this.clone(); localDate.min += utcOffsetMinutes - this.m_utcOffsetMinutes; localDate.m_utcOffsetMinutes = utcOffsetMinutes; return localDate; }; BDate.prototype.toJsDate = function () { var utc = this.toUTC(); return new Date(Date.UTC(utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec, utc.msec)); }; BDate.prototype.toYMD = function () { return { year: this.year, month: this.month, day: this.day, }; }; BDate.prototype.toHHMM = function () { return hhmm_1.HHMM.from("".concat(("00" + this.hour.toString()).slice(-2)).concat(("00" + this.min.toString()).slice(-2))); }; BDate.fromYMDHHMM = function (ymd, hhmm, utcOffset) { return BDate.fromLocalDateTimeValues((0, utc_offset_1.convertToUTCOffsetMinutes)(utcOffset), ymd.year, ymd.month, ymd.day, hhmm.h, hhmm.m); }; BDate.fromYMD = function (ymd, utcOffset) { return BDate.fromLocalDateTimeValues((0, utc_offset_1.convertToUTCOffsetMinutes)(utcOffset), ymd.year, ymd.month, ymd.day); }; /** calculations */ BDate.prototype.addMilliseconds = function (n) { var ndt = this.clone(); ndt.msec += n; return ndt; }; BDate.prototype.addSeconds = function (n) { var ndt = this.clone(); ndt.sec += n; return ndt; }; BDate.prototype.addMinutes = function (n) { var ndt = this.clone(); ndt.min += n; return ndt; }; BDate.prototype.addHours = function (n) { var ndt = this.clone(); ndt.hour += n; return ndt; }; BDate.prototype.addDays = function (n) { var ndt = this.clone(); ndt.day += n; return ndt; }; BDate.prototype.addMonths = function (n) { var ndt = this.clone(); ndt.month += n; return ndt; }; BDate.prototype.addYears = function (n) { var ndt = this.clone(); ndt.year += n; return ndt; }; BDate.prototype.beginOfDay = function () { var ndt = this.clone(); ndt.hour = 0; ndt.min = 0; ndt.sec = 0; ndt.msec = 0; return ndt; }; BDate.prototype.endOfDay = function () { var ndt = this.clone(); ndt.hour = 23; ndt.min = 59; ndt.sec = 59; ndt.msec = 999; return ndt; }; BDate.prototype.beginOfWeek = function () { return this.addDays(-this.dayOfWeek).beginOfDay(); }; BDate.prototype.endOfWeek = function () { return this.addDays(6 - this.dayOfWeek).endOfDay(); }; BDate.prototype.beginOfMonth = function () { var ndt = this.clone(); ndt.day = 1; return ndt.beginOfDay(); }; BDate.prototype.endOfMonth = function () { var ndt = this.clone(); ndt.day = 1; ndt.month += 1; return ndt.addDays(-1).endOfDay(); }; BDate.prototype.beginOfYear = function () { var ndt = this.clone(); ndt.day = 1; ndt.month = 1; return ndt.beginOfDay(); }; BDate.prototype.endOfYear = function () { var ndt = this.clone(); ndt.day = 1; ndt.month = 1; ndt.year += 1; return ndt.addDays(-1).endOfDay(); }; return BDate; }(DateTimeCore)); exports.BDate = BDate;