Skip to content

Commit

Permalink
fix(CalEventMap): 2nd date in calendar can't be reached
Browse files Browse the repository at this point in the history
CalEventMap loops over dates in the non-gregorian calendar but breaks
early. For some dates the exact given date can't be calculated.
Loop continuation fixes this issue.

refactor: hijri calendar map generation to improve readability
The resulting map is still the same.
  • Loading branch information
commenthol committed Dec 30, 2021
1 parent 4b89046 commit 5fd6102
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 19 deletions.
38 changes: 20 additions & 18 deletions scripts/hijri-calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,45 @@
* { year: {number}, <year>: [[M,D,iY], ... [M,D,iY,M,D,iY]], <year + 1>: {} ... }
* ```
* > Note: The library moment-hijri currently only provides dates between
* > 1936 and 2080 which should be sufficient for our needs
* > 1356/1/1 (1937-03-14) and 1500/12/30 (2077-11-16) which should be
* > sufficient for our needs
*/

const fs = require('fs')
const path = require('path')
const moment = require('moment-hijri')

// 1 Muharram 1389 is at 1969-03-19 in gregorian year
const START_YEAR = 1389

const filename = path.resolve(__dirname, '../../src/internal/hijri-calendar.js')
const out = {}
const newYear = year => moment(`${year}-01-01 00:00:00`)

const YEAR0 = 580
const out = {
year: START_YEAR
}

// only years 1936 ... 2080 are supported by moment-hijri
for (let y = 1969; y <= 2080; y++) {
const iy = y - YEAR0

if (!out.year) {
out.year = iy
}
const iyy = iy - out.year
const endYear = newYear(2077).iYear()

for (let iy = START_YEAR; iy <= endYear; iy++) {
for (let im = 1; im <= 12; im++) {
const g = moment(iy + '/' + im + '/1', 'iYYYY/iM/iD')
const m = moment(iy + '/' + im + '/1', 'iYYYY/iM/iD')

const iyy = iy - out.year

const gy = g.year()
const gm = g.iMonth()
const gy = m.year()
const iim = m.iMonth()

if (!out[gy]) {
out[gy] = []
}

const monthDateDiffYear = [g.month(), g.date(), iyy]
const monthDateDiffYear = [m.month(), m.date(), iyy]

if (out[gy][gm]) {
out[gy][gm] = out[gy][gm].concat(monthDateDiffYear)
if (out[gy][iim]) {
out[gy][iim] = out[gy][iim].concat(monthDateDiffYear)
} else {
out[gy][gm] = monthDateDiffYear
out[gy][iim] = monthDateDiffYear
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/CalEventMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class CalEventMap extends CalEvent {
if (this.opts.year) {
const calYear = this.calendar.year + firstDays[i + 2]
if (this.opts.year !== calYear) {
break
continue
}
}
const d = (new CalDate({
Expand Down
49 changes: 49 additions & 0 deletions test/CalEvent.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,55 @@ describe('#CalEventFactory', function () {
// console.log(fixResult(res))
assert.deepStrictEqual(fixResult(res), exp)
})

it('5 Jumada al-awwal 1440', function () {
const date = new CalEventFactory({
fn: 'islamic',
day: 5,
month: 5,
year: 1440
})
const res = date.inYear(2019).get()
const exp = [{
date: '2019-01-11 00:00:00 -0600',
end: 'fri 2019-01-11 18:00',
start: 'thu 2019-01-10 18:00'
}]
assert.deepStrictEqual(fixResult(res), exp)
})

it('5 Jumada al-awwal 1441', function () {
const date = new CalEventFactory({
fn: 'islamic',
day: 5,
month: 5,
year: 1441
})
const res = date.inYear(2019).get()
const exp = [{
date: '2019-12-31 00:00:00 -0600',
end: 'tue 2019-12-31 18:00',
start: 'mon 2019-12-30 18:00'
}]
assert.deepStrictEqual(fixResult(res), exp)
})

it('1 Muharram 1443', function () {
const date = new CalEventFactory({
fn: 'islamic',
day: 1,
month: 1,
year: 1443
})
const res = date.inYear(2021).get()
const exp = [{
date: '2021-08-09 00:00:00 -0600',
start: 'sun 2021-08-08 18:00',
end: 'mon 2021-08-09 18:00'
}]
// console.log(fixResult(res))
assert.deepStrictEqual(fixResult(res), exp)
})
})

describe('#CalEvent', function () {
Expand Down
22 changes: 22 additions & 0 deletions test/DateFn.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ describe('#DateFn', function () {
}]
assert.deepStrictEqual(fixResult(res), exp)
})

it('29 Dhu al-Hijjah 1442', function () {
const fn = new DateFn('29 Dhu al-Hijjah 1442')
const res = fn.inYear(2021).get()
const exp = [{
date: '2021-08-08 00:00:00 -0600',
start: 'sat 2021-08-07 18:00',
end: 'sun 2021-08-08 18:00'
}]
assert.deepStrictEqual(fixResult(res), exp)
})

it('1 Muharram 1443', function () {
const fn = new DateFn('1 Muharram 1443')
const res = fn.inYear(2021).get()
const exp = [{
date: '2021-08-09 00:00:00 -0600',
start: 'sun 2021-08-08 18:00',
end: 'mon 2021-08-09 18:00'
}]
assert.deepStrictEqual(fixResult(res), exp)
})
})

describe('hebrew calendar', function () {
Expand Down

0 comments on commit 5fd6102

Please sign in to comment.