12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- var DateMapping = /** @class */ (function () {
- function DateMapping(monthsLengthFlags, gy, gm, gd) {
- this.hijriMonthsLengthFlags = monthsLengthFlags;
- this.gregorianDate = new Date(gy, gm, gd);
- }
- return DateMapping;
- }());
- var DatePart;
- (function (DatePart) {
- DatePart[DatePart["Year"] = 0] = "Year";
- DatePart[DatePart["DayOfYear"] = 1] = "DayOfYear";
- DatePart[DatePart["Month"] = 2] = "Month";
- DatePart[DatePart["Day"] = 3] = "Day";
- })(DatePart || (DatePart = {}));
- var DatePart$1 = DatePart;
- // Parts of this file are (c) 2007-2009 Steven Levithan <stevenlevithan.com>
- // https://github.com/felixge/node-dateformat/blob/master/lib/dateformat.js
- var token = /d{1,4}|M{1,4}|yy(?:yy)?|([HhmsTt])\1?|[LlSWN]|"[^"]*"|'[^']*'/g;
- function pad(val, locale, len) {
- val = String(val);
- len = len || 2;
- while (val.length < len) {
- val = '0' + val;
- }
- return locale.localizeNum(val);
- }
- function format(date, mask, locale, hy, hm, hd, woy, dow) {
- mask = String(locale.masks[mask] || mask || locale.masks.default);
- var _ = 'get';
- var d = hd;
- var D = date[_ + 'Day']();
- var m = hm;
- var y = hy;
- var H = date[_ + 'Hours']();
- var M = date[_ + 'Minutes']();
- var s = date[_ + 'Seconds']();
- var L = date[_ + 'Milliseconds']();
- var W = woy;
- var N = dow;
- var flags = {
- d: locale.localizeNum(d),
- dd: pad(d, locale),
- ddd: locale.dayNamesShort[D],
- dddd: locale.dayNames[D],
- M: locale.localizeNum(m),
- MM: pad(m, locale),
- MMM: locale.monthNamesShort[m - 1],
- MMMM: locale.monthNames[m - 1],
- yy: locale.localizeNum(String(y).slice(2)),
- yyyy: locale.localizeNum(y),
- h: locale.localizeNum(H % 12 || 12),
- hh: pad(H % 12 || 12, locale),
- H: locale.localizeNum(H),
- HH: pad(H, locale),
- m: locale.localizeNum(M),
- mm: pad(M, locale),
- s: locale.localizeNum(s),
- ss: pad(s, locale),
- l: pad(L, locale, 3),
- L: pad(Math.round(L / 10), locale),
- t: H < 12 ? locale.timeNames[0] : locale.timeNames[1],
- tt: H < 12 ? locale.timeNames[2] : locale.timeNames[3],
- T: H < 12 ? locale.timeNames[4] : locale.timeNames[5],
- TT: H < 12 ? locale.timeNames[6] : locale.timeNames[7],
- S: locale.localizeDayNum(d),
- W: locale.localizeNum(W),
- N: locale.localizeNum(N),
- };
- return locale.localizeCommas(mask.replace(token, function (match) { return flags[match]; }));
- }
- var symbolMap = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
- var dayNumMap = ['الأول', 'الثاني', 'الثالث', 'الرابع', 'الخامس', 'السادس', 'السابع', 'الثامن', 'التاسع', 'العاشر', 'الحادي عشر'];
- var ar = {
- name: 'ar',
- rtl: true,
- dayNamesShort: ['ح', 'ن', 'ث', 'ر', 'خ', 'ج', 'س'],
- dayNames: ['الأحد', 'الإثنين', 'الثلاثاء', 'الأربعاء', 'الخميس', 'الجمعة', 'السبت'],
- monthNamesShort: ['محرم', 'صفر', 'ربيع ١', 'ربيع ٢', 'جمادى ١', 'جمادى ٢', 'رجب', 'شعبان', 'رمضان', 'شوال', 'ذو القعدة', 'ذو الحجة'],
- monthNames: ['محرم', 'صفر', 'ربيع الأول', 'ربيع الثاني', 'جمادى الأولى', 'جمادى الآخرة', 'رجب', 'شعبان', 'رمضان', 'شوال', 'ذو القعدة', 'ذو الحجة'],
- timeNames: ['ص', 'م', 'ص', 'م', 'ص', 'م', 'ص', 'م'],
- masks: {
- default: 'ddd dd MMM yyyy HH:mm:ss',
- shortDate: 'yy/M/d',
- mediumDate: 'd MMM, yyyy',
- longDate: 'd MMMM, yyyy',
- fullDate: 'dddd, d MMMM, yyyy',
- shortTime: 'h:mm TT',
- mediumTime: 'h:mm:ss TT',
- longTime: 'h:mm:ss.l TT',
- },
- localizeNum: function (num) {
- var s = String(num);
- var output = '';
- for (var i = 0; i < s.length; i++) {
- output += symbolMap[s.charAt(i)];
- }
- return output;
- },
- localizeDayNum: function (d) {
- var output = '';
- if (d === 11) {
- output = 'الحادي عشر';
- }
- else if (d === 20) {
- output = 'العشرون';
- }
- else if (d === 30) {
- output = 'الثلاثون';
- }
- else {
- output = dayNumMap[d - 1];
- }
- var section = d / 10;
- if (section > 1.1 && section < 2) {
- output = dayNumMap[(d - 1) % 10] + ' عشر';
- }
- else if (section > 2 && section < 3) {
- output = dayNumMap[(d - 1) % 10] + ' والعشرون';
- }
- return output + ' من';
- },
- localizeCommas: function (v) { return v.replace(/,/g, '،'); },
- };
- var en = {
- name: 'en',
- dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
- dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
- monthNamesShort: ['Muh', 'Ṣaf', 'Rab-I', 'Rab-II', 'Jum-I', 'Jum-II', 'Raj', 'Sha', 'Ram', 'Shw', 'Dhū-Q', 'Dhū-Ḥ'],
- monthNames: ['Muharram', 'Ṣafar', 'Rabīʿ al-Awwal', 'Rabīʿ ath-Thānī', 'Jumādá al-Ūlá', 'Jumādá al-Ākhirah', 'Rajab', 'Sha‘bān', 'Ramaḍān', 'Shawwāl', 'Dhū al-Qa‘dah', 'Dhū al-Ḥijjah'],
- timeNames: ['a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM'],
- masks: {
- default: 'ddd MMM dd yyyy HH:mm:ss',
- shortDate: 'M/d/yy',
- mediumDate: 'MMM d, yyyy',
- longDate: 'MMMM d, yyyy',
- fullDate: 'dddd, MMMM d, yyyy',
- shortTime: 'h:mm TT',
- mediumTime: 'h:mm:ss TT',
- longTime: 'h:mm:ss.l TT',
- },
- localizeNum: function (num) { return String(num); },
- // @ts-ignore
- localizeDayNum: function (d) { return ['th', 'st', 'nd', 'rd'][d % 10 > 3 ? 0 : (d % 100 - d % 10 !== 10) * d % 10]; },
- localizeCommas: function (v) { return v; },
- };
- /// Calendar support range:
- /// Calendar Minimum Maximum
- /// ========== ========== ==========
- /// Gregorian 1900/04/30 2077/11/16
- /// UmAlQura 1318/01/01 1500/12/30
- var UmAlQuraStatic = /** @class */ (function () {
- function UmAlQuraStatic() {
- }
- /**
- * Coverts the given Hijri date to Gregorian.
- * @param hy The Hijri year
- * @param hm The Hijri month
- * @param hd The Hijri day
- */
- UmAlQuraStatic.hijriToGregorian = function (hy, hm, hd) {
- this._checkYearRange(hy);
- this._checkMonthRange(hm);
- this._checkDayRange(hd);
- var nDays = hd - 1;
- var index = hy - this.minCalendarYear;
- var dt = this.hijriYearData[index].gregorianDate;
- var b = this.hijriYearData[index].hijriMonthsLengthFlags;
- for (var m = 1; m < hm; m++) {
- nDays = nDays + 29 + (b & 1);
- b >>= 1;
- }
- dt = this.addDays(dt, nDays);
- return {
- gy: dt.getFullYear(),
- gm: dt.getMonth(),
- gd: dt.getDate(),
- };
- };
- /**
- * Coverts the given Gregorian date to Hijri year, month and day.
- * @param date The date to be converted
- */
- UmAlQuraStatic.gregorianToHijri = function (date) {
- this._checkMillsRange(date.getTime());
- // Find the index where we should start our search by quessing the Hijri year that we will be in HijriYearInfo.
- // A Hijri year is 354 or 355 days. Use 355 days so that we will search from a lower index.
- var index = Math.trunc((date.getTime() - this.minDate.getTime()) / this.millisPerDay / 355);
- do {
- } while (date.getTime() > this.hijriYearData[++index].gregorianDate.getTime());
- if (date.getTime() !== this.hijriYearData[index].gregorianDate.getTime()) {
- index--;
- }
- var nDays = this._dayDiff(date, this.hijriYearData[index].gregorianDate);
- var yh1 = index + this.minCalendarYear;
- var mh1 = 1;
- var dh1 = 1;
- var b = this.hijriYearData[index].hijriMonthsLengthFlags;
- var daysPerThisMonth = 29 + (b & 1);
- while (nDays >= daysPerThisMonth) {
- nDays -= daysPerThisMonth;
- b >>= 1;
- daysPerThisMonth = 29 + (b & 1);
- mh1++;
- }
- dh1 += Math.trunc(nDays);
- return {
- hy: yh1,
- hm: mh1,
- hd: dh1,
- };
- };
- /**
- * Adds the specified amount of Hijri years to the given Gregorian date.
- * @param date The date
- * @param hys The Hijri years to be added
- */
- UmAlQuraStatic.addYears = function (date, hys) {
- return this.addMonths(date, hys * 12);
- };
- /**
- * Adds the specified amount of Hijri months to the given Gregorian date.
- * @param date The date
- * @param hms The Hijri months to be added
- */
- UmAlQuraStatic.addMonths = function (date, hms) {
- // Get the date in UmAlQura calendar.
- var y = this._getDatePart(date, DatePart$1.Year);
- var m = this._getDatePart(date, DatePart$1.Month);
- var d = this._getDatePart(date, DatePart$1.Day);
- var i = m - 1 + hms;
- if (i >= 0) {
- m = i % 12 + 1;
- y += Math.trunc(i / 12);
- }
- else {
- m = 12 + (i + 1) % 12;
- y += Math.trunc((i - 11) / 12);
- }
- if (d > 29) {
- var days = this.getDaysInMonth(y, m);
- if (d > days) {
- d = days;
- }
- }
- var _a = this.hijriToGregorian(y, m, d), gy = _a.gy, gm = _a.gm, gd = _a.gd;
- return this._setTime(new Date(gy, gm, gd), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
- };
- /**
- * Adds the specified amount of weeks to the given Gregorian date.
- * @param date The date
- * @param wks The weeks to be added
- */
- UmAlQuraStatic.addWeeks = function (date, wks) {
- return this.addDays(date, wks * 7);
- };
- /**
- * Adds the specified amount of days to the given Gregorian date.
- * @param date The date
- * @param days The days to be added
- */
- UmAlQuraStatic.addDays = function (date, days) {
- var d = new Date(date.valueOf());
- d.setDate(d.getDate() + days);
- return d;
- };
- /**
- * Adds the specified amount of units to the given Gregorian date.
- * @param date The date
- * @param value The amount of `unit`s to add
- * @param unit The unit of time
- */
- UmAlQuraStatic.addTime = function (date, value, unit) {
- var d = new Date(date.valueOf());
- switch (unit) {
- case 'hour':
- d.setHours(d.getHours() + value);
- break;
- case 'minute':
- d.setMinutes(d.getMinutes() + value);
- break;
- case 'second':
- d.setSeconds(d.getSeconds() + value);
- break;
- case 'millisecond':
- d.setMilliseconds(d.getMilliseconds() + value);
- break;
- default:
- throw new Error('Invalid value for `unit` param');
- }
- return d;
- };
- /**
- * Returns the Hijri day of year for the specified Gregorian date.
- * @param date The date
- */
- UmAlQuraStatic.getDayOfYear = function (date) {
- return this._getDatePart(date, DatePart$1.DayOfYear);
- };
- /**
- * Returns the Hijri day of month for the specified Gregorian date.
- * @param date The date
- */
- UmAlQuraStatic.getDayOfMonth = function (date) {
- return this._getDatePart(date, DatePart$1.Day);
- };
- /**
- * Returns the day of week for the specified Gregorian date.
- * @param date The date
- */
- UmAlQuraStatic.getDayOfWeek = function (date) {
- return date.getDay();
- };
- /**
- * Returns the Hijri week of year for the specified Gregorian date.
- * @param date The date
- */
- UmAlQuraStatic.getWeekOfYear = function (date) {
- var firstDayOfYear = this.startOf(date, 'year').getDay();
- var daysToDayOfWeek = firstDayOfYear - date.getDay();
- var d = this.addDays(date, daysToDayOfWeek);
- return Math.ceil(this.getDayOfYear(d) / 7);
- };
- /**
- * Returns the number of days in the specified Hijri year.
- * @param hy The Hijri year
- */
- UmAlQuraStatic.getDaysInYear = function (hy) {
- this._checkYearRange(hy);
- var days = 0;
- var b = this.hijriYearData[hy - this.minCalendarYear].hijriMonthsLengthFlags;
- for (var m = 1; m <= 12; m++) {
- days = days + 29 + (b & 1);
- b >>= 1;
- }
- if (days !== 354 && days !== 355) {
- throw new Error('Days in year assert error. This is possibly a bug.');
- }
- return days;
- };
- /**
- * Returns the number of days in the specified Hijri year and month.
- * @param hy The Hijri year
- * @param hm The Hijri month
- */
- UmAlQuraStatic.getDaysInMonth = function (hy, hm) {
- this._checkYearRange(hy);
- this._checkMonthRange(hm);
- if ((this.hijriYearData[hy - this.minCalendarYear].hijriMonthsLengthFlags & (1 << hm - 1)) === 0) {
- return 29;
- }
- else {
- return 30;
- }
- };
- /**
- * Returns the Hijri year corresponding to the given Gregorian date.
- * @param date The date
- */
- UmAlQuraStatic.getYear = function (date) {
- return this._getDatePart(date, DatePart$1.Year);
- };
- /**
- * Returns the Hijri month corresponding to the given Gregorian date.
- * @param date The date
- */
- UmAlQuraStatic.getMonth = function (date) {
- return this._getDatePart(date, DatePart$1.Month);
- };
- /**
- * Returns the Hijri month array for the given Gregorian date.
- * @param date The date
- */
- UmAlQuraStatic.getMonthArray = function (date) {
- var weeks = [];
- var month = this.getMonth(date);
- var start = this.startOf(this.startOf(date, 'month'), 'week');
- var end = this.endOf(this.endOf(date, 'month'), 'week');
- var i = 0;
- while (start < end) {
- var w = Math.floor(i / 7);
- var day = new Date(start.valueOf());
- weeks[w] = weeks[w] || [];
- weeks[w].push(this.getMonth(day) === month ? day : null);
- start.setDate(start.getDate() + 1);
- i++;
- }
- return weeks;
- };
- /**
- * Returns the Gregorian date corresponding to the Hijri date starting at the specified unit of time.
- * @param date: The date
- * @param unit: The unit of time
- */
- UmAlQuraStatic.startOf = function (date, unit) {
- var d = new Date(date);
- var _a = this.gregorianToHijri(d), hy = _a.hy, hm = _a.hm;
- switch (unit) {
- case 'year':
- return this.toDate(hy, 1, 1, 0, 0, 0, 0);
- case 'month':
- return this.toDate(hy, hm, 1, 0, 0, 0, 0);
- case 'week':
- var dow = this.getDayOfWeek(d);
- d = this.addDays(d, -dow);
- case 'day':
- d.setHours(0);
- case 'hour':
- d.setMinutes(0);
- case 'minute':
- d.setSeconds(0);
- case 'second':
- d.setMilliseconds(0);
- break;
- default:
- throw new Error('Invalid value for `unit` param');
- }
- return d;
- };
- /**
- * Returns the Gregorian date corresponding to the Hijri date ending at the specified unit of time.
- * @param date: The date
- * @param unit: The unit of time
- */
- UmAlQuraStatic.endOf = function (date, unit) {
- var d = new Date(date);
- var _a = this.gregorianToHijri(d), hy = _a.hy, hm = _a.hm;
- var daysInMonth;
- switch (unit) {
- case 'year':
- daysInMonth = this.getDaysInMonth(hy, 12);
- return this.toDate(hy, 12, daysInMonth, 23, 59, 59, 999);
- case 'month':
- daysInMonth = this.getDaysInMonth(hy, hm);
- return this.toDate(hy, hm, daysInMonth, 23, 59, 59, 999);
- case 'week':
- var dow = this.getDayOfWeek(d);
- d = this.addDays(d, 6 - dow);
- case 'day':
- d.setHours(23);
- case 'hour':
- d.setMinutes(59);
- case 'minute':
- d.setSeconds(59);
- case 'second':
- d.setMilliseconds(999);
- break;
- default:
- throw new Error('Invalid value for `unit` param');
- }
- return d;
- };
- /**
- * Returns whether or not the given Hijri year is a leap year.
- * A Hijri leap year is where the number of days in that year is 355.
- * @param hy The Hijri year
- */
- UmAlQuraStatic.isLeapYear = function (hy) {
- return this.getDaysInYear(hy) === 355;
- };
- /**
- * Converts the specified Hijri date time to a Gregorian Date instance.
- * @param hy The Hijri year
- * @param hm The Hijri month
- * @param hd The Hijri day
- * @param hour The Hour component
- * @param minute The Minute component
- * @param second The Second component
- * @param millisecond The Millisecond component
- */
- UmAlQuraStatic.toDate = function (hy, hm, hd, hour, minute, second, millisecond) {
- if (hour === void 0) { hour = 0; }
- if (minute === void 0) { minute = 0; }
- if (second === void 0) { second = 0; }
- if (millisecond === void 0) { millisecond = 0; }
- var daysInMonth = this.getDaysInMonth(hy, hm);
- if (hd < 1 || hd > daysInMonth) {
- throw new Error("Invalid value for day for the given year/month. Day must be between 1 and " + daysInMonth + ".");
- }
- if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60 || millisecond < 0 || millisecond >= this.millisPerSecond) {
- throw new Error('Invalid value for hour, minute, second or millisecond.');
- }
- var _a = this.hijriToGregorian(hy, hm, hd), gy = _a.gy, gm = _a.gm, gd = _a.gd;
- return this._setTime(new Date(gy, gm, gd), hour, minute, second, millisecond);
- };
- /**
- * Formats the specified Gregorian Date instance in Hijri date.
- * @param date The date
- * @param mask The format mask
- * @param locale The locale to use. If omitted, uses the globally set locale or the default locale.
- */
- UmAlQuraStatic.format = function (date, mask, locale) {
- var _a = this.gregorianToHijri(date), hy = _a.hy, hm = _a.hm, hd = _a.hd;
- return format(date, mask, locale ? this._loadLocale(locale) : this.locale, hy, hm, hd, this.getWeekOfYear(date), this.getDayOfWeek(date));
- };
- /**
- * Sets global locale to be used for formatting.
- * @param locale The locale
- */
- UmAlQuraStatic.setLocale = function (locale) {
- this.locale = this._loadLocale(locale);
- };
- /**
- * Registers the specified locale.
- * @param locale The locale
- */
- UmAlQuraStatic.registerLocale = function (locale) {
- if (!locale.name) {
- throw new Error("The locale's 'name' property must not be empty.");
- }
- if (this.locales[locale.name]) {
- throw new Error("A locale with the same name '" + locale.name + "' is already registered.");
- }
- this.locales[locale.name] = locale;
- };
- UmAlQuraStatic._loadLocale = function (locale) {
- if (this.locales[locale]) {
- return this.locales[locale];
- }
- console.warn("The requested locale '" + locale + "' could not be found. Using the default locale instead.");
- return en;
- };
- UmAlQuraStatic._getDatePart = function (date, part) {
- var _a = this.gregorianToHijri(date), hy = _a.hy, hm = _a.hm, hd = _a.hd;
- switch (part) {
- case DatePart$1.Year:
- return hy;
- case DatePart$1.Month:
- return hm;
- case DatePart$1.Day:
- return hd;
- case DatePart$1.DayOfYear:
- return Math.trunc(this._getAbsoluteDateUmAlQura(hy, hm, hd) - this._getAbsoluteDateUmAlQura(hy, 1, 1) + 1);
- }
- };
- UmAlQuraStatic._setTime = function (date, hour, minute, second, millisecond) {
- date.setHours(hour);
- date.setMinutes(minute);
- date.setSeconds(second);
- date.setMilliseconds(millisecond);
- return date;
- };
- UmAlQuraStatic._getAbsoluteDateUmAlQura = function (hy, hm, hd) {
- var _a = this.hijriToGregorian(hy, hm, hd), gy = _a.gy, gm = _a.gm, gd = _a.gd;
- return new Date(gy, gm, gd).getTime() / this.millisPerDay;
- };
- UmAlQuraStatic._checkYearRange = function (hy) {
- if (hy < this.minCalendarYear || hy > this.maxCalendarYear) {
- throw new Error("Invalid value for year. Must be between " + this.minCalendarYear + " and " + this.maxCalendarYear + ".");
- }
- };
- UmAlQuraStatic._checkMonthRange = function (hm) {
- if (hm < 1 || hm > 12) {
- throw new Error("Invalid value for month. Must be between 1 and 12.");
- }
- };
- UmAlQuraStatic._checkDayRange = function (day) {
- if (day < 1 || day > 30) {
- throw new Error("Invalid value for day. Must be between 1 and 30.");
- }
- };
- UmAlQuraStatic._checkMillsRange = function (millis) {
- if (millis < this.minDate.getTime() || millis > this.maxDate.getTime()) {
- throw new Error("Invalid value for epoch. Must be between " + this.minDate.getTime() + " and " + this.maxDate.getTime() + ".");
- }
- };
- UmAlQuraStatic._dayDiff = function (date, other) {
- return (date.getTime() - other.getTime()) / (1000 * 60 * 60 * 24);
- };
- UmAlQuraStatic._initDateMapping = function () {
- var rawData = [
- // This data is auto generated from the .net BCL which seemed the most accurate
- // Other source which have been found having abnormalities include:
- // http://www.staff.science.uu.nl/~gent0113/islam/addfiles/islamcalendar_dat.js - Has 28 days in one of the month which is impossible
- // http://www.ummulqura.org.sa/ - Has several inaccurate dates
- // Generated by /personal-proj/umalqura/data-gen
- /* DaysPerM GY GM GD D1 D2 D3 D4 D5 D6 D7 D8 D9 D10 D11 D12
- 1318*/ 0x02EA, 1900, 4, 30,
- 0x06E9, 1901, 4, 19,
- 0x0ED2, 1902, 4, 9,
- 0x0EA4, 1903, 3, 30,
- 0x0D4A, 1904, 3, 18,
- 0x0A96, 1905, 3, 7,
- 0x0536, 1906, 2, 24,
- 0x0AB5, 1907, 2, 13,
- 0x0DAA, 1908, 2, 3,
- 0x0BA4, 1909, 1, 23,
- 0x0B49, 1910, 1, 12,
- 0x0A93, 1911, 1, 1,
- 0x052B, 1911, 12, 21,
- 0x0A57, 1912, 12, 9,
- 0x04B6, 1913, 11, 29,
- 0x0AB5, 1914, 11, 18,
- 0x05AA, 1915, 11, 8,
- 0x0D55, 1916, 10, 27,
- 0x0D2A, 1917, 10, 17,
- 0x0A56, 1918, 10, 6,
- 0x04AE, 1919, 9, 25,
- 0x095D, 1920, 9, 13,
- 0x02EC, 1921, 9, 3,
- 0x06D5, 1922, 8, 23,
- 0x06AA, 1923, 8, 13,
- 0x0555, 1924, 8, 1,
- 0x04AB, 1925, 7, 21,
- 0x095B, 1926, 7, 10,
- 0x02BA, 1927, 6, 30,
- 0x0575, 1928, 6, 18,
- 0x0BB2, 1929, 6, 8,
- 0x0764, 1930, 5, 29,
- 0x0749, 1931, 5, 18,
- 0x0655, 1932, 5, 6,
- 0x02AB, 1933, 4, 25,
- 0x055B, 1934, 4, 14,
- 0x0ADA, 1935, 4, 4,
- 0x06D4, 1936, 3, 24,
- 0x0EC9, 1937, 3, 13,
- 0x0D92, 1938, 3, 3,
- 0x0D25, 1939, 2, 20,
- 0x0A4D, 1940, 2, 9,
- 0x02AD, 1941, 1, 28,
- 0x056D, 1942, 1, 17,
- 0x0B6A, 1943, 1, 7,
- 0x0B52, 1943, 12, 28,
- 0x0AA5, 1944, 12, 16,
- 0x0A4B, 1945, 12, 5,
- 0x0497, 1946, 11, 24,
- 0x0937, 1947, 11, 13,
- 0x02B6, 1948, 11, 2,
- 0x0575, 1949, 10, 22,
- 0x0D6A, 1950, 10, 12,
- 0x0D52, 1951, 10, 2,
- 0x0A96, 1952, 9, 20,
- 0x092D, 1953, 9, 9,
- 0x025D, 1954, 8, 29,
- 0x04DD, 1955, 8, 18,
- 0x0ADA, 1956, 8, 7,
- 0x05D4, 1957, 7, 28,
- 0x0DA9, 1958, 7, 17,
- 0x0D52, 1959, 7, 7,
- 0x0AAA, 1960, 6, 25,
- 0x04D6, 1961, 6, 14,
- 0x09B6, 1962, 6, 3,
- 0x0374, 1963, 5, 24,
- 0x0769, 1964, 5, 12,
- 0x0752, 1965, 5, 2,
- 0x06A5, 1966, 4, 21,
- 0x054B, 1967, 4, 10,
- 0x0AAB, 1968, 3, 29,
- 0x055A, 1969, 3, 19,
- 0x0AD5, 1970, 3, 8,
- 0x0DD2, 1971, 2, 26,
- 0x0DA4, 1972, 2, 16,
- 0x0D49, 1973, 2, 4,
- 0x0A95, 1974, 1, 24,
- 0x052D, 1975, 1, 13,
- 0x0A5D, 1976, 1, 2,
- 0x055A, 1976, 12, 22,
- 0x0AD5, 1977, 12, 11,
- 0x06AA, 1978, 12, 1,
- 0x0695, 1979, 11, 20,
- 0x052B, 1980, 11, 8,
- 0x0A57, 1981, 10, 28,
- 0x04AE, 1982, 10, 18,
- 0x0976, 1983, 10, 7,
- 0x056C, 1984, 9, 26,
- 0x0B55, 1985, 9, 15,
- 0x0AAA, 1986, 9, 5,
- 0x0A55, 1987, 8, 25,
- 0x04AD, 1988, 8, 13,
- 0x095D, 1989, 8, 2,
- 0x02DA, 1990, 7, 23,
- 0x05D9, 1991, 7, 12,
- 0x0DB2, 1992, 7, 1,
- 0x0BA4, 1993, 6, 21,
- 0x0B4A, 1994, 6, 10,
- 0x0A55, 1995, 5, 30,
- 0x02B5, 1996, 5, 18,
- 0x0575, 1997, 5, 7,
- 0x0B6A, 1998, 4, 27,
- 0x0BD2, 1999, 4, 17,
- 0x0BC4, 2000, 4, 6,
- 0x0B89, 2001, 3, 26,
- 0x0A95, 2002, 3, 15,
- 0x052D, 2003, 3, 4,
- 0x05AD, 2004, 2, 21,
- 0x0B6A, 2005, 2, 10,
- 0x06D4, 2006, 1, 31,
- 0x0DC9, 2007, 1, 20,
- 0x0D92, 2008, 1, 10,
- 0x0AA6, 2008, 12, 29,
- 0x0956, 2009, 12, 18,
- 0x02AE, 2010, 12, 7,
- 0x056D, 2011, 11, 26,
- 0x036A, 2012, 11, 15,
- 0x0B55, 2013, 11, 4,
- 0x0AAA, 2014, 10, 25,
- 0x094D, 2015, 10, 14,
- 0x049D, 2016, 10, 2,
- 0x095D, 2017, 9, 21,
- 0x02BA, 2018, 9, 11,
- 0x05B5, 2019, 8, 31,
- 0x05AA, 2020, 8, 20,
- 0x0D55, 2021, 8, 9,
- 0x0A9A, 2022, 7, 30,
- 0x092E, 2023, 7, 19,
- 0x026E, 2024, 7, 7,
- 0x055D, 2025, 6, 26,
- 0x0ADA, 2026, 6, 16,
- 0x06D4, 2027, 6, 6,
- 0x06A5, 2028, 5, 25,
- 0x054B, 2029, 5, 14,
- 0x0A97, 2030, 5, 3,
- 0x054E, 2031, 4, 23,
- 0x0AAE, 2032, 4, 11,
- 0x05AC, 2033, 4, 1,
- 0x0BA9, 2034, 3, 21,
- 0x0D92, 2035, 3, 11,
- 0x0B25, 2036, 2, 28,
- 0x064B, 2037, 2, 16,
- 0x0CAB, 2038, 2, 5,
- 0x055A, 2039, 1, 26,
- 0x0B55, 2040, 1, 15,
- 0x06D2, 2041, 1, 4,
- 0x0EA5, 2041, 12, 24,
- 0x0E4A, 2042, 12, 14,
- 0x0A95, 2043, 12, 3,
- 0x052D, 2044, 11, 21,
- 0x0AAD, 2045, 11, 10,
- 0x036C, 2046, 10, 31,
- 0x0759, 2047, 10, 20,
- 0x06D2, 2048, 10, 9,
- 0x0695, 2049, 9, 28,
- 0x052D, 2050, 9, 17,
- 0x0A5B, 2051, 9, 6,
- 0x04BA, 2052, 8, 26,
- 0x09BA, 2053, 8, 15,
- 0x03B4, 2054, 8, 5,
- 0x0B69, 2055, 7, 25,
- 0x0B52, 2056, 7, 14,
- 0x0AA6, 2057, 7, 3,
- 0x04B6, 2058, 6, 22,
- 0x096D, 2059, 6, 11,
- 0x02EC, 2060, 5, 31,
- 0x06D9, 2061, 5, 20,
- 0x0EB2, 2062, 5, 10,
- 0x0D54, 2063, 4, 30,
- 0x0D2A, 2064, 4, 18,
- 0x0A56, 2065, 4, 7,
- 0x04AE, 2066, 3, 27,
- 0x096D, 2067, 3, 16,
- 0x0D6A, 2068, 3, 5,
- 0x0B54, 2069, 2, 23,
- 0x0B29, 2070, 2, 12,
- 0x0A93, 2071, 2, 1,
- 0x052B, 2072, 1, 21,
- 0x0A57, 2073, 1, 9,
- 0x0536, 2073, 12, 30,
- 0x0AB5, 2074, 12, 19,
- 0x06AA, 2075, 12, 9,
- 0x0E93, 2076, 11, 27,
- 0, 2077, 11, 17 /* 0 0 0 0 0 0 0 0 0 0 0 0 2077-11-17*/
- ];
- var mapping = [];
- for (var i = 0; i < rawData.length / 4; i++) {
- mapping.push(new DateMapping(rawData[i * 4], rawData[i * 4 + 1], rawData[i * 4 + 2] - 1, rawData[i * 4 + 3]));
- }
- return mapping;
- };
- // private static readonly maxSeconds = 9223372036854775807 / 10000000;
- // private static readonly minSeconds = -9223372036854775807 / 10000000;
- UmAlQuraStatic.millisPerSecond = 1000;
- UmAlQuraStatic.millisPerMinute = UmAlQuraStatic.millisPerSecond * 60;
- UmAlQuraStatic.millisPerHour = UmAlQuraStatic.millisPerMinute * 60;
- UmAlQuraStatic.millisPerDay = UmAlQuraStatic.millisPerHour * 24;
- UmAlQuraStatic.minDate = new Date(1900, 3, 30);
- UmAlQuraStatic.maxDate = new Date(2077, 10, 16, 23, 59, 59, 999);
- UmAlQuraStatic.hijriYearData = UmAlQuraStatic._initDateMapping();
- // Holds globally set locale
- UmAlQuraStatic.locale = en;
- // Holds registered locales
- UmAlQuraStatic.locales = {};
- UmAlQuraStatic.minCalendarYear = 1318;
- UmAlQuraStatic.maxCalendarYear = 1500;
- return UmAlQuraStatic;
- }());
- // Register both locales so they'll be bundled in the package.
- // This is fine since for this library, there will probably be
- // only be these two locales.
- UmAlQuraStatic.registerLocale(ar);
- UmAlQuraStatic.registerLocale(en);
- var UmAlQura = /** @class */ (function () {
- function UmAlQura(dateOrHy, hm, hd, hour, minute, second, millisecond) {
- if (hour === void 0) { hour = 0; }
- if (minute === void 0) { minute = 0; }
- if (second === void 0) { second = 0; }
- if (millisecond === void 0) { millisecond = 0; }
- this._date = new Date(0, 0, 0);
- this._hy = 0;
- this._hm = 0;
- this._hd = 0;
- if (dateOrHy instanceof Date) {
- this._setDate(dateOrHy);
- }
- else if (dateOrHy !== undefined && hm !== undefined && hd !== undefined) {
- var _a = UmAlQuraStatic.hijriToGregorian(dateOrHy, hm, hd), gy = _a.gy, gm = _a.gm, gd = _a.gd;
- this._setDate(new Date(gy, gm, gd, hour, minute, second, millisecond));
- }
- else {
- this._setDate(new Date());
- }
- }
- Object.defineProperty(UmAlQura.prototype, "date", {
- /**
- * Returns the `Date` object of this instance.
- */
- get: function () { return new Date(this._date.valueOf()); },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "hy", {
- /**
- * Returns the Hijri year of this instance.
- */
- get: function () { return this._hy; },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "hm", {
- /**
- * Returns the Hijri month of this instance.
- */
- get: function () { return this._hm; },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "hd", {
- /**
- * Returns the Hijri day of month of this instance.
- */
- get: function () { return this._hd; },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "dayOfYear", {
- /**
- * Returns the Hijri day of year of this instance.
- */
- get: function () { return UmAlQuraStatic.getDayOfYear(this.date); },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "dayOfWeek", {
- /**
- * Returns the day of week of this instance.
- */
- get: function () { return UmAlQuraStatic.getDayOfWeek(this.date); },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "weekOfYear", {
- /**
- * Returns the Hijri week of year of this instance.
- */
- get: function () { return UmAlQuraStatic.getWeekOfYear(this.date); },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "daysInYear", {
- /**
- * Returns the number of days in year of this instance.
- */
- get: function () { return UmAlQuraStatic.getDaysInYear(this.hy); },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "daysInMonth", {
- /**
- * Returns the number of days in month of this instance.
- */
- get: function () { return UmAlQuraStatic.getDaysInMonth(this.hy, this.hm); },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "isLeapYear", {
- /**
- * Returns whether or not the Hijri year of this instance is a leap year.
- */
- get: function () { return UmAlQuraStatic.isLeapYear(this.hy); },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(UmAlQura.prototype, "monthArray", {
- /**
- * Returns the Hijri month array of this instance.
- */
- get: function () {
- return UmAlQuraStatic.getMonthArray(this.date)
- .map(function (w) { return w.map(function (d) { return d ? new UmAlQura(d) : null; }); });
- },
- enumerable: true,
- configurable: true
- });
- /**
- * Adds the specified amount of `unit` to the current date and returns a new instance.
- * @param {number} value The amount of units to be added
- * @param {UnitOfTimeMs} unit The unit of time
- */
- UmAlQura.prototype.add = function (value, unit) {
- switch (unit) {
- case 'year':
- return new UmAlQura(UmAlQuraStatic.addYears(this.date, value));
- case 'month':
- return new UmAlQura(UmAlQuraStatic.addMonths(this.date, value));
- case 'week':
- return new UmAlQura(UmAlQuraStatic.addWeeks(this.date, value));
- case 'day':
- return new UmAlQura(UmAlQuraStatic.addDays(this.date, value));
- case 'hour':
- case 'minute':
- case 'second':
- case 'millisecond':
- return new UmAlQura(UmAlQuraStatic.addTime(this.date, value, unit));
- default:
- throw new Error('Invalid value for `unit`');
- }
- };
- /**
- * Subtracts the specified amount of `unit` from the current date and returns a new instance.
- * @param {number} value The amount of units to be subtracted
- * @param {UnitOfTimeMs} unit The unit of time
- */
- UmAlQura.prototype.subtract = function (value, unit) {
- return this.add(value * -1, unit);
- };
- /**
- * Returns a new instance having the Hijri date of this instance starting at the specified unit of time.
- * @param {UnitOfTime} unit The unit of time
- */
- UmAlQura.prototype.startOf = function (unit) {
- return new UmAlQura(UmAlQuraStatic.startOf(this.date, unit));
- };
- /**
- * Returns a new instance having the Hijri date of this instance ending at the specified unit of time.
- * @param {UnitOfTime} unit The unit of time
- */
- UmAlQura.prototype.endOf = function (unit) {
- return new UmAlQura(UmAlQuraStatic.endOf(this.date, unit));
- };
- /**
- * Checks if current date is before the specified date. The comparison is made based on milliseconds of both
- * times. This can be changed by specifying a value for the `unit` parameter.
- * @param {(UmAlQura | Date)} other The date to compare against
- * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
- */
- UmAlQura.prototype.isBefore = function (other, unit) {
- if (unit === void 0) { unit = 'millisecond'; }
- var thisDate = this.date;
- var thatDate = other instanceof Date ? other : other.date;
- if (unit === 'millisecond') {
- return thisDate.valueOf() < thatDate.valueOf();
- }
- else {
- return this.endOf(unit).date.valueOf() < thatDate.valueOf();
- }
- };
- /**
- * Checks if current date is after the specified date. The comparison is made based on milliseconds,
- * this can be changed by specifying a value for the `unit` parameter.
- * @param {(UmAlQura | Date)} other The date to compare against
- * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
- */
- UmAlQura.prototype.isAfter = function (other, unit) {
- if (unit === void 0) { unit = 'millisecond'; }
- var thisDate = this.date;
- var thatDate = other instanceof Date ? other : other.date;
- if (unit === 'millisecond') {
- return thisDate.valueOf() > thatDate.valueOf();
- }
- else {
- return this.startOf(unit).date.valueOf() > thatDate.valueOf();
- }
- };
- /**
- * Checks if current date is same as the specified date. The comparison is made based on milliseconds,
- * this can be changed by specifying a value for the `unit` parameter.
- * @param {(UmAlQura | Date)} other The date to compare against
- * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
- */
- UmAlQura.prototype.isSame = function (other, unit) {
- if (unit === void 0) { unit = 'millisecond'; }
- var thisDate = this.date;
- var thatDate = other instanceof Date ? other : other.date;
- if (unit === 'millisecond') {
- return thisDate.valueOf() === thatDate.valueOf();
- }
- else {
- return this.startOf(unit).date.valueOf() === new UmAlQura(thatDate).startOf(unit).date.valueOf();
- }
- };
- /**
- * Checks if current date is same as or before the specified date. The comparison is made based on milliseconds,
- * this can be changed by specifying a value for the `unit` parameter.
- * @param {(UmAlQura | Date)} other The date to compare against
- * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
- */
- UmAlQura.prototype.isSameOrBefore = function (other, unit) {
- if (unit === void 0) { unit = 'millisecond'; }
- return this.isSame(other, unit) || this.isBefore(other, unit);
- };
- /**
- * Checks if current date is same as or after the specified date. The comparison is made based on milliseconds,
- * this can be changed by specifying a value for the `unit` parameter.
- * @param {(UmAlQura | Date)} other The date to compare against
- * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
- */
- UmAlQura.prototype.isSameOrAfter = function (other, unit) {
- if (unit === void 0) { unit = 'millisecond'; }
- return this.isSame(other, unit) || this.isAfter(other, unit);
- };
- /**
- * Checks if current date is between the specified `from`/`to` dates. The comparison is made based on milliseconds,
- * this can be changed by specifying a value for the `unit` parameter. The comparison is exclusive of both ends by default,
- * this can be controller by `fromInclusive`/`toInclusive` parameters.
- * @param {(UmAlQura | Date)} from The lower bound date
- * @param {(UmAlQura | Date)} to The higher bound date
- * @param {boolean} [fromInclusive=false] Whether lower bound is inclusive, defaults to false.
- * @param {boolean} [toInclusive=false] Whether upper bound is inclusive, defaults to false.
- * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
- */
- UmAlQura.prototype.isBetween = function (from, to, fromInclusive, toInclusive, unit) {
- if (fromInclusive === void 0) { fromInclusive = false; }
- if (toInclusive === void 0) { toInclusive = false; }
- if (unit === void 0) { unit = 'millisecond'; }
- return (fromInclusive ? this.isSameOrAfter(from, unit) : this.isAfter(from, unit)) &&
- (toInclusive ? this.isSameOrBefore(to, unit) : this.isBefore(to, unit));
- };
- /**
- * Formats this instance in Hijri date.
- * @param {string} mask The mask
- * @param {string} locale The locale to use. If omitted, uses the locale set via `locale` or the default locale.
- */
- UmAlQura.prototype.format = function (mask, locale) {
- // tslint:disable-next-line:no-string-literal
- return UmAlQuraStatic.format(this.date, mask, locale || UmAlQuraStatic['locale'].name);
- };
- /**
- * Clones this instance and returns a new instance with the same values.
- */
- UmAlQura.prototype.clone = function () {
- return new UmAlQura(this.date);
- };
- UmAlQura.prototype._setDate = function (date) {
- var _a = UmAlQuraStatic.gregorianToHijri(date), hy = _a.hy, hm = _a.hm, hd = _a.hd;
- this._date = new Date(date.valueOf());
- this._hy = hy;
- this._hm = hm;
- this._hd = hd;
- };
- return UmAlQura;
- }());
- function umalqura(dateOrHy, hm, hd, hour, minute, second, millisecond) {
- if (hour === void 0) { hour = 0; }
- if (minute === void 0) { minute = 0; }
- if (second === void 0) { second = 0; }
- if (millisecond === void 0) { millisecond = 0; }
- if (dateOrHy instanceof Date) {
- return new UmAlQura(dateOrHy);
- }
- else if (dateOrHy !== undefined && hm !== undefined && hd !== undefined) {
- return new UmAlQura(dateOrHy, hm, hd, hour, minute, second, millisecond);
- }
- else {
- return new UmAlQura();
- }
- }
- /**
- * Returns the library version.
- */
- umalqura.VERSION = "0.0.7";
- /**
- * Returns a class which exposes static Hijri related functions.
- */
- umalqura.$ = UmAlQuraStatic;
- /**
- * Returns the minimum supported Hijri date.
- */
- umalqura.min = umalqura(UmAlQuraStatic['minCalendarYear'], 1, 1);
- /**
- * Returns the maximum supported Hijri date.
- */
- umalqura.max = umalqura(UmAlQuraStatic['maxCalendarYear'], 1, 1).endOf('year');
- /**
- * Gets or sets the global locale
- * @param locale The locale to set. If omitted, returns the current locale
- */
- umalqura.locale = function (locale) { return locale ? UmAlQuraStatic.setLocale(locale) : UmAlQuraStatic['locale'].name; };
- /**
- * Returns whether the currently set locale is RTL or not.
- */
- umalqura.rtl = function () { return !!UmAlQuraStatic['locale'].rtl; };
- /**
- * Returns the times names using the currently set locale.
- */
- umalqura.times = function () { return UmAlQuraStatic['locale'].timeNames.slice(); };
- /**
- * Returns the days names using the currently set locale.
- */
- umalqura.days = function () { return UmAlQuraStatic['locale'].dayNames.slice(); };
- /**
- * Returns the days short names using the currently set locale.
- */
- umalqura.daysShort = function () { return UmAlQuraStatic['locale'].dayNamesShort.slice(); };
- /**
- * Returns the months names using the currently set locale.
- */
- umalqura.months = function () { return UmAlQuraStatic['locale'].monthNames.slice(); };
- /**
- * Returns the months short names using the currently set locale.
- */
- umalqura.monthsShort = function () { return UmAlQuraStatic['locale'].monthNamesShort.slice(); };
- /**
- * Returns the localized number for the given number using the currently set locale.
- */
- umalqura.localizeNum = function (num) { return UmAlQuraStatic['locale'].localizeNum(num); };
- /**
- * Returns the localized day number for the given day number using the currently set locale.
- */
- umalqura.localizeDayNum = function (d) { return UmAlQuraStatic['locale'].localizeDayNum(d); };
- export default umalqura;
|