UNPKG

jalali-ts

Version:

Parse and interact with jalali date

277 lines (276 loc) 11.4 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Jalali = void 0; var fa_locale_1 = require("./locale/fa.locale"); var Utils_1 = require("./Utils"); var helpers_1 = require("./helpers"); /** * Inspired by: * https://github.com/jalaali/moment-jalaali * Thanks to all contributors * https://github.com/jalaali */ var Jalali = /** @class */ (function () { function Jalali(date, includeMS) { if (date === void 0) { date = new Date(); } if (includeMS === void 0) { includeMS = true; } this.date = date; if (Jalali.checkTimeZone) { var targetTimeZone = Jalali.timeZone; var systemTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; if (systemTimeZone !== targetTimeZone) { console.warn("Your system time zone doesn't equal to '".concat(targetTimeZone, "', current: ").concat(systemTimeZone)); console.warn("You may getting unexpected results (calculated timestamp)"); } } if (!includeMS) { this.date.setMilliseconds(0); } } Object.defineProperty(Jalali, "timeZone", { get: function () { var _a; return (_a = this._timeZone) !== null && _a !== void 0 ? _a : this.defaultTimeZone; }, set: function (value) { var _a; this._timeZone = value; if (this.setTimeZone && typeof process === 'object' && ((_a = process === null || process === void 0 ? void 0 : process.release) === null || _a === void 0 ? void 0 : _a.name) === 'node') { process.env.TZ = value; } }, enumerable: false, configurable: true }); Jalali.parse = function (stringValue, includeMS) { if (includeMS === void 0) { includeMS = true; } var value = (0, helpers_1.normalizeNumbers)(stringValue); var matches = (value.match(/\d\d?\d?\d?/g) || []); var empty = new Array(7).fill('0'); var _a = __spreadArray(__spreadArray([], matches, true), empty, true).slice(0, 7) .map(function (val, index) { var numberValue = Number(val); if (index === 3) numberValue = (0, helpers_1.normalizeHours)(value, Number(val)); else if (index === 6) numberValue = (0, helpers_1.normalizeMilliseconds)(val); return numberValue; }), year = _a[0], month = _a[1], date = _a[2], hours = _a[3], minutes = _a[4], seconds = _a[5], ms = _a[6]; if (!Utils_1.Utils.isValid(year, month, date, hours, minutes, seconds, ms)) (0, helpers_1.throwError)(stringValue); return new Jalali(Utils_1.Utils.toDate(year, month, date, hours, minutes, seconds, ms), includeMS); }; Jalali.gregorian = function (stringValue, includeMS) { if (includeMS === void 0) { includeMS = true; } var value = (0, helpers_1.normalizeNumbers)(stringValue); var date = new Date(value); if (Number.isNaN(+date)) (0, helpers_1.throwError)(stringValue); return new Jalali(date, includeMS); }; Jalali.timestamp = function (value, includeMS) { if (includeMS === void 0) { includeMS = true; } return new Jalali(new Date(value), includeMS); }; Jalali.now = function (includeMS) { if (includeMS === void 0) { includeMS = true; } return new Jalali(new Date(), includeMS); }; Jalali.prototype.clone = function (includeMS) { if (includeMS === void 0) { includeMS = true; } return Jalali.timestamp(+this, includeMS); }; Jalali.prototype.valueOf = function () { return +this.date; }; Jalali.prototype.toString = function () { return this.format(); }; Jalali.prototype.getFullYear = function () { return (0, helpers_1.toJalali)(this.date).year; }; Jalali.prototype.getMonth = function () { return (0, helpers_1.toJalali)(this.date).month; }; Jalali.prototype.getDate = function () { return (0, helpers_1.toJalali)(this.date).date; }; Jalali.prototype.getHours = function () { return this.date.getHours(); }; Jalali.prototype.getMinutes = function () { return this.date.getMinutes(); }; Jalali.prototype.getSeconds = function () { return this.date.getSeconds(); }; Jalali.prototype.getMilliseconds = function () { return this.date.getMilliseconds(); }; Jalali.prototype.setFullYear = function (value) { var jalaliDate = (0, helpers_1.toJalali)(this.date); var date = Math.min(jalaliDate.date, (0, helpers_1.monthLength)(value, jalaliDate.month)); var gregorianDate = (0, helpers_1.toGregorian)(value, jalaliDate.month, date); this.update(gregorianDate); return this; }; Jalali.prototype.setMonth = function (value) { var jalaliDate = (0, helpers_1.toJalali)(this.date); var date = Math.min(jalaliDate.date, (0, helpers_1.monthLength)(jalaliDate.year, value)); this.setFullYear(jalaliDate.year + Utils_1.Utils.div(value, 12)); value = Utils_1.Utils.mod(value, 12); if (value < 0) { value += 12; this.add(-1, 'year'); } var gregorianDate = (0, helpers_1.toGregorian)(this.getFullYear(), value, date); this.update(gregorianDate); return this; }; Jalali.prototype.setDate = function (value) { var jalaliDate = (0, helpers_1.toJalali)(this.date); var gregorianDate = (0, helpers_1.toGregorian)(jalaliDate.year, jalaliDate.month, value); this.update(gregorianDate); return this; }; Jalali.prototype.setHours = function (value) { this.date.setHours(value); return this; }; Jalali.prototype.setMinutes = function (value) { this.date.setMinutes(value); return this; }; Jalali.prototype.setSeconds = function (value) { this.date.setSeconds(value); return this; }; Jalali.prototype.setMilliseconds = function (value) { this.date.setMilliseconds(value); return this; }; Jalali.prototype.isLeapYear = function () { return Utils_1.Utils.isLeapYear((0, helpers_1.toJalali)(this.date).year); }; Jalali.prototype.monthLength = function () { var jalaliDate = (0, helpers_1.toJalali)(this.date); return (0, helpers_1.monthLength)(jalaliDate.year, jalaliDate.month); }; Jalali.prototype.add = function (value, unit) { switch (unit) { case 'year': this.setFullYear(this.getFullYear() + value); break; case 'month': this.setMonth(this.getMonth() + value); break; case 'week': this.date.setDate(this.date.getDate() + (value * 7)); break; case 'day': this.date.setDate(this.date.getDate() + value); break; } return this; }; Jalali.prototype.startOf = function (unit) { if (unit === 'year') { this.setMonth(0); } if (unit === 'year' || unit === 'month') { this.setDate(1); } if (unit === 'week') { var dayOfDate = this.date.getDay(); var startOfWeek = this.date.getDate() - (dayOfDate === 6 ? 0 : this.date.getDay() + 1); this.date.setDate(startOfWeek); } this.setHours(0).setMinutes(0).setSeconds(0).setMilliseconds(0); return this; }; Jalali.prototype.endOf = function (unit) { this.startOf(unit).add(1, unit).setMilliseconds(-1); return this; }; Jalali.prototype.dayOfYear = function (value) { var jalali = this.clone(); var startOfDay = +jalali.startOf('day'); var startOfYear = +jalali.startOf('year'); var dayOfYear = Math.round((startOfDay - startOfYear) / 864e5) + 1; if (value === undefined) return dayOfYear; this.add(value - dayOfYear, 'day'); return this; }; Jalali.prototype.format = function (format, gregorian) { if (format === void 0) { format = 'YYYY/MM/DD HH:mm:ss'; } if (gregorian === void 0) { gregorian = false; } var value = String(format); var ref = gregorian ? this.date : this; var monthIndex = ref.getMonth(); var dayIndex = this.date.getDay(); var year = ref.getFullYear(); var month = monthIndex + 1; var date = ref.getDate(); var hours = ref.getHours(); var minutes = ref.getMinutes(); var seconds = ref.getSeconds(); var ms = ref.getMilliseconds(); if (!gregorian) { if (format.includes('dddd')) value = value.replace('dddd', fa_locale_1.faWeekDays[dayIndex]); if (format.includes('dd')) value = value.replace('dd', fa_locale_1.faWeekDaysShort[dayIndex]); if (format.includes('MMMM')) value = value.replace('MMMM', fa_locale_1.faMonths[monthIndex]); } if (format.includes('YYYY')) value = value.replace('YYYY', String(year)); if (format.includes('MM')) value = value.replace('MM', (0, helpers_1.zeroPad)(month)); if (format.includes('DD')) value = value.replace('DD', (0, helpers_1.zeroPad)(date)); if (format.includes('HH')) value = value.replace('HH', (0, helpers_1.zeroPad)(hours)); if (format.includes('mm')) value = value.replace('mm', (0, helpers_1.zeroPad)(minutes)); if (format.includes('ss')) value = value.replace('ss', (0, helpers_1.zeroPad)(seconds)); if (format.includes('SSS')) value = value.replace('SSS', (0, helpers_1.zeroPad)(ms, 3)); if (format.includes('hh')) { var symbol = hours >= 12 ? 'pm' : 'am'; if (format.includes('a')) value = value.replace('a', symbol); if (format.includes('A')) value = value.replace('A', symbol.toUpperCase()); if (hours === 0) hours = 12; if (hours >= 13 && hours <= 23) hours -= 12; value = value.replace('hh', (0, helpers_1.zeroPad)(hours)); } return value; }; Jalali.prototype.gregorian = function (format) { if (format === void 0) { format = 'YYYY-MM-DD HH:mm:ss'; } return this.format(format, true); }; Jalali.prototype.update = function (value) { this.date = new Date(value.year, value.month, value.date, this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds()); }; Jalali.defaultTimeZone = 'Asia/Tehran'; Jalali.checkTimeZone = true; Jalali.setTimeZone = true; return Jalali; }()); exports.Jalali = Jalali;