bikram-sambat
Advanced tools
Comparing version 1.3.10 to 1.4.0
{ | ||
"name": "bikram-sambat", | ||
"version": "1.3.10", | ||
"version": "1.4.0", | ||
"description": "JS utilities for converting between the Nepali Bikram Sambat (Vikram Samvat) and Gregorian (standard European) calendars.", | ||
@@ -38,2 +38,2 @@ "main": "src/index.js", | ||
} | ||
} | ||
} |
@@ -18,5 +18,7 @@ var toDevanagari = require('eurodigit/src/to_non_euro').devanagari; | ||
var BS_LAST_YEAR = BS_YEAR_ZERO + ENCODED_MONTH_LENGTHS.length - 1; | ||
/** | ||
* Magic numbers: | ||
* 2000 <- the first year encoded in ENCODED_MONTH_LENGTHS | ||
* 2000 <- the first year (BS) encoded in ENCODED_MONTH_LENGTHS | ||
* month #5 <- this is the only month which has a day variation of more than 1 | ||
@@ -26,3 +28,7 @@ * & 3 <- this is a 2 bit mask, i.e. 0...011 | ||
function daysInMonth(year, month) { | ||
return 29 + ((ENCODED_MONTH_LENGTHS[year - 2000] >>> | ||
// TODO why does this accept 0? | ||
if(month < 0 || month > 12) throw new Error('Invalid month value ' + month); | ||
var delta = ENCODED_MONTH_LENGTHS[year - 2000]; | ||
if(typeof delta === 'undefined') throw new Error('No data for year: ' + year + ' BS'); | ||
return 29 + ((delta >>> | ||
(((month-1) << 1))) & 3); | ||
@@ -65,15 +71,17 @@ } | ||
function toGreg(year, month, day) { | ||
// TODO month bounds-checking should be handled in daysInMonth() | ||
if(month < 1) throw new Error('Invalid month value ' + month); | ||
if(year < BS_YEAR_ZERO) throw new Error('Invalid year value ' + year); | ||
if(day < 1 || day > daysInMonth(year, month)) throw new Error('Invalid day value', day); | ||
var timestamp = BS_EPOCH_TS; | ||
while(year >= BS_YEAR_ZERO) { | ||
while(month >= 1) { | ||
while(day >= 1) { | ||
while(--day >= 0) { | ||
timestamp += MS_PER_DAY; | ||
--day; | ||
} | ||
--month; | ||
day = daysInMonth(year, month, day); | ||
day = daysInMonth(year, --month); | ||
} | ||
--year; | ||
month = 12; | ||
day = daysInMonth(year, month, day); | ||
day = daysInMonth(--year, month); | ||
} | ||
@@ -89,2 +97,7 @@ | ||
function toGreg_text(year, month, day) { | ||
var d = toGreg(year, month, day); | ||
return d.year + '-' + zPad(d.month) + '-' + zPad(d.day); | ||
} | ||
module.exports = { | ||
@@ -95,3 +108,4 @@ daysInMonth: daysInMonth, | ||
toBik_text: toBik_text, | ||
toGreg: toGreg | ||
toGreg: toGreg, | ||
toGreg_text: toGreg_text | ||
}; |
@@ -73,4 +73,78 @@ var _ = require('lodash'), | ||
}); | ||
it('should throw Error if year is too small', () => { | ||
assert.throw(() => | ||
bs.toGreg(2006, 1, 1)); | ||
}); | ||
it('should throw Error if year is too big', () => { | ||
assert.throw(() => | ||
bs.toGreg(9999, 1, 1)); | ||
}); | ||
it('should throw Error if year is NaN', () => { | ||
assert.throw(() => | ||
bs.toGreg('', 1, 1)); | ||
}); | ||
it('should throw Error if month is too small', () => { | ||
assert.throw(() => | ||
bs.toGreg(2033, 0, 1)); | ||
}); | ||
it('should throw Error if month is too big', () => { | ||
assert.throw(() => | ||
bs.toGreg(2033, 13, 1)); | ||
}) | ||
it('should throw Error if month is NaN', () => { | ||
assert.throw(() => | ||
bs.toGreg(2033, '', 1)); | ||
}) | ||
it('should throw Error if day is too small', () => { | ||
assert.throw(() => | ||
bs.toGreg(2033, 1, 0)); | ||
}); | ||
it('should throw Error if day is too small', () => { | ||
assert.throw(() => | ||
bs.toGreg(2033, 1, 0)); | ||
}); | ||
it('should throw Error if day is NaN', () => { | ||
assert.throw(() => | ||
bs.toGreg(2033, 1, '')); | ||
}); | ||
}); | ||
describe('#toGreg()', function() { | ||
it('should translate a bikram date to a zero-padded string', () => { | ||
// expect | ||
assert.equal('1955-01-01', bs.toGreg_text(2011, 9, 17)); | ||
}); | ||
}); | ||
describe('#daysInMonth()', function() { | ||
@@ -77,0 +151,0 @@ |
9556
240