You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1383 lines
38 KiB

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