index.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var DateMapping = /** @class */ (function () {
  4. function DateMapping(monthsLengthFlags, gy, gm, gd) {
  5. this.hijriMonthsLengthFlags = monthsLengthFlags;
  6. this.gregorianDate = new Date(gy, gm, gd);
  7. }
  8. return DateMapping;
  9. }());
  10. var DatePart;
  11. (function (DatePart) {
  12. DatePart[DatePart["Year"] = 0] = "Year";
  13. DatePart[DatePart["DayOfYear"] = 1] = "DayOfYear";
  14. DatePart[DatePart["Month"] = 2] = "Month";
  15. DatePart[DatePart["Day"] = 3] = "Day";
  16. })(DatePart || (DatePart = {}));
  17. var DatePart$1 = DatePart;
  18. // Parts of this file are (c) 2007-2009 Steven Levithan <stevenlevithan.com>
  19. // https://github.com/felixge/node-dateformat/blob/master/lib/dateformat.js
  20. var token = /d{1,4}|M{1,4}|yy(?:yy)?|([HhmsTt])\1?|[LlSWN]|"[^"]*"|'[^']*'/g;
  21. function pad(val, locale, len) {
  22. val = String(val);
  23. len = len || 2;
  24. while (val.length < len) {
  25. val = '0' + val;
  26. }
  27. return locale.localizeNum(val);
  28. }
  29. function format(date, mask, locale, hy, hm, hd, woy, dow) {
  30. mask = String(locale.masks[mask] || mask || locale.masks.default);
  31. var _ = 'get';
  32. var d = hd;
  33. var D = date[_ + 'Day']();
  34. var m = hm;
  35. var y = hy;
  36. var H = date[_ + 'Hours']();
  37. var M = date[_ + 'Minutes']();
  38. var s = date[_ + 'Seconds']();
  39. var L = date[_ + 'Milliseconds']();
  40. var W = woy;
  41. var N = dow;
  42. var flags = {
  43. d: locale.localizeNum(d),
  44. dd: pad(d, locale),
  45. ddd: locale.dayNamesShort[D],
  46. dddd: locale.dayNames[D],
  47. M: locale.localizeNum(m),
  48. MM: pad(m, locale),
  49. MMM: locale.monthNamesShort[m - 1],
  50. MMMM: locale.monthNames[m - 1],
  51. yy: locale.localizeNum(String(y).slice(2)),
  52. yyyy: locale.localizeNum(y),
  53. h: locale.localizeNum(H % 12 || 12),
  54. hh: pad(H % 12 || 12, locale),
  55. H: locale.localizeNum(H),
  56. HH: pad(H, locale),
  57. m: locale.localizeNum(M),
  58. mm: pad(M, locale),
  59. s: locale.localizeNum(s),
  60. ss: pad(s, locale),
  61. l: pad(L, locale, 3),
  62. L: pad(Math.round(L / 10), locale),
  63. t: H < 12 ? locale.timeNames[0] : locale.timeNames[1],
  64. tt: H < 12 ? locale.timeNames[2] : locale.timeNames[3],
  65. T: H < 12 ? locale.timeNames[4] : locale.timeNames[5],
  66. TT: H < 12 ? locale.timeNames[6] : locale.timeNames[7],
  67. S: locale.localizeDayNum(d),
  68. W: locale.localizeNum(W),
  69. N: locale.localizeNum(N),
  70. };
  71. return locale.localizeCommas(mask.replace(token, function (match) { return flags[match]; }));
  72. }
  73. var symbolMap = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
  74. var dayNumMap = ['الأول', 'الثاني', 'الثالث', 'الرابع', 'الخامس', 'السادس', 'السابع', 'الثامن', 'التاسع', 'العاشر', 'الحادي عشر'];
  75. var ar = {
  76. name: 'ar',
  77. rtl: true,
  78. dayNamesShort: ['ح', 'ن', 'ث', 'ر', 'خ', 'ج', 'س'],
  79. dayNames: ['الأحد', 'الإثنين', 'الثلاثاء', 'الأربعاء', 'الخميس', 'الجمعة', 'السبت'],
  80. monthNamesShort: ['محرم', 'صفر', 'ربيع ١', 'ربيع ٢', 'جمادى ١', 'جمادى ٢', 'رجب', 'شعبان', 'رمضان', 'شوال', 'ذو القعدة', 'ذو الحجة'],
  81. monthNames: ['محرم', 'صفر', 'ربيع الأول', 'ربيع الثاني', 'جمادى الأولى', 'جمادى الآخرة', 'رجب', 'شعبان', 'رمضان', 'شوال', 'ذو القعدة', 'ذو الحجة'],
  82. timeNames: ['ص', 'م', 'ص', 'م', 'ص', 'م', 'ص', 'م'],
  83. masks: {
  84. default: 'ddd dd MMM yyyy HH:mm:ss',
  85. shortDate: 'yy/M/d',
  86. mediumDate: 'd MMM, yyyy',
  87. longDate: 'd MMMM, yyyy',
  88. fullDate: 'dddd, d MMMM, yyyy',
  89. shortTime: 'h:mm TT',
  90. mediumTime: 'h:mm:ss TT',
  91. longTime: 'h:mm:ss.l TT',
  92. },
  93. localizeNum: function (num) {
  94. var s = String(num);
  95. var output = '';
  96. for (var i = 0; i < s.length; i++) {
  97. output += symbolMap[s.charAt(i)];
  98. }
  99. return output;
  100. },
  101. localizeDayNum: function (d) {
  102. var output = '';
  103. if (d === 11) {
  104. output = 'الحادي عشر';
  105. }
  106. else if (d === 20) {
  107. output = 'العشرون';
  108. }
  109. else if (d === 30) {
  110. output = 'الثلاثون';
  111. }
  112. else {
  113. output = dayNumMap[d - 1];
  114. }
  115. var section = d / 10;
  116. if (section > 1.1 && section < 2) {
  117. output = dayNumMap[(d - 1) % 10] + ' عشر';
  118. }
  119. else if (section > 2 && section < 3) {
  120. output = dayNumMap[(d - 1) % 10] + ' والعشرون';
  121. }
  122. return output + ' من';
  123. },
  124. localizeCommas: function (v) { return v.replace(/,/g, '،'); },
  125. };
  126. var en = {
  127. name: 'en',
  128. dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  129. dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  130. monthNamesShort: ['Muh', 'Ṣaf', 'Rab-I', 'Rab-II', 'Jum-I', 'Jum-II', 'Raj', 'Sha', 'Ram', 'Shw', 'Dhū-Q', 'Dhū-Ḥ'],
  131. 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'],
  132. timeNames: ['a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM'],
  133. masks: {
  134. default: 'ddd MMM dd yyyy HH:mm:ss',
  135. shortDate: 'M/d/yy',
  136. mediumDate: 'MMM d, yyyy',
  137. longDate: 'MMMM d, yyyy',
  138. fullDate: 'dddd, MMMM d, yyyy',
  139. shortTime: 'h:mm TT',
  140. mediumTime: 'h:mm:ss TT',
  141. longTime: 'h:mm:ss.l TT',
  142. },
  143. localizeNum: function (num) { return String(num); },
  144. // @ts-ignore
  145. localizeDayNum: function (d) { return ['th', 'st', 'nd', 'rd'][d % 10 > 3 ? 0 : (d % 100 - d % 10 !== 10) * d % 10]; },
  146. localizeCommas: function (v) { return v; },
  147. };
  148. /// Calendar support range:
  149. /// Calendar Minimum Maximum
  150. /// ========== ========== ==========
  151. /// Gregorian 1900/04/30 2077/11/16
  152. /// UmAlQura 1318/01/01 1500/12/30
  153. var UmAlQuraStatic = /** @class */ (function () {
  154. function UmAlQuraStatic() {
  155. }
  156. /**
  157. * Coverts the given Hijri date to Gregorian.
  158. * @param hy The Hijri year
  159. * @param hm The Hijri month
  160. * @param hd The Hijri day
  161. */
  162. UmAlQuraStatic.hijriToGregorian = function (hy, hm, hd) {
  163. this._checkYearRange(hy);
  164. this._checkMonthRange(hm);
  165. this._checkDayRange(hd);
  166. var nDays = hd - 1;
  167. var index = hy - this.minCalendarYear;
  168. var dt = this.hijriYearData[index].gregorianDate;
  169. var b = this.hijriYearData[index].hijriMonthsLengthFlags;
  170. for (var m = 1; m < hm; m++) {
  171. nDays = nDays + 29 + (b & 1);
  172. b >>= 1;
  173. }
  174. dt = this.addDays(dt, nDays);
  175. return {
  176. gy: dt.getFullYear(),
  177. gm: dt.getMonth(),
  178. gd: dt.getDate(),
  179. };
  180. };
  181. /**
  182. * Coverts the given Gregorian date to Hijri year, month and day.
  183. * @param date The date to be converted
  184. */
  185. UmAlQuraStatic.gregorianToHijri = function (date) {
  186. this._checkMillsRange(date.getTime());
  187. // Find the index where we should start our search by quessing the Hijri year that we will be in HijriYearInfo.
  188. // A Hijri year is 354 or 355 days. Use 355 days so that we will search from a lower index.
  189. var index = Math.trunc((date.getTime() - this.minDate.getTime()) / this.millisPerDay / 355);
  190. do {
  191. } while (date.getTime() > this.hijriYearData[++index].gregorianDate.getTime());
  192. if (date.getTime() !== this.hijriYearData[index].gregorianDate.getTime()) {
  193. index--;
  194. }
  195. var nDays = this._dayDiff(date, this.hijriYearData[index].gregorianDate);
  196. var yh1 = index + this.minCalendarYear;
  197. var mh1 = 1;
  198. var dh1 = 1;
  199. var b = this.hijriYearData[index].hijriMonthsLengthFlags;
  200. var daysPerThisMonth = 29 + (b & 1);
  201. while (nDays >= daysPerThisMonth) {
  202. nDays -= daysPerThisMonth;
  203. b >>= 1;
  204. daysPerThisMonth = 29 + (b & 1);
  205. mh1++;
  206. }
  207. dh1 += Math.trunc(nDays);
  208. return {
  209. hy: yh1,
  210. hm: mh1,
  211. hd: dh1,
  212. };
  213. };
  214. /**
  215. * Adds the specified amount of Hijri years to the given Gregorian date.
  216. * @param date The date
  217. * @param hys The Hijri years to be added
  218. */
  219. UmAlQuraStatic.addYears = function (date, hys) {
  220. return this.addMonths(date, hys * 12);
  221. };
  222. /**
  223. * Adds the specified amount of Hijri months to the given Gregorian date.
  224. * @param date The date
  225. * @param hms The Hijri months to be added
  226. */
  227. UmAlQuraStatic.addMonths = function (date, hms) {
  228. // Get the date in UmAlQura calendar.
  229. var y = this._getDatePart(date, DatePart$1.Year);
  230. var m = this._getDatePart(date, DatePart$1.Month);
  231. var d = this._getDatePart(date, DatePart$1.Day);
  232. var i = m - 1 + hms;
  233. if (i >= 0) {
  234. m = i % 12 + 1;
  235. y += Math.trunc(i / 12);
  236. }
  237. else {
  238. m = 12 + (i + 1) % 12;
  239. y += Math.trunc((i - 11) / 12);
  240. }
  241. if (d > 29) {
  242. var days = this.getDaysInMonth(y, m);
  243. if (d > days) {
  244. d = days;
  245. }
  246. }
  247. var _a = this.hijriToGregorian(y, m, d), gy = _a.gy, gm = _a.gm, gd = _a.gd;
  248. return this._setTime(new Date(gy, gm, gd), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
  249. };
  250. /**
  251. * Adds the specified amount of weeks to the given Gregorian date.
  252. * @param date The date
  253. * @param wks The weeks to be added
  254. */
  255. UmAlQuraStatic.addWeeks = function (date, wks) {
  256. return this.addDays(date, wks * 7);
  257. };
  258. /**
  259. * Adds the specified amount of days to the given Gregorian date.
  260. * @param date The date
  261. * @param days The days to be added
  262. */
  263. UmAlQuraStatic.addDays = function (date, days) {
  264. var d = new Date(date.valueOf());
  265. d.setDate(d.getDate() + days);
  266. return d;
  267. };
  268. /**
  269. * Adds the specified amount of units to the given Gregorian date.
  270. * @param date The date
  271. * @param value The amount of `unit`s to add
  272. * @param unit The unit of time
  273. */
  274. UmAlQuraStatic.addTime = function (date, value, unit) {
  275. var d = new Date(date.valueOf());
  276. switch (unit) {
  277. case 'hour':
  278. d.setHours(d.getHours() + value);
  279. break;
  280. case 'minute':
  281. d.setMinutes(d.getMinutes() + value);
  282. break;
  283. case 'second':
  284. d.setSeconds(d.getSeconds() + value);
  285. break;
  286. case 'millisecond':
  287. d.setMilliseconds(d.getMilliseconds() + value);
  288. break;
  289. default:
  290. throw new Error('Invalid value for `unit` param');
  291. }
  292. return d;
  293. };
  294. /**
  295. * Returns the Hijri day of year for the specified Gregorian date.
  296. * @param date The date
  297. */
  298. UmAlQuraStatic.getDayOfYear = function (date) {
  299. return this._getDatePart(date, DatePart$1.DayOfYear);
  300. };
  301. /**
  302. * Returns the Hijri day of month for the specified Gregorian date.
  303. * @param date The date
  304. */
  305. UmAlQuraStatic.getDayOfMonth = function (date) {
  306. return this._getDatePart(date, DatePart$1.Day);
  307. };
  308. /**
  309. * Returns the day of week for the specified Gregorian date.
  310. * @param date The date
  311. */
  312. UmAlQuraStatic.getDayOfWeek = function (date) {
  313. return date.getDay();
  314. };
  315. /**
  316. * Returns the Hijri week of year for the specified Gregorian date.
  317. * @param date The date
  318. */
  319. UmAlQuraStatic.getWeekOfYear = function (date) {
  320. var firstDayOfYear = this.startOf(date, 'year').getDay();
  321. var daysToDayOfWeek = firstDayOfYear - date.getDay();
  322. var d = this.addDays(date, daysToDayOfWeek);
  323. return Math.ceil(this.getDayOfYear(d) / 7);
  324. };
  325. /**
  326. * Returns the number of days in the specified Hijri year.
  327. * @param hy The Hijri year
  328. */
  329. UmAlQuraStatic.getDaysInYear = function (hy) {
  330. this._checkYearRange(hy);
  331. var days = 0;
  332. var b = this.hijriYearData[hy - this.minCalendarYear].hijriMonthsLengthFlags;
  333. for (var m = 1; m <= 12; m++) {
  334. days = days + 29 + (b & 1);
  335. b >>= 1;
  336. }
  337. if (days !== 354 && days !== 355) {
  338. throw new Error('Days in year assert error. This is possibly a bug.');
  339. }
  340. return days;
  341. };
  342. /**
  343. * Returns the number of days in the specified Hijri year and month.
  344. * @param hy The Hijri year
  345. * @param hm The Hijri month
  346. */
  347. UmAlQuraStatic.getDaysInMonth = function (hy, hm) {
  348. this._checkYearRange(hy);
  349. this._checkMonthRange(hm);
  350. if ((this.hijriYearData[hy - this.minCalendarYear].hijriMonthsLengthFlags & (1 << hm - 1)) === 0) {
  351. return 29;
  352. }
  353. else {
  354. return 30;
  355. }
  356. };
  357. /**
  358. * Returns the Hijri year corresponding to the given Gregorian date.
  359. * @param date The date
  360. */
  361. UmAlQuraStatic.getYear = function (date) {
  362. return this._getDatePart(date, DatePart$1.Year);
  363. };
  364. /**
  365. * Returns the Hijri month corresponding to the given Gregorian date.
  366. * @param date The date
  367. */
  368. UmAlQuraStatic.getMonth = function (date) {
  369. return this._getDatePart(date, DatePart$1.Month);
  370. };
  371. /**
  372. * Returns the Hijri month array for the given Gregorian date.
  373. * @param date The date
  374. */
  375. UmAlQuraStatic.getMonthArray = function (date) {
  376. var weeks = [];
  377. var month = this.getMonth(date);
  378. var start = this.startOf(this.startOf(date, 'month'), 'week');
  379. var end = this.endOf(this.endOf(date, 'month'), 'week');
  380. var i = 0;
  381. while (start < end) {
  382. var w = Math.floor(i / 7);
  383. var day = new Date(start.valueOf());
  384. weeks[w] = weeks[w] || [];
  385. weeks[w].push(this.getMonth(day) === month ? day : null);
  386. start.setDate(start.getDate() + 1);
  387. i++;
  388. }
  389. return weeks;
  390. };
  391. /**
  392. * Returns the Gregorian date corresponding to the Hijri date starting at the specified unit of time.
  393. * @param date: The date
  394. * @param unit: The unit of time
  395. */
  396. UmAlQuraStatic.startOf = function (date, unit) {
  397. var d = new Date(date);
  398. var _a = this.gregorianToHijri(d), hy = _a.hy, hm = _a.hm;
  399. switch (unit) {
  400. case 'year':
  401. return this.toDate(hy, 1, 1, 0, 0, 0, 0);
  402. case 'month':
  403. return this.toDate(hy, hm, 1, 0, 0, 0, 0);
  404. case 'week':
  405. var dow = this.getDayOfWeek(d);
  406. d = this.addDays(d, -dow);
  407. case 'day':
  408. d.setHours(0);
  409. case 'hour':
  410. d.setMinutes(0);
  411. case 'minute':
  412. d.setSeconds(0);
  413. case 'second':
  414. d.setMilliseconds(0);
  415. break;
  416. default:
  417. throw new Error('Invalid value for `unit` param');
  418. }
  419. return d;
  420. };
  421. /**
  422. * Returns the Gregorian date corresponding to the Hijri date ending at the specified unit of time.
  423. * @param date: The date
  424. * @param unit: The unit of time
  425. */
  426. UmAlQuraStatic.endOf = function (date, unit) {
  427. var d = new Date(date);
  428. var _a = this.gregorianToHijri(d), hy = _a.hy, hm = _a.hm;
  429. var daysInMonth;
  430. switch (unit) {
  431. case 'year':
  432. daysInMonth = this.getDaysInMonth(hy, 12);
  433. return this.toDate(hy, 12, daysInMonth, 23, 59, 59, 999);
  434. case 'month':
  435. daysInMonth = this.getDaysInMonth(hy, hm);
  436. return this.toDate(hy, hm, daysInMonth, 23, 59, 59, 999);
  437. case 'week':
  438. var dow = this.getDayOfWeek(d);
  439. d = this.addDays(d, 6 - dow);
  440. case 'day':
  441. d.setHours(23);
  442. case 'hour':
  443. d.setMinutes(59);
  444. case 'minute':
  445. d.setSeconds(59);
  446. case 'second':
  447. d.setMilliseconds(999);
  448. break;
  449. default:
  450. throw new Error('Invalid value for `unit` param');
  451. }
  452. return d;
  453. };
  454. /**
  455. * Returns whether or not the given Hijri year is a leap year.
  456. * A Hijri leap year is where the number of days in that year is 355.
  457. * @param hy The Hijri year
  458. */
  459. UmAlQuraStatic.isLeapYear = function (hy) {
  460. return this.getDaysInYear(hy) === 355;
  461. };
  462. /**
  463. * Converts the specified Hijri date time to a Gregorian Date instance.
  464. * @param hy The Hijri year
  465. * @param hm The Hijri month
  466. * @param hd The Hijri day
  467. * @param hour The Hour component
  468. * @param minute The Minute component
  469. * @param second The Second component
  470. * @param millisecond The Millisecond component
  471. */
  472. UmAlQuraStatic.toDate = function (hy, hm, hd, hour, minute, second, millisecond) {
  473. if (hour === void 0) { hour = 0; }
  474. if (minute === void 0) { minute = 0; }
  475. if (second === void 0) { second = 0; }
  476. if (millisecond === void 0) { millisecond = 0; }
  477. var daysInMonth = this.getDaysInMonth(hy, hm);
  478. if (hd < 1 || hd > daysInMonth) {
  479. throw new Error("Invalid value for day for the given year/month. Day must be between 1 and " + daysInMonth + ".");
  480. }
  481. if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60 || millisecond < 0 || millisecond >= this.millisPerSecond) {
  482. throw new Error('Invalid value for hour, minute, second or millisecond.');
  483. }
  484. var _a = this.hijriToGregorian(hy, hm, hd), gy = _a.gy, gm = _a.gm, gd = _a.gd;
  485. return this._setTime(new Date(gy, gm, gd), hour, minute, second, millisecond);
  486. };
  487. /**
  488. * Formats the specified Gregorian Date instance in Hijri date.
  489. * @param date The date
  490. * @param mask The format mask
  491. * @param locale The locale to use. If omitted, uses the globally set locale or the default locale.
  492. */
  493. UmAlQuraStatic.format = function (date, mask, locale) {
  494. var _a = this.gregorianToHijri(date), hy = _a.hy, hm = _a.hm, hd = _a.hd;
  495. return format(date, mask, locale ? this._loadLocale(locale) : this.locale, hy, hm, hd, this.getWeekOfYear(date), this.getDayOfWeek(date));
  496. };
  497. /**
  498. * Sets global locale to be used for formatting.
  499. * @param locale The locale
  500. */
  501. UmAlQuraStatic.setLocale = function (locale) {
  502. this.locale = this._loadLocale(locale);
  503. };
  504. /**
  505. * Registers the specified locale.
  506. * @param locale The locale
  507. */
  508. UmAlQuraStatic.registerLocale = function (locale) {
  509. if (!locale.name) {
  510. throw new Error("The locale's 'name' property must not be empty.");
  511. }
  512. if (this.locales[locale.name]) {
  513. throw new Error("A locale with the same name '" + locale.name + "' is already registered.");
  514. }
  515. this.locales[locale.name] = locale;
  516. };
  517. UmAlQuraStatic._loadLocale = function (locale) {
  518. if (this.locales[locale]) {
  519. return this.locales[locale];
  520. }
  521. console.warn("The requested locale '" + locale + "' could not be found. Using the default locale instead.");
  522. return en;
  523. };
  524. UmAlQuraStatic._getDatePart = function (date, part) {
  525. var _a = this.gregorianToHijri(date), hy = _a.hy, hm = _a.hm, hd = _a.hd;
  526. switch (part) {
  527. case DatePart$1.Year:
  528. return hy;
  529. case DatePart$1.Month:
  530. return hm;
  531. case DatePart$1.Day:
  532. return hd;
  533. case DatePart$1.DayOfYear:
  534. return Math.trunc(this._getAbsoluteDateUmAlQura(hy, hm, hd) - this._getAbsoluteDateUmAlQura(hy, 1, 1) + 1);
  535. }
  536. };
  537. UmAlQuraStatic._setTime = function (date, hour, minute, second, millisecond) {
  538. date.setHours(hour);
  539. date.setMinutes(minute);
  540. date.setSeconds(second);
  541. date.setMilliseconds(millisecond);
  542. return date;
  543. };
  544. UmAlQuraStatic._getAbsoluteDateUmAlQura = function (hy, hm, hd) {
  545. var _a = this.hijriToGregorian(hy, hm, hd), gy = _a.gy, gm = _a.gm, gd = _a.gd;
  546. return new Date(gy, gm, gd).getTime() / this.millisPerDay;
  547. };
  548. UmAlQuraStatic._checkYearRange = function (hy) {
  549. if (hy < this.minCalendarYear || hy > this.maxCalendarYear) {
  550. throw new Error("Invalid value for year. Must be between " + this.minCalendarYear + " and " + this.maxCalendarYear + ".");
  551. }
  552. };
  553. UmAlQuraStatic._checkMonthRange = function (hm) {
  554. if (hm < 1 || hm > 12) {
  555. throw new Error("Invalid value for month. Must be between 1 and 12.");
  556. }
  557. };
  558. UmAlQuraStatic._checkDayRange = function (day) {
  559. if (day < 1 || day > 30) {
  560. throw new Error("Invalid value for day. Must be between 1 and 30.");
  561. }
  562. };
  563. UmAlQuraStatic._checkMillsRange = function (millis) {
  564. if (millis < this.minDate.getTime() || millis > this.maxDate.getTime()) {
  565. throw new Error("Invalid value for epoch. Must be between " + this.minDate.getTime() + " and " + this.maxDate.getTime() + ".");
  566. }
  567. };
  568. UmAlQuraStatic._dayDiff = function (date, other) {
  569. return (date.getTime() - other.getTime()) / (1000 * 60 * 60 * 24);
  570. };
  571. UmAlQuraStatic._initDateMapping = function () {
  572. var rawData = [
  573. // This data is auto generated from the .net BCL which seemed the most accurate
  574. // Other source which have been found having abnormalities include:
  575. // http://www.staff.science.uu.nl/~gent0113/islam/addfiles/islamcalendar_dat.js - Has 28 days in one of the month which is impossible
  576. // http://www.ummulqura.org.sa/ - Has several inaccurate dates
  577. // Generated by /personal-proj/umalqura/data-gen
  578. /* DaysPerM GY GM GD D1 D2 D3 D4 D5 D6 D7 D8 D9 D10 D11 D12
  579. 1318*/ 0x02EA, 1900, 4, 30,
  580. 0x06E9, 1901, 4, 19,
  581. 0x0ED2, 1902, 4, 9,
  582. 0x0EA4, 1903, 3, 30,
  583. 0x0D4A, 1904, 3, 18,
  584. 0x0A96, 1905, 3, 7,
  585. 0x0536, 1906, 2, 24,
  586. 0x0AB5, 1907, 2, 13,
  587. 0x0DAA, 1908, 2, 3,
  588. 0x0BA4, 1909, 1, 23,
  589. 0x0B49, 1910, 1, 12,
  590. 0x0A93, 1911, 1, 1,
  591. 0x052B, 1911, 12, 21,
  592. 0x0A57, 1912, 12, 9,
  593. 0x04B6, 1913, 11, 29,
  594. 0x0AB5, 1914, 11, 18,
  595. 0x05AA, 1915, 11, 8,
  596. 0x0D55, 1916, 10, 27,
  597. 0x0D2A, 1917, 10, 17,
  598. 0x0A56, 1918, 10, 6,
  599. 0x04AE, 1919, 9, 25,
  600. 0x095D, 1920, 9, 13,
  601. 0x02EC, 1921, 9, 3,
  602. 0x06D5, 1922, 8, 23,
  603. 0x06AA, 1923, 8, 13,
  604. 0x0555, 1924, 8, 1,
  605. 0x04AB, 1925, 7, 21,
  606. 0x095B, 1926, 7, 10,
  607. 0x02BA, 1927, 6, 30,
  608. 0x0575, 1928, 6, 18,
  609. 0x0BB2, 1929, 6, 8,
  610. 0x0764, 1930, 5, 29,
  611. 0x0749, 1931, 5, 18,
  612. 0x0655, 1932, 5, 6,
  613. 0x02AB, 1933, 4, 25,
  614. 0x055B, 1934, 4, 14,
  615. 0x0ADA, 1935, 4, 4,
  616. 0x06D4, 1936, 3, 24,
  617. 0x0EC9, 1937, 3, 13,
  618. 0x0D92, 1938, 3, 3,
  619. 0x0D25, 1939, 2, 20,
  620. 0x0A4D, 1940, 2, 9,
  621. 0x02AD, 1941, 1, 28,
  622. 0x056D, 1942, 1, 17,
  623. 0x0B6A, 1943, 1, 7,
  624. 0x0B52, 1943, 12, 28,
  625. 0x0AA5, 1944, 12, 16,
  626. 0x0A4B, 1945, 12, 5,
  627. 0x0497, 1946, 11, 24,
  628. 0x0937, 1947, 11, 13,
  629. 0x02B6, 1948, 11, 2,
  630. 0x0575, 1949, 10, 22,
  631. 0x0D6A, 1950, 10, 12,
  632. 0x0D52, 1951, 10, 2,
  633. 0x0A96, 1952, 9, 20,
  634. 0x092D, 1953, 9, 9,
  635. 0x025D, 1954, 8, 29,
  636. 0x04DD, 1955, 8, 18,
  637. 0x0ADA, 1956, 8, 7,
  638. 0x05D4, 1957, 7, 28,
  639. 0x0DA9, 1958, 7, 17,
  640. 0x0D52, 1959, 7, 7,
  641. 0x0AAA, 1960, 6, 25,
  642. 0x04D6, 1961, 6, 14,
  643. 0x09B6, 1962, 6, 3,
  644. 0x0374, 1963, 5, 24,
  645. 0x0769, 1964, 5, 12,
  646. 0x0752, 1965, 5, 2,
  647. 0x06A5, 1966, 4, 21,
  648. 0x054B, 1967, 4, 10,
  649. 0x0AAB, 1968, 3, 29,
  650. 0x055A, 1969, 3, 19,
  651. 0x0AD5, 1970, 3, 8,
  652. 0x0DD2, 1971, 2, 26,
  653. 0x0DA4, 1972, 2, 16,
  654. 0x0D49, 1973, 2, 4,
  655. 0x0A95, 1974, 1, 24,
  656. 0x052D, 1975, 1, 13,
  657. 0x0A5D, 1976, 1, 2,
  658. 0x055A, 1976, 12, 22,
  659. 0x0AD5, 1977, 12, 11,
  660. 0x06AA, 1978, 12, 1,
  661. 0x0695, 1979, 11, 20,
  662. 0x052B, 1980, 11, 8,
  663. 0x0A57, 1981, 10, 28,
  664. 0x04AE, 1982, 10, 18,
  665. 0x0976, 1983, 10, 7,
  666. 0x056C, 1984, 9, 26,
  667. 0x0B55, 1985, 9, 15,
  668. 0x0AAA, 1986, 9, 5,
  669. 0x0A55, 1987, 8, 25,
  670. 0x04AD, 1988, 8, 13,
  671. 0x095D, 1989, 8, 2,
  672. 0x02DA, 1990, 7, 23,
  673. 0x05D9, 1991, 7, 12,
  674. 0x0DB2, 1992, 7, 1,
  675. 0x0BA4, 1993, 6, 21,
  676. 0x0B4A, 1994, 6, 10,
  677. 0x0A55, 1995, 5, 30,
  678. 0x02B5, 1996, 5, 18,
  679. 0x0575, 1997, 5, 7,
  680. 0x0B6A, 1998, 4, 27,
  681. 0x0BD2, 1999, 4, 17,
  682. 0x0BC4, 2000, 4, 6,
  683. 0x0B89, 2001, 3, 26,
  684. 0x0A95, 2002, 3, 15,
  685. 0x052D, 2003, 3, 4,
  686. 0x05AD, 2004, 2, 21,
  687. 0x0B6A, 2005, 2, 10,
  688. 0x06D4, 2006, 1, 31,
  689. 0x0DC9, 2007, 1, 20,
  690. 0x0D92, 2008, 1, 10,
  691. 0x0AA6, 2008, 12, 29,
  692. 0x0956, 2009, 12, 18,
  693. 0x02AE, 2010, 12, 7,
  694. 0x056D, 2011, 11, 26,
  695. 0x036A, 2012, 11, 15,
  696. 0x0B55, 2013, 11, 4,
  697. 0x0AAA, 2014, 10, 25,
  698. 0x094D, 2015, 10, 14,
  699. 0x049D, 2016, 10, 2,
  700. 0x095D, 2017, 9, 21,
  701. 0x02BA, 2018, 9, 11,
  702. 0x05B5, 2019, 8, 31,
  703. 0x05AA, 2020, 8, 20,
  704. 0x0D55, 2021, 8, 9,
  705. 0x0A9A, 2022, 7, 30,
  706. 0x092E, 2023, 7, 19,
  707. 0x026E, 2024, 7, 7,
  708. 0x055D, 2025, 6, 26,
  709. 0x0ADA, 2026, 6, 16,
  710. 0x06D4, 2027, 6, 6,
  711. 0x06A5, 2028, 5, 25,
  712. 0x054B, 2029, 5, 14,
  713. 0x0A97, 2030, 5, 3,
  714. 0x054E, 2031, 4, 23,
  715. 0x0AAE, 2032, 4, 11,
  716. 0x05AC, 2033, 4, 1,
  717. 0x0BA9, 2034, 3, 21,
  718. 0x0D92, 2035, 3, 11,
  719. 0x0B25, 2036, 2, 28,
  720. 0x064B, 2037, 2, 16,
  721. 0x0CAB, 2038, 2, 5,
  722. 0x055A, 2039, 1, 26,
  723. 0x0B55, 2040, 1, 15,
  724. 0x06D2, 2041, 1, 4,
  725. 0x0EA5, 2041, 12, 24,
  726. 0x0E4A, 2042, 12, 14,
  727. 0x0A95, 2043, 12, 3,
  728. 0x052D, 2044, 11, 21,
  729. 0x0AAD, 2045, 11, 10,
  730. 0x036C, 2046, 10, 31,
  731. 0x0759, 2047, 10, 20,
  732. 0x06D2, 2048, 10, 9,
  733. 0x0695, 2049, 9, 28,
  734. 0x052D, 2050, 9, 17,
  735. 0x0A5B, 2051, 9, 6,
  736. 0x04BA, 2052, 8, 26,
  737. 0x09BA, 2053, 8, 15,
  738. 0x03B4, 2054, 8, 5,
  739. 0x0B69, 2055, 7, 25,
  740. 0x0B52, 2056, 7, 14,
  741. 0x0AA6, 2057, 7, 3,
  742. 0x04B6, 2058, 6, 22,
  743. 0x096D, 2059, 6, 11,
  744. 0x02EC, 2060, 5, 31,
  745. 0x06D9, 2061, 5, 20,
  746. 0x0EB2, 2062, 5, 10,
  747. 0x0D54, 2063, 4, 30,
  748. 0x0D2A, 2064, 4, 18,
  749. 0x0A56, 2065, 4, 7,
  750. 0x04AE, 2066, 3, 27,
  751. 0x096D, 2067, 3, 16,
  752. 0x0D6A, 2068, 3, 5,
  753. 0x0B54, 2069, 2, 23,
  754. 0x0B29, 2070, 2, 12,
  755. 0x0A93, 2071, 2, 1,
  756. 0x052B, 2072, 1, 21,
  757. 0x0A57, 2073, 1, 9,
  758. 0x0536, 2073, 12, 30,
  759. 0x0AB5, 2074, 12, 19,
  760. 0x06AA, 2075, 12, 9,
  761. 0x0E93, 2076, 11, 27,
  762. 0, 2077, 11, 17 /* 0 0 0 0 0 0 0 0 0 0 0 0 2077-11-17*/
  763. ];
  764. var mapping = [];
  765. for (var i = 0; i < rawData.length / 4; i++) {
  766. mapping.push(new DateMapping(rawData[i * 4], rawData[i * 4 + 1], rawData[i * 4 + 2] - 1, rawData[i * 4 + 3]));
  767. }
  768. return mapping;
  769. };
  770. // private static readonly maxSeconds = 9223372036854775807 / 10000000;
  771. // private static readonly minSeconds = -9223372036854775807 / 10000000;
  772. UmAlQuraStatic.millisPerSecond = 1000;
  773. UmAlQuraStatic.millisPerMinute = UmAlQuraStatic.millisPerSecond * 60;
  774. UmAlQuraStatic.millisPerHour = UmAlQuraStatic.millisPerMinute * 60;
  775. UmAlQuraStatic.millisPerDay = UmAlQuraStatic.millisPerHour * 24;
  776. UmAlQuraStatic.minDate = new Date(1900, 3, 30);
  777. UmAlQuraStatic.maxDate = new Date(2077, 10, 16, 23, 59, 59, 999);
  778. UmAlQuraStatic.hijriYearData = UmAlQuraStatic._initDateMapping();
  779. // Holds globally set locale
  780. UmAlQuraStatic.locale = en;
  781. // Holds registered locales
  782. UmAlQuraStatic.locales = {};
  783. UmAlQuraStatic.minCalendarYear = 1318;
  784. UmAlQuraStatic.maxCalendarYear = 1500;
  785. return UmAlQuraStatic;
  786. }());
  787. // Register both locales so they'll be bundled in the package.
  788. // This is fine since for this library, there will probably be
  789. // only be these two locales.
  790. UmAlQuraStatic.registerLocale(ar);
  791. UmAlQuraStatic.registerLocale(en);
  792. var UmAlQura = /** @class */ (function () {
  793. function UmAlQura(dateOrHy, hm, hd, hour, minute, second, millisecond) {
  794. if (hour === void 0) { hour = 0; }
  795. if (minute === void 0) { minute = 0; }
  796. if (second === void 0) { second = 0; }
  797. if (millisecond === void 0) { millisecond = 0; }
  798. this._date = new Date(0, 0, 0);
  799. this._hy = 0;
  800. this._hm = 0;
  801. this._hd = 0;
  802. if (dateOrHy instanceof Date) {
  803. this._setDate(dateOrHy);
  804. }
  805. else if (dateOrHy !== undefined && hm !== undefined && hd !== undefined) {
  806. var _a = UmAlQuraStatic.hijriToGregorian(dateOrHy, hm, hd), gy = _a.gy, gm = _a.gm, gd = _a.gd;
  807. this._setDate(new Date(gy, gm, gd, hour, minute, second, millisecond));
  808. }
  809. else {
  810. this._setDate(new Date());
  811. }
  812. }
  813. Object.defineProperty(UmAlQura.prototype, "date", {
  814. /**
  815. * Returns the `Date` object of this instance.
  816. */
  817. get: function () { return new Date(this._date.valueOf()); },
  818. enumerable: true,
  819. configurable: true
  820. });
  821. Object.defineProperty(UmAlQura.prototype, "hy", {
  822. /**
  823. * Returns the Hijri year of this instance.
  824. */
  825. get: function () { return this._hy; },
  826. enumerable: true,
  827. configurable: true
  828. });
  829. Object.defineProperty(UmAlQura.prototype, "hm", {
  830. /**
  831. * Returns the Hijri month of this instance.
  832. */
  833. get: function () { return this._hm; },
  834. enumerable: true,
  835. configurable: true
  836. });
  837. Object.defineProperty(UmAlQura.prototype, "hd", {
  838. /**
  839. * Returns the Hijri day of month of this instance.
  840. */
  841. get: function () { return this._hd; },
  842. enumerable: true,
  843. configurable: true
  844. });
  845. Object.defineProperty(UmAlQura.prototype, "dayOfYear", {
  846. /**
  847. * Returns the Hijri day of year of this instance.
  848. */
  849. get: function () { return UmAlQuraStatic.getDayOfYear(this.date); },
  850. enumerable: true,
  851. configurable: true
  852. });
  853. Object.defineProperty(UmAlQura.prototype, "dayOfWeek", {
  854. /**
  855. * Returns the day of week of this instance.
  856. */
  857. get: function () { return UmAlQuraStatic.getDayOfWeek(this.date); },
  858. enumerable: true,
  859. configurable: true
  860. });
  861. Object.defineProperty(UmAlQura.prototype, "weekOfYear", {
  862. /**
  863. * Returns the Hijri week of year of this instance.
  864. */
  865. get: function () { return UmAlQuraStatic.getWeekOfYear(this.date); },
  866. enumerable: true,
  867. configurable: true
  868. });
  869. Object.defineProperty(UmAlQura.prototype, "daysInYear", {
  870. /**
  871. * Returns the number of days in year of this instance.
  872. */
  873. get: function () { return UmAlQuraStatic.getDaysInYear(this.hy); },
  874. enumerable: true,
  875. configurable: true
  876. });
  877. Object.defineProperty(UmAlQura.prototype, "daysInMonth", {
  878. /**
  879. * Returns the number of days in month of this instance.
  880. */
  881. get: function () { return UmAlQuraStatic.getDaysInMonth(this.hy, this.hm); },
  882. enumerable: true,
  883. configurable: true
  884. });
  885. Object.defineProperty(UmAlQura.prototype, "isLeapYear", {
  886. /**
  887. * Returns whether or not the Hijri year of this instance is a leap year.
  888. */
  889. get: function () { return UmAlQuraStatic.isLeapYear(this.hy); },
  890. enumerable: true,
  891. configurable: true
  892. });
  893. Object.defineProperty(UmAlQura.prototype, "monthArray", {
  894. /**
  895. * Returns the Hijri month array of this instance.
  896. */
  897. get: function () {
  898. return UmAlQuraStatic.getMonthArray(this.date)
  899. .map(function (w) { return w.map(function (d) { return d ? new UmAlQura(d) : null; }); });
  900. },
  901. enumerable: true,
  902. configurable: true
  903. });
  904. /**
  905. * Adds the specified amount of `unit` to the current date and returns a new instance.
  906. * @param {number} value The amount of units to be added
  907. * @param {UnitOfTimeMs} unit The unit of time
  908. */
  909. UmAlQura.prototype.add = function (value, unit) {
  910. switch (unit) {
  911. case 'year':
  912. return new UmAlQura(UmAlQuraStatic.addYears(this.date, value));
  913. case 'month':
  914. return new UmAlQura(UmAlQuraStatic.addMonths(this.date, value));
  915. case 'week':
  916. return new UmAlQura(UmAlQuraStatic.addWeeks(this.date, value));
  917. case 'day':
  918. return new UmAlQura(UmAlQuraStatic.addDays(this.date, value));
  919. case 'hour':
  920. case 'minute':
  921. case 'second':
  922. case 'millisecond':
  923. return new UmAlQura(UmAlQuraStatic.addTime(this.date, value, unit));
  924. default:
  925. throw new Error('Invalid value for `unit`');
  926. }
  927. };
  928. /**
  929. * Subtracts the specified amount of `unit` from the current date and returns a new instance.
  930. * @param {number} value The amount of units to be subtracted
  931. * @param {UnitOfTimeMs} unit The unit of time
  932. */
  933. UmAlQura.prototype.subtract = function (value, unit) {
  934. return this.add(value * -1, unit);
  935. };
  936. /**
  937. * Returns a new instance having the Hijri date of this instance starting at the specified unit of time.
  938. * @param {UnitOfTime} unit The unit of time
  939. */
  940. UmAlQura.prototype.startOf = function (unit) {
  941. return new UmAlQura(UmAlQuraStatic.startOf(this.date, unit));
  942. };
  943. /**
  944. * Returns a new instance having the Hijri date of this instance ending at the specified unit of time.
  945. * @param {UnitOfTime} unit The unit of time
  946. */
  947. UmAlQura.prototype.endOf = function (unit) {
  948. return new UmAlQura(UmAlQuraStatic.endOf(this.date, unit));
  949. };
  950. /**
  951. * Checks if current date is before the specified date. The comparison is made based on milliseconds of both
  952. * times. This can be changed by specifying a value for the `unit` parameter.
  953. * @param {(UmAlQura | Date)} other The date to compare against
  954. * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
  955. */
  956. UmAlQura.prototype.isBefore = function (other, unit) {
  957. if (unit === void 0) { unit = 'millisecond'; }
  958. var thisDate = this.date;
  959. var thatDate = other instanceof Date ? other : other.date;
  960. if (unit === 'millisecond') {
  961. return thisDate.valueOf() < thatDate.valueOf();
  962. }
  963. else {
  964. return this.endOf(unit).date.valueOf() < thatDate.valueOf();
  965. }
  966. };
  967. /**
  968. * Checks if current date is after the specified date. The comparison is made based on milliseconds,
  969. * this can be changed by specifying a value for the `unit` parameter.
  970. * @param {(UmAlQura | Date)} other The date to compare against
  971. * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
  972. */
  973. UmAlQura.prototype.isAfter = function (other, unit) {
  974. if (unit === void 0) { unit = 'millisecond'; }
  975. var thisDate = this.date;
  976. var thatDate = other instanceof Date ? other : other.date;
  977. if (unit === 'millisecond') {
  978. return thisDate.valueOf() > thatDate.valueOf();
  979. }
  980. else {
  981. return this.startOf(unit).date.valueOf() > thatDate.valueOf();
  982. }
  983. };
  984. /**
  985. * Checks if current date is same as the specified date. The comparison is made based on milliseconds,
  986. * this can be changed by specifying a value for the `unit` parameter.
  987. * @param {(UmAlQura | Date)} other The date to compare against
  988. * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
  989. */
  990. UmAlQura.prototype.isSame = function (other, unit) {
  991. if (unit === void 0) { unit = 'millisecond'; }
  992. var thisDate = this.date;
  993. var thatDate = other instanceof Date ? other : other.date;
  994. if (unit === 'millisecond') {
  995. return thisDate.valueOf() === thatDate.valueOf();
  996. }
  997. else {
  998. return this.startOf(unit).date.valueOf() === new UmAlQura(thatDate).startOf(unit).date.valueOf();
  999. }
  1000. };
  1001. /**
  1002. * Checks if current date is same as or before the specified date. The comparison is made based on milliseconds,
  1003. * this can be changed by specifying a value for the `unit` parameter.
  1004. * @param {(UmAlQura | Date)} other The date to compare against
  1005. * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
  1006. */
  1007. UmAlQura.prototype.isSameOrBefore = function (other, unit) {
  1008. if (unit === void 0) { unit = 'millisecond'; }
  1009. return this.isSame(other, unit) || this.isBefore(other, unit);
  1010. };
  1011. /**
  1012. * Checks if current date is same as or after the specified date. The comparison is made based on milliseconds,
  1013. * this can be changed by specifying a value for the `unit` parameter.
  1014. * @param {(UmAlQura | Date)} other The date to compare against
  1015. * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
  1016. */
  1017. UmAlQura.prototype.isSameOrAfter = function (other, unit) {
  1018. if (unit === void 0) { unit = 'millisecond'; }
  1019. return this.isSame(other, unit) || this.isAfter(other, unit);
  1020. };
  1021. /**
  1022. * Checks if current date is between the specified `from`/`to` dates. The comparison is made based on milliseconds,
  1023. * this can be changed by specifying a value for the `unit` parameter. The comparison is exclusive of both ends by default,
  1024. * this can be controller by `fromInclusive`/`toInclusive` parameters.
  1025. * @param {(UmAlQura | Date)} from The lower bound date
  1026. * @param {(UmAlQura | Date)} to The higher bound date
  1027. * @param {boolean} [fromInclusive=false] Whether lower bound is inclusive, defaults to false.
  1028. * @param {boolean} [toInclusive=false] Whether upper bound is inclusive, defaults to false.
  1029. * @param {UnitOfTimeMs} [unit='millisecond'] The unit of time
  1030. */
  1031. UmAlQura.prototype.isBetween = function (from, to, fromInclusive, toInclusive, unit) {
  1032. if (fromInclusive === void 0) { fromInclusive = false; }
  1033. if (toInclusive === void 0) { toInclusive = false; }
  1034. if (unit === void 0) { unit = 'millisecond'; }
  1035. return (fromInclusive ? this.isSameOrAfter(from, unit) : this.isAfter(from, unit)) &&
  1036. (toInclusive ? this.isSameOrBefore(to, unit) : this.isBefore(to, unit));
  1037. };
  1038. /**
  1039. * Formats this instance in Hijri date.
  1040. * @param {string} mask The mask
  1041. * @param {string} locale The locale to use. If omitted, uses the locale set via `locale` or the default locale.
  1042. */
  1043. UmAlQura.prototype.format = function (mask, locale) {
  1044. // tslint:disable-next-line:no-string-literal
  1045. return UmAlQuraStatic.format(this.date, mask, locale || UmAlQuraStatic['locale'].name);
  1046. };
  1047. /**
  1048. * Clones this instance and returns a new instance with the same values.
  1049. */
  1050. UmAlQura.prototype.clone = function () {
  1051. return new UmAlQura(this.date);
  1052. };
  1053. UmAlQura.prototype._setDate = function (date) {
  1054. var _a = UmAlQuraStatic.gregorianToHijri(date), hy = _a.hy, hm = _a.hm, hd = _a.hd;
  1055. this._date = new Date(date.valueOf());
  1056. this._hy = hy;
  1057. this._hm = hm;
  1058. this._hd = hd;
  1059. };
  1060. return UmAlQura;
  1061. }());
  1062. function umalqura(dateOrHy, hm, hd, hour, minute, second, millisecond) {
  1063. if (hour === void 0) { hour = 0; }
  1064. if (minute === void 0) { minute = 0; }
  1065. if (second === void 0) { second = 0; }
  1066. if (millisecond === void 0) { millisecond = 0; }
  1067. if (dateOrHy instanceof Date) {
  1068. return new UmAlQura(dateOrHy);
  1069. }
  1070. else if (dateOrHy !== undefined && hm !== undefined && hd !== undefined) {
  1071. return new UmAlQura(dateOrHy, hm, hd, hour, minute, second, millisecond);
  1072. }
  1073. else {
  1074. return new UmAlQura();
  1075. }
  1076. }
  1077. /**
  1078. * Returns the library version.
  1079. */
  1080. umalqura.VERSION = "0.0.7";
  1081. /**
  1082. * Returns a class which exposes static Hijri related functions.
  1083. */
  1084. umalqura.$ = UmAlQuraStatic;
  1085. /**
  1086. * Returns the minimum supported Hijri date.
  1087. */
  1088. umalqura.min = umalqura(UmAlQuraStatic['minCalendarYear'], 1, 1);
  1089. /**
  1090. * Returns the maximum supported Hijri date.
  1091. */
  1092. umalqura.max = umalqura(UmAlQuraStatic['maxCalendarYear'], 1, 1).endOf('year');
  1093. /**
  1094. * Gets or sets the global locale
  1095. * @param locale The locale to set. If omitted, returns the current locale
  1096. */
  1097. umalqura.locale = function (locale) { return locale ? UmAlQuraStatic.setLocale(locale) : UmAlQuraStatic['locale'].name; };
  1098. /**
  1099. * Returns whether the currently set locale is RTL or not.
  1100. */
  1101. umalqura.rtl = function () { return !!UmAlQuraStatic['locale'].rtl; };
  1102. /**
  1103. * Returns the times names using the currently set locale.
  1104. */
  1105. umalqura.times = function () { return UmAlQuraStatic['locale'].timeNames.slice(); };
  1106. /**
  1107. * Returns the days names using the currently set locale.
  1108. */
  1109. umalqura.days = function () { return UmAlQuraStatic['locale'].dayNames.slice(); };
  1110. /**
  1111. * Returns the days short names using the currently set locale.
  1112. */
  1113. umalqura.daysShort = function () { return UmAlQuraStatic['locale'].dayNamesShort.slice(); };
  1114. /**
  1115. * Returns the months names using the currently set locale.
  1116. */
  1117. umalqura.months = function () { return UmAlQuraStatic['locale'].monthNames.slice(); };
  1118. /**
  1119. * Returns the months short names using the currently set locale.
  1120. */
  1121. umalqura.monthsShort = function () { return UmAlQuraStatic['locale'].monthNamesShort.slice(); };
  1122. /**
  1123. * Returns the localized number for the given number using the currently set locale.
  1124. */
  1125. umalqura.localizeNum = function (num) { return UmAlQuraStatic['locale'].localizeNum(num); };
  1126. /**
  1127. * Returns the localized day number for the given day number using the currently set locale.
  1128. */
  1129. umalqura.localizeDayNum = function (d) { return UmAlQuraStatic['locale'].localizeDayNum(d); };
  1130. exports.default = umalqura;