Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved performance #13

Merged
merged 9 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ module.exports =
, d2g: d2g
}

/*
Jalaali years starting the 33-year rule.
*/
var breaks = [ -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210
, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
]

/*
Converts a Gregorian date to Jalaali.
*/
Expand Down Expand Up @@ -46,7 +53,7 @@ function isValidJalaaliDate(jy, jm, jd) {
Is this a leap year or not?
*/
function isLeapJalaaliYear(jy) {
return jalCal(jy).leap === 0
return jalCalLeap(jy) === 0
}

/*
Expand All @@ -59,26 +66,61 @@ function jalaaliMonthLength(jy, jm) {
return 29
}

/*
This function determines if the Jalaali (Persian) year is
leap (366-day long) or is the common year (365 days)

@param jy Jalaali calendar year (-61 to 3177)
@returns number of years since the last leap year (0 to 4)
*/
function jalCalLeap(jy) {
var bl = breaks.length
, jp = breaks[0]
, jm
, jump
, leap
, n
, i

if (jy < jp || jy >= breaks[bl - 1])
throw new Error('Invalid Jalaali year ' + jy)

for (i = 1; i < bl; i += 1) {
jm = breaks[i]
jump = jm - jp
if (jy < jm)
break
jp = jm
}
n = jy - jp

if (jump - n < 6)
n = n - jump + div(jump + 4, 33) * 33
leap = mod(mod(n + 1, 33) - 1, 4)
if (leap === -1) {
leap = 4
}

return leap
}

/*
This function determines if the Jalaali (Persian) year is
leap (366-day long) or is the common year (365 days), and
finds the day in March (Gregorian calendar) of the first
day of the Jalaali year (jy).

@param jy Jalaali calendar year (-61 to 3177)
@param withoutLeap when don't need leap (true or false) default is false
@return
leap: number of years since the last leap year (0 to 4)
gy: Gregorian year of the beginning of Jalaali year
march: the March day of Farvardin the 1st (1st day of jy)
@see: http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm
@see: http://www.fourmilab.ch/documents/calendar/
*/
function jalCal(jy) {
// Jalaali years starting the 33-year rule.
var breaks = [ -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210
, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
]
, bl = breaks.length
function jalCal(jy, withoutLeap) {
var bl = breaks.length
, gy = jy + 621
, leapJ = -14
, jp = breaks[0]
Expand Down Expand Up @@ -117,11 +159,13 @@ function jalCal(jy) {
march = 20 + leapJ - leapG

// Find how many years have passed since the last leap year.
if (jump - n < 6)
n = n - jump + div(jump + 4, 33) * 33
leap = mod(mod(n + 1, 33) - 1, 4)
if (leap === -1) {
leap = 4
if(!withoutLeap){
if (jump - n < 6)
n = n - jump + div(jump + 4, 33) * 33
leap = mod(mod(n + 1, 33) - 1, 4)
if (leap === -1) {
leap = 4
}
}

return { leap: leap
Expand All @@ -139,7 +183,7 @@ function jalCal(jy) {
@return Julian Day number
*/
function j2d(jy, jm, jd) {
var r = jalCal(jy)
var r = jalCal(jy, true)
return g2d(r.gy, 3, r.march) + (jm - 1) * 31 - div(jm, 7) * (jm - 7) + jd - 1
}

Expand All @@ -155,7 +199,7 @@ function j2d(jy, jm, jd) {
function d2j(jdn) {
var gy = d2g(jdn).gy // Calculate Gregorian year (gy).
, jy = gy - 621
, r = jalCal(jy)
, r = jalCal(jy, false)
, jdn1f = g2d(gy, 3, r.march)
, jd
, jm
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jalaali-js",
"version": "1.1.1",
"version": "1.1.2",
"description": "Converts Gregorian and Jalaali calendars to each other",
"main": "index.js",
"repository": {
Expand Down