Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

360 linhas
9.1 KiB

  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jalaali = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. /*
  3. Expose functions.
  4. */
  5. module.exports =
  6. { toJalaali: toJalaali
  7. , toGregorian: toGregorian
  8. , isValidJalaaliDate: isValidJalaaliDate
  9. , isLeapJalaaliYear: isLeapJalaaliYear
  10. , jalaaliMonthLength: jalaaliMonthLength
  11. , jalCal: jalCal
  12. , j2d: j2d
  13. , d2j: d2j
  14. , g2d: g2d
  15. , d2g: d2g
  16. , jalaaliToDateObject: jalaaliToDateObject
  17. , jalaaliWeek: jalaaliWeek
  18. }
  19. /*
  20. Jalaali years starting the 33-year rule.
  21. */
  22. var breaks = [ -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210
  23. , 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
  24. ]
  25. /*
  26. Converts a Gregorian date to Jalaali.
  27. */
  28. function toJalaali(gy, gm, gd) {
  29. if (Object.prototype.toString.call(gy) === '[object Date]') {
  30. gd = gy.getDate()
  31. gm = gy.getMonth() + 1
  32. gy = gy.getFullYear()
  33. }
  34. return d2j(g2d(gy, gm, gd))
  35. }
  36. /*
  37. Converts a Jalaali date to Gregorian.
  38. */
  39. function toGregorian(jy, jm, jd) {
  40. return d2g(j2d(jy, jm, jd))
  41. }
  42. /*
  43. Checks whether a Jalaali date is valid or not.
  44. */
  45. function isValidJalaaliDate(jy, jm, jd) {
  46. return jy >= -61 && jy <= 3177 &&
  47. jm >= 1 && jm <= 12 &&
  48. jd >= 1 && jd <= jalaaliMonthLength(jy, jm)
  49. }
  50. /*
  51. Is this a leap year or not?
  52. */
  53. function isLeapJalaaliYear(jy) {
  54. return jalCalLeap(jy) === 0
  55. }
  56. /*
  57. Number of days in a given month in a Jalaali year.
  58. */
  59. function jalaaliMonthLength(jy, jm) {
  60. if (jm <= 6) return 31
  61. if (jm <= 11) return 30
  62. if (isLeapJalaaliYear(jy)) return 30
  63. return 29
  64. }
  65. /*
  66. This function determines if the Jalaali (Persian) year is
  67. leap (366-day long) or is the common year (365 days)
  68. @param jy Jalaali calendar year (-61 to 3177)
  69. @returns number of years since the last leap year (0 to 4)
  70. */
  71. function jalCalLeap(jy) {
  72. var bl = breaks.length
  73. , jp = breaks[0]
  74. , jm
  75. , jump
  76. , leap
  77. , n
  78. , i
  79. if (jy < jp || jy >= breaks[bl - 1])
  80. throw new Error('Invalid Jalaali year ' + jy)
  81. for (i = 1; i < bl; i += 1) {
  82. jm = breaks[i]
  83. jump = jm - jp
  84. if (jy < jm)
  85. break
  86. jp = jm
  87. }
  88. n = jy - jp
  89. if (jump - n < 6)
  90. n = n - jump + div(jump + 4, 33) * 33
  91. leap = mod(mod(n + 1, 33) - 1, 4)
  92. if (leap === -1) {
  93. leap = 4
  94. }
  95. return leap
  96. }
  97. /*
  98. This function determines if the Jalaali (Persian) year is
  99. leap (366-day long) or is the common year (365 days), and
  100. finds the day in March (Gregorian calendar) of the first
  101. day of the Jalaali year (jy).
  102. @param jy Jalaali calendar year (-61 to 3177)
  103. @param withoutLeap when don't need leap (true or false) default is false
  104. @return
  105. leap: number of years since the last leap year (0 to 4)
  106. gy: Gregorian year of the beginning of Jalaali year
  107. march: the March day of Farvardin the 1st (1st day of jy)
  108. @see: http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm
  109. @see: http://www.fourmilab.ch/documents/calendar/
  110. */
  111. function jalCal(jy, withoutLeap) {
  112. var bl = breaks.length
  113. , gy = jy + 621
  114. , leapJ = -14
  115. , jp = breaks[0]
  116. , jm
  117. , jump
  118. , leap
  119. , leapG
  120. , march
  121. , n
  122. , i
  123. if (jy < jp || jy >= breaks[bl - 1])
  124. throw new Error('Invalid Jalaali year ' + jy)
  125. // Find the limiting years for the Jalaali year jy.
  126. for (i = 1; i < bl; i += 1) {
  127. jm = breaks[i]
  128. jump = jm - jp
  129. if (jy < jm)
  130. break
  131. leapJ = leapJ + div(jump, 33) * 8 + div(mod(jump, 33), 4)
  132. jp = jm
  133. }
  134. n = jy - jp
  135. // Find the number of leap years from AD 621 to the beginning
  136. // of the current Jalaali year in the Persian calendar.
  137. leapJ = leapJ + div(n, 33) * 8 + div(mod(n, 33) + 3, 4)
  138. if (mod(jump, 33) === 4 && jump - n === 4)
  139. leapJ += 1
  140. // And the same in the Gregorian calendar (until the year gy).
  141. leapG = div(gy, 4) - div((div(gy, 100) + 1) * 3, 4) - 150
  142. // Determine the Gregorian date of Farvardin the 1st.
  143. march = 20 + leapJ - leapG
  144. // return with gy and march when we don't need leap
  145. if (withoutLeap) return { gy: gy, march: march };
  146. // Find how many years have passed since the last leap year.
  147. if (jump - n < 6)
  148. n = n - jump + div(jump + 4, 33) * 33
  149. leap = mod(mod(n + 1, 33) - 1, 4)
  150. if (leap === -1) {
  151. leap = 4
  152. }
  153. return { leap: leap
  154. , gy: gy
  155. , march: march
  156. }
  157. }
  158. /*
  159. Converts a date of the Jalaali calendar to the Julian Day number.
  160. @param jy Jalaali year (1 to 3100)
  161. @param jm Jalaali month (1 to 12)
  162. @param jd Jalaali day (1 to 29/31)
  163. @return Julian Day number
  164. */
  165. function j2d(jy, jm, jd) {
  166. var r = jalCal(jy, true)
  167. return g2d(r.gy, 3, r.march) + (jm - 1) * 31 - div(jm, 7) * (jm - 7) + jd - 1
  168. }
  169. /*
  170. Converts the Julian Day number to a date in the Jalaali calendar.
  171. @param jdn Julian Day number
  172. @return
  173. jy: Jalaali year (1 to 3100)
  174. jm: Jalaali month (1 to 12)
  175. jd: Jalaali day (1 to 29/31)
  176. */
  177. function d2j(jdn) {
  178. var gy = d2g(jdn).gy // Calculate Gregorian year (gy).
  179. , jy = gy - 621
  180. , r = jalCal(jy, false)
  181. , jdn1f = g2d(gy, 3, r.march)
  182. , jd
  183. , jm
  184. , k
  185. // Find number of days that passed since 1 Farvardin.
  186. k = jdn - jdn1f
  187. if (k >= 0) {
  188. if (k <= 185) {
  189. // The first 6 months.
  190. jm = 1 + div(k, 31)
  191. jd = mod(k, 31) + 1
  192. return { jy: jy
  193. , jm: jm
  194. , jd: jd
  195. }
  196. } else {
  197. // The remaining months.
  198. k -= 186
  199. }
  200. } else {
  201. // Previous Jalaali year.
  202. jy -= 1
  203. k += 179
  204. if (r.leap === 1)
  205. k += 1
  206. }
  207. jm = 7 + div(k, 30)
  208. jd = mod(k, 30) + 1
  209. return { jy: jy
  210. , jm: jm
  211. , jd: jd
  212. }
  213. }
  214. /*
  215. Calculates the Julian Day number from Gregorian or Julian
  216. calendar dates. This integer number corresponds to the noon of
  217. the date (i.e. 12 hours of Universal Time).
  218. The procedure was tested to be good since 1 March, -100100 (of both
  219. calendars) up to a few million years into the future.
  220. @param gy Calendar year (years BC numbered 0, -1, -2, ...)
  221. @param gm Calendar month (1 to 12)
  222. @param gd Calendar day of the month (1 to 28/29/30/31)
  223. @return Julian Day number
  224. */
  225. function g2d(gy, gm, gd) {
  226. var d = div((gy + div(gm - 8, 6) + 100100) * 1461, 4)
  227. + div(153 * mod(gm + 9, 12) + 2, 5)
  228. + gd - 34840408
  229. d = d - div(div(gy + 100100 + div(gm - 8, 6), 100) * 3, 4) + 752
  230. return d
  231. }
  232. /*
  233. Calculates Gregorian and Julian calendar dates from the Julian Day number
  234. (jdn) for the period since jdn=-34839655 (i.e. the year -100100 of both
  235. calendars) to some millions years ahead of the present.
  236. @param jdn Julian Day number
  237. @return
  238. gy: Calendar year (years BC numbered 0, -1, -2, ...)
  239. gm: Calendar month (1 to 12)
  240. gd: Calendar day of the month M (1 to 28/29/30/31)
  241. */
  242. function d2g(jdn) {
  243. var j
  244. , i
  245. , gd
  246. , gm
  247. , gy
  248. j = 4 * jdn + 139361631
  249. j = j + div(div(4 * jdn + 183187720, 146097) * 3, 4) * 4 - 3908
  250. i = div(mod(j, 1461), 4) * 5 + 308
  251. gd = div(mod(i, 153), 5) + 1
  252. gm = mod(div(i, 153), 12) + 1
  253. gy = div(j, 1461) - 100100 + div(8 - gm, 6)
  254. return { gy: gy
  255. , gm: gm
  256. , gd: gd
  257. }
  258. }
  259. /**
  260. * Return Saturday and Friday day of current week(week start in Saturday)
  261. * @param {number} jy jalaali year
  262. * @param {number} jm jalaali month
  263. * @param {number} jd jalaali day
  264. * @returns Saturday and Friday of current week
  265. */
  266. function jalaaliWeek(jy, jm, jd) {
  267. var dayOfWeek = jalaaliToDateObject(jy, jm, jd).getDay();
  268. var startDayDifference = dayOfWeek == 6 ? 0 : -(dayOfWeek+1);
  269. var endDayDifference = 6+startDayDifference;
  270. return {
  271. saturday: d2j(j2d(jy, jm, jd+startDayDifference)),
  272. friday: d2j(j2d(jy, jm, jd+endDayDifference))
  273. }
  274. }
  275. /**
  276. * Convert Jalaali calendar dates to javascript Date object
  277. * @param {number} jy jalaali year
  278. * @param {number} jm jalaali month
  279. * @param {number} jd jalaali day
  280. * @param {number} [h] hours
  281. * @param {number} [m] minutes
  282. * @param {number} [s] seconds
  283. * @param {number} [ms] milliseconds
  284. * @returns Date object of the jalaali calendar dates
  285. */
  286. function jalaaliToDateObject(
  287. jy,
  288. jm,
  289. jd,
  290. h,
  291. m,
  292. s,
  293. ms
  294. ) {
  295. var gregorianCalenderDate = toGregorian(jy, jm, jd);
  296. return new Date(
  297. gregorianCalenderDate.gy,
  298. gregorianCalenderDate.gm - 1,
  299. gregorianCalenderDate.gd,
  300. h || 0,
  301. m || 0,
  302. s || 0,
  303. ms || 0
  304. );
  305. }
  306. /*
  307. Utility helper functions.
  308. */
  309. function div(a, b) {
  310. return ~~(a / b)
  311. }
  312. function mod(a, b) {
  313. return a - ~~(a / b) * b
  314. }
  315. },{}],2:[function(require,module,exports){
  316. module.exports = require('./index.js');
  317. },{"./index.js":1}]},{},[2])(2)
  318. });