All files / calendrica-js/src julian.js

100% Statements 21/21
100% Branches 14/14
100% Functions 3/3
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4524x 24x     24x     139x     24x 171x 171x                 24x 33x 33x 33x   33x 4x 29x 11x   18x   33x 33x 33x     24x            
const { mod } = require( './general' )
const { DECEMBER, JANUARY, MARCH, fixedFromGregorian } = require( './gregorian' )
 
// Fixed date of start of the Julian calendar.
const JULIAN_EPOCH = fixedFromGregorian( 0, DECEMBER, 30 )
 
// True if j-year is a leap year on the Julian calendar.
const isJulianLeapYear = jYear => mod( jYear, 4 ) === ( jYear > 0 ? 0 : 3 )
 
// Fixed date equivalent to the Julian date j-date.
const fixedFromJulian = ( year, month, day ) => {
  const y = year < 0 ? year + 1 : year
  return JULIAN_EPOCH - 1
    + 365 * ( y - 1 )
    + Math.floor( ( y - 1 ) / 4 )
    + Math.floor( ( 1 / 12 ) * ( 367 * month - 362 ) )
    + ( month <= 2 ? 0 : ( isJulianLeapYear( year ) ? -1 : -2 ) )
    + day
}
 
// Julian (year month day) corresponding to fixed date.
const julianFromFixed = date => {
  const approx = Math.floor( ( 1 / 1461 ) * ( 4 * ( date - JULIAN_EPOCH ) + 1464 ) )
  const year = approx <= 0 ? approx - 1 : approx
  const priorDays = date - fixedFromJulian( year, JANUARY, 1 )
  let correction
  if ( date < fixedFromJulian( year, MARCH, 1 ) ) {
    correction = 0
  } else if ( isJulianLeapYear( year ) ) {
    correction = 1
  } else {
    correction = 2
  }
  const month = Math.floor( ( 1 / 367 ) * ( 12 * ( priorDays + correction ) + 373 ) )
  const day = date - fixedFromJulian( year, month, 1 ) + 1
  return { year, month, day }
}
 
module.exports = {
  JULIAN_EPOCH,
  isJulianLeapYear,
  fixedFromJulian,
  julianFromFixed,
}