moment-jalali.min.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. ;(function(){
  2. /**
  3. * Require the module at `name`.
  4. *
  5. * @param {String} name
  6. * @return {Object} exports
  7. * @api public
  8. */
  9. function require(name) {
  10. var module = require.modules[name];
  11. if (!module) throw new Error('failed to require "' + name + '"');
  12. if (!('exports' in module) && typeof module.definition === 'function') {
  13. module.client = module.component = true;
  14. module.definition.call(this, module.exports = {}, module);
  15. delete module.definition;
  16. }
  17. return module.exports;
  18. }
  19. /**
  20. * Registered modules.
  21. */
  22. require.modules = {
  23. moment: { exports: moment }
  24. };
  25. /**
  26. * Register module at `name` with callback `definition`.
  27. *
  28. * @param {String} name
  29. * @param {Function} definition
  30. * @api private
  31. */
  32. require.register = function (name, definition) {
  33. require.modules[name] = {
  34. definition: definition
  35. };
  36. };
  37. /**
  38. * Define a module's exports immediately with `exports`.
  39. *
  40. * @param {String} name
  41. * @param {Generic} exports
  42. * @api private
  43. */
  44. require.define = function (name, exports) {
  45. require.modules[name] = {
  46. exports: exports
  47. };
  48. };
  49. require.register("jalaali-js", function (exports, module) {
  50. /*
  51. Expose functions.
  52. */
  53. module.exports =
  54. { toJalaali: toJalaali
  55. , toGregorian: toGregorian
  56. , isValidJalaaliDate: isValidJalaaliDate
  57. , isLeapJalaaliYear: isLeapJalaaliYear
  58. , jalaaliMonthLength: jalaaliMonthLength
  59. , jalCal: jalCal
  60. , j2d: j2d
  61. , d2j: d2j
  62. , g2d: g2d
  63. , d2g: d2g
  64. }
  65. /*
  66. Converts a Gregorian date to Jalaali.
  67. */
  68. function toJalaali(gy, gm, gd) {
  69. if (Object.prototype.toString.call(gy) === '[object Date]') {
  70. gd = gy.getDate()
  71. gm = gy.getMonth() + 1
  72. gy = gy.getFullYear()
  73. }
  74. return d2j(g2d(gy, gm, gd))
  75. }
  76. /*
  77. Converts a Jalaali date to Gregorian.
  78. */
  79. function toGregorian(jy, jm, jd) {
  80. return d2g(j2d(jy, jm, jd))
  81. }
  82. /*
  83. Checks whether a Jalaali date is valid or not.
  84. */
  85. function isValidJalaaliDate(jy, jm, jd) {
  86. return jy >= -61 && jy <= 3177 &&
  87. jm >= 1 && jm <= 12 &&
  88. jd >= 1 && jd <= jalaaliMonthLength(jy, jm)
  89. }
  90. /*
  91. Is this a leap year or not?
  92. */
  93. function isLeapJalaaliYear(jy) {
  94. return jalCal(jy).leap === 0
  95. }
  96. /*
  97. Number of days in a given month in a Jalaali year.
  98. */
  99. function jalaaliMonthLength(jy, jm) {
  100. if (jm <= 6) return 31
  101. if (jm <= 11) return 30
  102. if (isLeapJalaaliYear(jy)) return 30
  103. return 29
  104. }
  105. /*
  106. This function determines if the Jalaali (Persian) year is
  107. leap (366-day long) or is the common year (365 days), and
  108. finds the day in March (Gregorian calendar) of the first
  109. day of the Jalaali year (jy).
  110. @param jy Jalaali calendar year (-61 to 3177)
  111. @return
  112. leap: number of years since the last leap year (0 to 4)
  113. gy: Gregorian year of the beginning of Jalaali year
  114. march: the March day of Farvardin the 1st (1st day of jy)
  115. @see: http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm
  116. @see: http://www.fourmilab.ch/documents/calendar/
  117. */
  118. function jalCal(jy) {
  119. // Jalaali years starting the 33-year rule.
  120. var breaks = [ -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210
  121. , 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
  122. ]
  123. , bl = breaks.length
  124. , gy = jy + 621
  125. , leapJ = -14
  126. , jp = breaks[0]
  127. , jm
  128. , jump
  129. , leap
  130. , leapG
  131. , march
  132. , n
  133. , i
  134. if (jy < jp || jy >= breaks[bl - 1])
  135. throw new Error('Invalid Jalaali year ' + jy)
  136. // Find the limiting years for the Jalaali year jy.
  137. for (i = 1; i < bl; i += 1) {
  138. jm = breaks[i]
  139. jump = jm - jp
  140. if (jy < jm)
  141. break
  142. leapJ = leapJ + div(jump, 33) * 8 + div(mod(jump, 33), 4)
  143. jp = jm
  144. }
  145. n = jy - jp
  146. // Find the number of leap years from AD 621 to the beginning
  147. // of the current Jalaali year in the Persian calendar.
  148. leapJ = leapJ + div(n, 33) * 8 + div(mod(n, 33) + 3, 4)
  149. if (mod(jump, 33) === 4 && jump - n === 4)
  150. leapJ += 1
  151. // And the same in the Gregorian calendar (until the year gy).
  152. leapG = div(gy, 4) - div((div(gy, 100) + 1) * 3, 4) - 150
  153. // Determine the Gregorian date of Farvardin the 1st.
  154. march = 20 + leapJ - leapG
  155. // Find how many years have passed since the last leap year.
  156. if (jump - n < 6)
  157. n = n - jump + div(jump + 4, 33) * 33
  158. leap = mod(mod(n + 1, 33) - 1, 4)
  159. if (leap === -1) {
  160. leap = 4
  161. }
  162. return { leap: leap
  163. , gy: gy
  164. , march: march
  165. }
  166. }
  167. /*
  168. Converts a date of the Jalaali calendar to the Julian Day number.
  169. @param jy Jalaali year (1 to 3100)
  170. @param jm Jalaali month (1 to 12)
  171. @param jd Jalaali day (1 to 29/31)
  172. @return Julian Day number
  173. */
  174. function j2d(jy, jm, jd) {
  175. var r = jalCal(jy)
  176. return g2d(r.gy, 3, r.march) + (jm - 1) * 31 - div(jm, 7) * (jm - 7) + jd - 1
  177. }
  178. /*
  179. Converts the Julian Day number to a date in the Jalaali calendar.
  180. @param jdn Julian Day number
  181. @return
  182. jy: Jalaali year (1 to 3100)
  183. jm: Jalaali month (1 to 12)
  184. jd: Jalaali day (1 to 29/31)
  185. */
  186. function d2j(jdn) {
  187. var gy = d2g(jdn).gy // Calculate Gregorian year (gy).
  188. , jy = gy - 621
  189. , r = jalCal(jy)
  190. , jdn1f = g2d(gy, 3, r.march)
  191. , jd
  192. , jm
  193. , k
  194. // Find number of days that passed since 1 Farvardin.
  195. k = jdn - jdn1f
  196. if (k >= 0) {
  197. if (k <= 185) {
  198. // The first 6 months.
  199. jm = 1 + div(k, 31)
  200. jd = mod(k, 31) + 1
  201. return { jy: jy
  202. , jm: jm
  203. , jd: jd
  204. }
  205. } else {
  206. // The remaining months.
  207. k -= 186
  208. }
  209. } else {
  210. // Previous Jalaali year.
  211. jy -= 1
  212. k += 179
  213. if (r.leap === 1)
  214. k += 1
  215. }
  216. jm = 7 + div(k, 30)
  217. jd = mod(k, 30) + 1
  218. return { jy: jy
  219. , jm: jm
  220. , jd: jd
  221. }
  222. }
  223. /*
  224. Calculates the Julian Day number from Gregorian or Julian
  225. calendar dates. This integer number corresponds to the noon of
  226. the date (i.e. 12 hours of Universal Time).
  227. The procedure was tested to be good since 1 March, -100100 (of both
  228. calendars) up to a few million years into the future.
  229. @param gy Calendar year (years BC numbered 0, -1, -2, ...)
  230. @param gm Calendar month (1 to 12)
  231. @param gd Calendar day of the month (1 to 28/29/30/31)
  232. @return Julian Day number
  233. */
  234. function g2d(gy, gm, gd) {
  235. var d = div((gy + div(gm - 8, 6) + 100100) * 1461, 4)
  236. + div(153 * mod(gm + 9, 12) + 2, 5)
  237. + gd - 34840408
  238. d = d - div(div(gy + 100100 + div(gm - 8, 6), 100) * 3, 4) + 752
  239. return d
  240. }
  241. /*
  242. Calculates Gregorian and Julian calendar dates from the Julian Day number
  243. (jdn) for the period since jdn=-34839655 (i.e. the year -100100 of both
  244. calendars) to some millions years ahead of the present.
  245. @param jdn Julian Day number
  246. @return
  247. gy: Calendar year (years BC numbered 0, -1, -2, ...)
  248. gm: Calendar month (1 to 12)
  249. gd: Calendar day of the month M (1 to 28/29/30/31)
  250. */
  251. function d2g(jdn) {
  252. var j
  253. , i
  254. , gd
  255. , gm
  256. , gy
  257. j = 4 * jdn + 139361631
  258. j = j + div(div(4 * jdn + 183187720, 146097) * 3, 4) * 4 - 3908
  259. i = div(mod(j, 1461), 4) * 5 + 308
  260. gd = div(mod(i, 153), 5) + 1
  261. gm = mod(div(i, 153), 12) + 1
  262. gy = div(j, 1461) - 100100 + div(8 - gm, 6)
  263. return { gy: gy
  264. , gm: gm
  265. , gd: gd
  266. }
  267. }
  268. /*
  269. Utility helper functions.
  270. */
  271. function div(a, b) {
  272. return ~~(a / b)
  273. }
  274. function mod(a, b) {
  275. return a - ~~(a / b) * b
  276. }
  277. })
  278. require.register("moment-jalaali", function (exports, module) {
  279. module.exports = jMoment
  280. var moment = require('moment')
  281. , jalaali = require('jalaali-js')
  282. /************************************
  283. Constants
  284. ************************************/
  285. var formattingTokens = /(\[[^\[]*\])|(\\)?j(Mo|MM?M?M?|Do|DDDo|DD?D?D?|w[o|w]?|YYYYY|YYYY|YY|gg(ggg?)?|)|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/g
  286. , localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS?|LL?L?L?|l{1,4})/g
  287. , parseTokenOneOrTwoDigits = /\d\d?/
  288. , parseTokenOneToThreeDigits = /\d{1,3}/
  289. , parseTokenThreeDigits = /\d{3}/
  290. , parseTokenFourDigits = /\d{1,4}/
  291. , parseTokenSixDigits = /[+\-]?\d{1,6}/
  292. , parseTokenWord = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i
  293. , parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i
  294. , parseTokenT = /T/i
  295. , parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/
  296. , symbolMap = {
  297. '1': '۱',
  298. '2': '۲',
  299. '3': '۳',
  300. '4': '۴',
  301. '5': '۵',
  302. '6': '۶',
  303. '7': '۷',
  304. '8': '۸',
  305. '9': '۹',
  306. '0': '۰'
  307. }
  308. , numberMap = {
  309. '۱': '1',
  310. '۲': '2',
  311. '۳': '3',
  312. '۴': '4',
  313. '۵': '5',
  314. '۶': '6',
  315. '۷': '7',
  316. '۸': '8',
  317. '۹': '9',
  318. '۰': '0'
  319. }
  320. , unitAliases =
  321. { jm: 'jmonth'
  322. , jmonths: 'jmonth'
  323. , jy: 'jyear'
  324. , jyears: 'jyear'
  325. }
  326. , formatFunctions = {}
  327. , ordinalizeTokens = 'DDD w M D'.split(' ')
  328. , paddedTokens = 'M D w'.split(' ')
  329. , formatTokenFunctions =
  330. { jM: function () {
  331. return this.jMonth() + 1
  332. }
  333. , jMMM: function (format) {
  334. return this.localeData().jMonthsShort(this, format)
  335. }
  336. , jMMMM: function (format) {
  337. return this.localeData().jMonths(this, format)
  338. }
  339. , jD: function () {
  340. return this.jDate()
  341. }
  342. , jDDD: function () {
  343. return this.jDayOfYear()
  344. }
  345. , jw: function () {
  346. return this.jWeek()
  347. }
  348. , jYY: function () {
  349. return leftZeroFill(this.jYear() % 100, 2)
  350. }
  351. , jYYYY: function () {
  352. return leftZeroFill(this.jYear(), 4)
  353. }
  354. , jYYYYY: function () {
  355. return leftZeroFill(this.jYear(), 5)
  356. }
  357. , jgg: function () {
  358. return leftZeroFill(this.jWeekYear() % 100, 2)
  359. }
  360. , jgggg: function () {
  361. return this.jWeekYear()
  362. }
  363. , jggggg: function () {
  364. return leftZeroFill(this.jWeekYear(), 5)
  365. }
  366. }
  367. function padToken(func, count) {
  368. return function (a) {
  369. return leftZeroFill(func.call(this, a), count)
  370. }
  371. }
  372. function ordinalizeToken(func, period) {
  373. return function (a) {
  374. return this.localeData().ordinal(func.call(this, a), period)
  375. }
  376. }
  377. (function () {
  378. var i
  379. while (ordinalizeTokens.length) {
  380. i = ordinalizeTokens.pop()
  381. formatTokenFunctions['j' + i + 'o'] = ordinalizeToken(formatTokenFunctions['j' + i], i)
  382. }
  383. while (paddedTokens.length) {
  384. i = paddedTokens.pop()
  385. formatTokenFunctions['j' + i + i] = padToken(formatTokenFunctions['j' + i], 2)
  386. }
  387. formatTokenFunctions.jDDDD = padToken(formatTokenFunctions.jDDD, 3)
  388. }())
  389. /************************************
  390. Helpers
  391. ************************************/
  392. function extend(a, b) {
  393. var key
  394. for (key in b)
  395. if (b.hasOwnProperty(key))
  396. a[key] = b[key]
  397. return a
  398. }
  399. function leftZeroFill(number, targetLength) {
  400. var output = number + ''
  401. while (output.length < targetLength)
  402. output = '0' + output
  403. return output
  404. }
  405. function isArray(input) {
  406. return Object.prototype.toString.call(input) === '[object Array]'
  407. }
  408. // function compareArrays(array1, array2) {
  409. // var len = Math.min(array1.length, array2.length)
  410. // , lengthDiff = Math.abs(array1.length - array2.length)
  411. // , diffs = 0
  412. // , i
  413. // for (i = 0; i < len; i += 1)
  414. // if (~~array1[i] !== ~~array2[i])
  415. // diffs += 1
  416. // return diffs + lengthDiff
  417. // }
  418. function normalizeUnits(units) {
  419. if (units) {
  420. var lowered = units.toLowerCase()
  421. units = unitAliases[lowered] || lowered
  422. }
  423. return units
  424. }
  425. function setDate(m, year, month, date) {
  426. var d = m._d
  427. if (isNaN(year)) {
  428. m._isValid = false
  429. }
  430. if (m._isUTC) {
  431. /*eslint-disable new-cap*/
  432. m._d = new Date(Date.UTC(year, month, date,
  433. d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()))
  434. /*eslint-enable new-cap*/
  435. } else {
  436. m._d = new Date(year, month, date,
  437. d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds())
  438. }
  439. }
  440. function objectCreate(parent) {
  441. function F() {}
  442. F.prototype = parent
  443. return new F()
  444. }
  445. function getPrototypeOf(object) {
  446. if (Object.getPrototypeOf)
  447. return Object.getPrototypeOf(object)
  448. else if (''.__proto__)
  449. return object.__proto__
  450. else
  451. return object.constructor.prototype
  452. }
  453. /************************************
  454. Languages
  455. ************************************/
  456. extend(getPrototypeOf(moment.localeData()),
  457. { _jMonths: [ 'Farvardin'
  458. , 'Ordibehesht'
  459. , 'Khordaad'
  460. , 'Tir'
  461. , 'Amordaad'
  462. , 'Shahrivar'
  463. , 'Mehr'
  464. , 'Aabaan'
  465. , 'Aazar'
  466. , 'Dey'
  467. , 'Bahman'
  468. , 'Esfand'
  469. ]
  470. , jMonths: function (m) {
  471. return this._jMonths[m.jMonth()]
  472. }
  473. , _jMonthsShort: [ 'Far'
  474. , 'Ord'
  475. , 'Kho'
  476. , 'Tir'
  477. , 'Amo'
  478. , 'Sha'
  479. , 'Meh'
  480. , 'Aab'
  481. , 'Aaz'
  482. , 'Dey'
  483. , 'Bah'
  484. , 'Esf'
  485. ]
  486. , jMonthsShort: function (m) {
  487. return this._jMonthsShort[m.jMonth()]
  488. }
  489. , jMonthsParse: function (monthName) {
  490. var i
  491. , mom
  492. , regex
  493. if (!this._jMonthsParse)
  494. this._jMonthsParse = []
  495. for (i = 0; i < 12; i += 1) {
  496. // Make the regex if we don't have it already.
  497. if (!this._jMonthsParse[i]) {
  498. mom = jMoment([2000, (2 + i) % 12, 25])
  499. regex = '^' + this.jMonths(mom, '') + '|^' + this.jMonthsShort(mom, '')
  500. this._jMonthsParse[i] = new RegExp(regex.replace('.', ''), 'i')
  501. }
  502. // Test the regex.
  503. if (this._jMonthsParse[i].test(monthName))
  504. return i
  505. }
  506. }
  507. }
  508. )
  509. /************************************
  510. Formatting
  511. ************************************/
  512. function makeFormatFunction(format) {
  513. var array = format.match(formattingTokens)
  514. , length = array.length
  515. , i
  516. for (i = 0; i < length; i += 1)
  517. if (formatTokenFunctions[array[i]])
  518. array[i] = formatTokenFunctions[array[i]]
  519. return function (mom) {
  520. var output = ''
  521. for (i = 0; i < length; i += 1)
  522. output += array[i] instanceof Function ? '[' + array[i].call(mom, format) + ']' : array[i]
  523. return output
  524. }
  525. }
  526. /************************************
  527. Parsing
  528. ************************************/
  529. function getParseRegexForToken(token, config) {
  530. switch (token) {
  531. case 'jDDDD':
  532. return parseTokenThreeDigits
  533. case 'jYYYY':
  534. return parseTokenFourDigits
  535. case 'jYYYYY':
  536. return parseTokenSixDigits
  537. case 'jDDD':
  538. return parseTokenOneToThreeDigits
  539. case 'jMMM':
  540. case 'jMMMM':
  541. return parseTokenWord
  542. case 'jMM':
  543. case 'jDD':
  544. case 'jYY':
  545. case 'jM':
  546. case 'jD':
  547. return parseTokenOneOrTwoDigits
  548. case 'DDDD':
  549. return parseTokenThreeDigits
  550. case 'YYYY':
  551. return parseTokenFourDigits
  552. case 'YYYYY':
  553. return parseTokenSixDigits
  554. case 'S':
  555. case 'SS':
  556. case 'SSS':
  557. case 'DDD':
  558. return parseTokenOneToThreeDigits
  559. case 'MMM':
  560. case 'MMMM':
  561. case 'dd':
  562. case 'ddd':
  563. case 'dddd':
  564. return parseTokenWord
  565. case 'a':
  566. case 'A':
  567. return moment.localeData(config._l)._meridiemParse
  568. case 'X':
  569. return parseTokenTimestampMs
  570. case 'Z':
  571. case 'ZZ':
  572. return parseTokenTimezone
  573. case 'T':
  574. return parseTokenT
  575. case 'MM':
  576. case 'DD':
  577. case 'YY':
  578. case 'HH':
  579. case 'hh':
  580. case 'mm':
  581. case 'ss':
  582. case 'M':
  583. case 'D':
  584. case 'd':
  585. case 'H':
  586. case 'h':
  587. case 'm':
  588. case 's':
  589. return parseTokenOneOrTwoDigits
  590. default:
  591. return new RegExp(token.replace('\\', ''))
  592. }
  593. }
  594. function addTimeToArrayFromToken(token, input, config) {
  595. var a
  596. , datePartArray = config._a
  597. switch (token) {
  598. case 'jM':
  599. case 'jMM':
  600. datePartArray[1] = input == null ? 0 : ~~input - 1
  601. break
  602. case 'jMMM':
  603. case 'jMMMM':
  604. a = moment.localeData(config._l).jMonthsParse(input)
  605. if (a != null)
  606. datePartArray[1] = a
  607. else
  608. config._isValid = false
  609. break
  610. case 'jD':
  611. case 'jDD':
  612. case 'jDDD':
  613. case 'jDDDD':
  614. if (input != null)
  615. datePartArray[2] = ~~input
  616. break
  617. case 'jYY':
  618. datePartArray[0] = ~~input + (~~input > 47 ? 1300 : 1400)
  619. break
  620. case 'jYYYY':
  621. case 'jYYYYY':
  622. datePartArray[0] = ~~input
  623. }
  624. if (input == null)
  625. config._isValid = false
  626. }
  627. function dateFromArray(config) {
  628. var g
  629. , j
  630. , jy = config._a[0]
  631. , jm = config._a[1]
  632. , jd = config._a[2]
  633. if ((jy == null) && (jm == null) && (jd == null))
  634. return [0, 0, 1]
  635. jy = jy != null ? jy : 0
  636. jm = jm != null ? jm : 0
  637. jd = jd != null ? jd : 1
  638. if (jd < 1 || jd > jMoment.jDaysInMonth(jy, jm) || jm < 0 || jm > 11)
  639. config._isValid = false
  640. g = toGregorian(jy, jm, jd)
  641. j = toJalaali(g.gy, g.gm, g.gd)
  642. if (isNaN(g.gy))
  643. config._isValid = false
  644. config._jDiff = 0
  645. if (~~j.jy !== jy)
  646. config._jDiff += 1
  647. if (~~j.jm !== jm)
  648. config._jDiff += 1
  649. if (~~j.jd !== jd)
  650. config._jDiff += 1
  651. return [g.gy, g.gm, g.gd]
  652. }
  653. function makeDateFromStringAndFormat(config) {
  654. var tokens = config._f.match(formattingTokens)
  655. , string = config._i + ''
  656. , len = tokens.length
  657. , i
  658. , token
  659. , parsedInput
  660. config._a = []
  661. for (i = 0; i < len; i += 1) {
  662. token = tokens[i]
  663. parsedInput = (getParseRegexForToken(token, config).exec(string) || [])[0]
  664. if (parsedInput)
  665. string = string.slice(string.indexOf(parsedInput) + parsedInput.length)
  666. if (formatTokenFunctions[token])
  667. addTimeToArrayFromToken(token, parsedInput, config)
  668. }
  669. if (string)
  670. config._il = string
  671. return dateFromArray(config)
  672. }
  673. function makeDateFromStringAndArray(config, utc) {
  674. var len = config._f.length
  675. , i
  676. , format
  677. , tempMoment
  678. , bestMoment
  679. , currentScore
  680. , scoreToBeat
  681. if (len === 0) {
  682. return makeMoment(new Date(NaN))
  683. }
  684. for (i = 0; i < len; i += 1) {
  685. format = config._f[i]
  686. currentScore = 0
  687. tempMoment = makeMoment(config._i, format, config._l, config._strict, utc)
  688. if (!tempMoment.isValid()) continue
  689. // currentScore = compareArrays(tempMoment._a, tempMoment.toArray())
  690. currentScore += tempMoment._jDiff
  691. if (tempMoment._il)
  692. currentScore += tempMoment._il.length
  693. if (scoreToBeat == null || currentScore < scoreToBeat) {
  694. scoreToBeat = currentScore
  695. bestMoment = tempMoment
  696. }
  697. }
  698. return bestMoment
  699. }
  700. function removeParsedTokens(config) {
  701. var string = config._i + ''
  702. , input = ''
  703. , format = ''
  704. , array = config._f.match(formattingTokens)
  705. , len = array.length
  706. , i
  707. , match
  708. , parsed
  709. for (i = 0; i < len; i += 1) {
  710. match = array[i]
  711. parsed = (getParseRegexForToken(match, config).exec(string) || [])[0]
  712. if (parsed)
  713. string = string.slice(string.indexOf(parsed) + parsed.length)
  714. if (!(formatTokenFunctions[match] instanceof Function)) {
  715. format += match
  716. if (parsed)
  717. input += parsed
  718. }
  719. }
  720. config._i = input
  721. config._f = format
  722. }
  723. /************************************
  724. Week of Year
  725. ************************************/
  726. function jWeekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
  727. var end = firstDayOfWeekOfYear - firstDayOfWeek
  728. , daysToDayOfWeek = firstDayOfWeekOfYear - mom.day()
  729. , adjustedMoment
  730. if (daysToDayOfWeek > end) {
  731. daysToDayOfWeek -= 7
  732. }
  733. if (daysToDayOfWeek < end - 7) {
  734. daysToDayOfWeek += 7
  735. }
  736. adjustedMoment = jMoment(mom).add(daysToDayOfWeek, 'd')
  737. return { week: Math.ceil(adjustedMoment.jDayOfYear() / 7)
  738. , year: adjustedMoment.jYear()
  739. }
  740. }
  741. /************************************
  742. Top Level Functions
  743. ************************************/
  744. var maxTimestamp = 57724432199999
  745. function makeMoment(input, format, lang, strict, utc) {
  746. if (typeof lang === 'boolean') {
  747. strict = lang
  748. lang = undefined
  749. }
  750. if (format && typeof format === 'string')
  751. format = fixFormat(format, moment)
  752. var config =
  753. { _i: input
  754. , _f: format
  755. , _l: lang
  756. , _strict: strict
  757. , _isUTC: utc
  758. }
  759. , date
  760. , m
  761. , jm
  762. , origInput = input
  763. , origFormat = format
  764. if (format) {
  765. if (isArray(format)) {
  766. return makeDateFromStringAndArray(config, utc)
  767. } else {
  768. date = makeDateFromStringAndFormat(config)
  769. removeParsedTokens(config)
  770. format = 'YYYY-MM-DD-' + config._f
  771. input = leftZeroFill(date[0], 4) + '-'
  772. + leftZeroFill(date[1] + 1, 2) + '-'
  773. + leftZeroFill(date[2], 2) + '-'
  774. + config._i
  775. }
  776. }
  777. if (utc)
  778. m = moment.utc(input, format, lang, strict)
  779. else
  780. m = moment(input, format, lang, strict)
  781. if (config._isValid === false)
  782. m._isValid = false
  783. m._jDiff = config._jDiff || 0
  784. jm = objectCreate(jMoment.fn)
  785. extend(jm, m)
  786. if (strict && format && jm.isValid()) {
  787. jm._isValid = jm.format(origFormat) === origInput
  788. }
  789. if (m._d.getTime() > maxTimestamp) {
  790. jm._isValid = false
  791. }
  792. return jm
  793. }
  794. function jMoment(input, format, lang, strict) {
  795. return makeMoment(input, format, lang, strict, false)
  796. }
  797. extend(jMoment, moment)
  798. jMoment.fn = objectCreate(moment.fn)
  799. jMoment.utc = function (input, format, lang, strict) {
  800. return makeMoment(input, format, lang, strict, true)
  801. }
  802. jMoment.unix = function (input) {
  803. return makeMoment(input * 1000)
  804. }
  805. /************************************
  806. jMoment Prototype
  807. ************************************/
  808. function fixFormat(format, _moment) {
  809. var i = 5
  810. var replace = function (input) {
  811. return _moment.localeData().longDateFormat(input) || input
  812. }
  813. while (i > 0 && localFormattingTokens.test(format)) {
  814. i -= 1
  815. format = format.replace(localFormattingTokens, replace)
  816. }
  817. return format
  818. }
  819. jMoment.fn.format = function (format) {
  820. if (format) {
  821. format = fixFormat(format, this)
  822. if (!formatFunctions[format]) {
  823. formatFunctions[format] = makeFormatFunction(format)
  824. }
  825. format = formatFunctions[format](this)
  826. }
  827. return moment.fn.format.call(this, format)
  828. }
  829. jMoment.fn.jYear = function (input) {
  830. var lastDay
  831. , j
  832. , g
  833. if (typeof input === 'number') {
  834. j = toJalaali(this.year(), this.month(), this.date())
  835. lastDay = Math.min(j.jd, jMoment.jDaysInMonth(input, j.jm))
  836. g = toGregorian(input, j.jm, lastDay)
  837. setDate(this, g.gy, g.gm, g.gd)
  838. moment.updateOffset(this)
  839. return this
  840. } else {
  841. return toJalaali(this.year(), this.month(), this.date()).jy
  842. }
  843. }
  844. jMoment.fn.jMonth = function (input) {
  845. var lastDay
  846. , j
  847. , g
  848. if (input != null) {
  849. if (typeof input === 'string') {
  850. input = this.localeData().jMonthsParse(input)
  851. if (typeof input !== 'number')
  852. return this
  853. }
  854. j = toJalaali(this.year(), this.month(), this.date())
  855. lastDay = Math.min(j.jd, jMoment.jDaysInMonth(j.jy, input))
  856. this.jYear(j.jy + div(input, 12))
  857. input = mod(input, 12)
  858. if (input < 0) {
  859. input += 12
  860. this.jYear(this.jYear() - 1)
  861. }
  862. g = toGregorian(this.jYear(), input, lastDay)
  863. setDate(this, g.gy, g.gm, g.gd)
  864. moment.updateOffset(this)
  865. return this
  866. } else {
  867. return toJalaali(this.year(), this.month(), this.date()).jm
  868. }
  869. }
  870. jMoment.fn.jDate = function (input) {
  871. var j
  872. , g
  873. if (typeof input === 'number') {
  874. j = toJalaali(this.year(), this.month(), this.date())
  875. g = toGregorian(j.jy, j.jm, input)
  876. setDate(this, g.gy, g.gm, g.gd)
  877. moment.updateOffset(this)
  878. return this
  879. } else {
  880. return toJalaali(this.year(), this.month(), this.date()).jd
  881. }
  882. }
  883. jMoment.fn.jDayOfYear = function (input) {
  884. var dayOfYear = Math.round((jMoment(this).startOf('day') - jMoment(this).startOf('jYear')) / 864e5) + 1
  885. return input == null ? dayOfYear : this.add(input - dayOfYear, 'd')
  886. }
  887. jMoment.fn.jWeek = function (input) {
  888. var week = jWeekOfYear(this, this.localeData()._week.dow, this.localeData()._week.doy).week
  889. return input == null ? week : this.add((input - week) * 7, 'd')
  890. }
  891. jMoment.fn.jWeekYear = function (input) {
  892. var year = jWeekOfYear(this, this.localeData()._week.dow, this.localeData()._week.doy).year
  893. return input == null ? year : this.add(input - year, 'y')
  894. }
  895. jMoment.fn.add = function (val, units) {
  896. var temp
  897. if (units !== null && !isNaN(+units)) {
  898. temp = val
  899. val = units
  900. units = temp
  901. }
  902. units = normalizeUnits(units)
  903. if (units === 'jyear') {
  904. this.jYear(this.jYear() + val)
  905. } else if (units === 'jmonth') {
  906. this.jMonth(this.jMonth() + val)
  907. } else {
  908. moment.fn.add.call(this, val, units)
  909. if (isNaN(this.jYear())) {
  910. this._isValid = false
  911. }
  912. }
  913. return this
  914. }
  915. jMoment.fn.subtract = function (val, units) {
  916. var temp
  917. if (units !== null && !isNaN(+units)) {
  918. temp = val
  919. val = units
  920. units = temp
  921. }
  922. units = normalizeUnits(units)
  923. if (units === 'jyear') {
  924. this.jYear(this.jYear() - val)
  925. } else if (units === 'jmonth') {
  926. this.jMonth(this.jMonth() - val)
  927. } else {
  928. moment.fn.subtract.call(this, val, units)
  929. }
  930. return this
  931. }
  932. jMoment.fn.startOf = function (units) {
  933. units = normalizeUnits(units)
  934. if (units === 'jyear' || units === 'jmonth') {
  935. if (units === 'jyear') {
  936. this.jMonth(0)
  937. }
  938. this.jDate(1)
  939. this.hours(0)
  940. this.minutes(0)
  941. this.seconds(0)
  942. this.milliseconds(0)
  943. return this
  944. } else {
  945. return moment.fn.startOf.call(this, units)
  946. }
  947. }
  948. jMoment.fn.endOf = function (units) {
  949. units = normalizeUnits(units)
  950. if (units === undefined || units === 'milisecond') {
  951. return this
  952. }
  953. return this.startOf(units).add(1, (units === 'isoweek' ? 'week' : units)).subtract(1, 'ms')
  954. }
  955. jMoment.fn.isSame = function (other, units) {
  956. units = normalizeUnits(units)
  957. if (units === 'jyear' || units === 'jmonth') {
  958. return moment.fn.isSame.call(this.startOf(units), other.startOf(units))
  959. }
  960. return moment.fn.isSame.call(this, other, units)
  961. }
  962. jMoment.fn.clone = function () {
  963. return jMoment(this)
  964. }
  965. jMoment.fn.jYears = jMoment.fn.jYear
  966. jMoment.fn.jMonths = jMoment.fn.jMonth
  967. jMoment.fn.jDates = jMoment.fn.jDate
  968. jMoment.fn.jWeeks = jMoment.fn.jWeek
  969. /************************************
  970. jMoment Statics
  971. ************************************/
  972. jMoment.jDaysInMonth = function (year, month) {
  973. year += div(month, 12)
  974. month = mod(month, 12)
  975. if (month < 0) {
  976. month += 12
  977. year -= 1
  978. }
  979. if (month < 6) {
  980. return 31
  981. } else if (month < 11) {
  982. return 30
  983. } else if (jMoment.jIsLeapYear(year)) {
  984. return 30
  985. } else {
  986. return 29
  987. }
  988. }
  989. jMoment.jIsLeapYear = jalaali.isLeapJalaaliYear
  990. jMoment.loadPersian = function (args) {
  991. var usePersianDigits = args !== undefined && args.hasOwnProperty('usePersianDigits') ? args.usePersianDigits : false
  992. var dialect = args !== undefined && args.hasOwnProperty('dialect') ? args.dialect : 'persian'
  993. moment.locale('fa')
  994. moment.updateLocale('fa'
  995. , { months: ('ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر').split('_')
  996. , monthsShort: ('ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر').split('_')
  997. , weekdays:
  998. {
  999. 'persian': ('یک\u200cشنبه_دوشنبه_سه\u200cشنبه_چهارشنبه_پنج\u200cشنبه_آدینه_شنبه').split('_'),
  1000. 'persian-modern': ('یک\u200cشنبه_دوشنبه_سه\u200cشنبه_چهارشنبه_پنج\u200cشنبه_جمعه_شنبه').split('_')
  1001. }[dialect]
  1002. , weekdaysShort:
  1003. {
  1004. 'persian': ('یک\u200cشنبه_دوشنبه_سه\u200cشنبه_چهارشنبه_پنج\u200cشنبه_آدینه_شنبه').split('_'),
  1005. 'persian-modern': ('یک\u200cشنبه_دوشنبه_سه\u200cشنبه_چهارشنبه_پنج\u200cشنبه_جمعه_شنبه').split('_')
  1006. }[dialect]
  1007. , weekdaysMin:
  1008. {
  1009. 'persian': 'ی_د_س_چ_پ_آ_ش'.split('_'),
  1010. 'persian-modern': 'ی_د_س_چ_پ_ج_ش'.split('_')
  1011. }[dialect]
  1012. , longDateFormat:
  1013. { LT: 'HH:mm'
  1014. , L: 'jYYYY/jMM/jDD'
  1015. , LL: 'jD jMMMM jYYYY'
  1016. , LLL: 'jD jMMMM jYYYY LT'
  1017. , LLLL: 'dddd، jD jMMMM jYYYY LT'
  1018. }
  1019. , calendar:
  1020. { sameDay: '[امروز ساعت] LT'
  1021. , nextDay: '[فردا ساعت] LT'
  1022. , nextWeek: 'dddd [ساعت] LT'
  1023. , lastDay: '[دیروز ساعت] LT'
  1024. , lastWeek: 'dddd [ی پیش ساعت] LT'
  1025. , sameElse: 'L'
  1026. }
  1027. , relativeTime:
  1028. { future: 'در %s'
  1029. , past: '%s پیش'
  1030. , s: 'چند ثانیه'
  1031. , m: '1 دقیقه'
  1032. , mm: '%d دقیقه'
  1033. , h: '1 ساعت'
  1034. , hh: '%d ساعت'
  1035. , d: '1 روز'
  1036. , dd: '%d روز'
  1037. , M: '1 ماه'
  1038. , MM: '%d ماه'
  1039. , y: '1 سال'
  1040. , yy: '%d سال'
  1041. }
  1042. , preparse: function (string) {
  1043. if (usePersianDigits) {
  1044. return string.replace(/[۰-۹]/g, function (match) {
  1045. return numberMap[match]
  1046. }).replace(/،/g, ',')
  1047. }
  1048. return string
  1049. }
  1050. , postformat: function (string) {
  1051. if (usePersianDigits) {
  1052. return string.replace(/\d/g, function (match) {
  1053. return symbolMap[match]
  1054. }).replace(/,/g, '،')
  1055. }
  1056. return string
  1057. }
  1058. , ordinal: '%dم'
  1059. , week:
  1060. { dow: 6 // Saturday is the first day of the week.
  1061. , doy: 12 // The week that contains Jan 1st is the first week of the year.
  1062. }
  1063. , meridiem: function (hour) {
  1064. return hour < 12 ? 'ق.ظ' : 'ب.ظ'
  1065. }
  1066. , jMonths:
  1067. {
  1068. 'persian': ('فروردین_اردیبهشت_خرداد_تیر_امرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند').split('_'),
  1069. 'persian-modern': ('فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند').split('_')
  1070. }[dialect]
  1071. , jMonthsShort:
  1072. {
  1073. 'persian': 'فرو_ارد_خرد_تیر_امر_شهر_مهر_آبا_آذر_دی_بهم_اسف'.split('_'),
  1074. 'persian-modern': 'فرو_ارد_خرد_تیر_مرد_شهر_مهر_آبا_آذر_دی_بهم_اسف'.split('_')
  1075. }[dialect]
  1076. }
  1077. )
  1078. }
  1079. jMoment.jConvert = { toJalaali: toJalaali
  1080. , toGregorian: toGregorian
  1081. }
  1082. /************************************
  1083. Jalaali Conversion
  1084. ************************************/
  1085. function toJalaali(gy, gm, gd) {
  1086. try {
  1087. var j = jalaali.toJalaali(gy, gm + 1, gd)
  1088. j.jm -= 1
  1089. return j
  1090. } catch (e) {
  1091. return {
  1092. jy: NaN
  1093. , jm: NaN
  1094. , jd: NaN
  1095. }
  1096. }
  1097. }
  1098. function toGregorian(jy, jm, jd) {
  1099. try {
  1100. var g = jalaali.toGregorian(jy, jm + 1, jd)
  1101. g.gm -= 1
  1102. return g
  1103. } catch (e) {
  1104. return {
  1105. gy: NaN
  1106. , gm: NaN
  1107. , gd: NaN
  1108. }
  1109. }
  1110. }
  1111. /*
  1112. Utility helper functions.
  1113. */
  1114. function div(a, b) {
  1115. return ~~(a / b)
  1116. }
  1117. function mod(a, b) {
  1118. return a - ~~(a / b) * b
  1119. }
  1120. });
  1121. if (typeof exports == "object") {
  1122. module.exports = require("moment-jalaali");
  1123. } else if (typeof define == "function" && define.amd) {
  1124. define([], function(){ return require("moment-jalaali"); });
  1125. } else {
  1126. this["moment"] = require("moment-jalaali");
  1127. }
  1128. })();