1 /*
  2  * JulianDay.js - A Julian Day object.
  3  *
  4  * Copyright © 2012-2015, JEDLSoft
  5  *
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  *
 10  *     http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *
 16  * See the License for the specific language governing permissions and
 17  * limitations under the License.
 18  */
 19 
 20 /**
 21  * @class
 22  * A Julian Day class. A Julian Day is a date based on the Julian Day count
 23  * of time invented by Joseph Scaliger in 1583 for use with astronomical calculations.
 24  * Do not confuse it with a date in the Julian calendar, which it has very
 25  * little in common with. The naming is unfortunately close, and comes from history.<p>
 26  *
 27  *
 28  * @constructor
 29  * @param {number} num the Julian Day expressed as a floating point number
 30  */
 31 var JulianDay = function(num) {
 32     this.jd = num;
 33     this.days = Math.floor(this.jd);
 34     this.frac = num - this.days;
 35 };
 36 
 37 JulianDay.prototype = {
 38     /**
 39      * Return the integral portion of this Julian Day instance. This corresponds to
 40      * the number of days since the beginning of the epoch.
 41      *
 42      * @return {number} the integral portion of this Julian Day
 43      */
 44     getDays: function() {
 45         return this.days;
 46     },
 47 
 48     /**
 49      * Set the date of this Julian Day instance.
 50      *
 51      * @param {number} days the julian date expressed as a floating point number
 52      */
 53     setDays: function(days) {
 54         this.days = Math.floor(days);
 55         this.jd = this.days + this.frac;
 56     },
 57 
 58     /**
 59      * Return the fractional portion of this Julian Day instance. This portion
 60      * corresponds to the time of day for the instance.
 61      */
 62     getDayFraction: function() {
 63         return this.frac;
 64     },
 65 
 66     /**
 67      * Set the fractional part of the Julian Day. The fractional part represents
 68      * the portion of a fully day. Julian dates start at noon, and proceed until
 69      * noon of the next day. That would mean midnight is represented as a fractional
 70      * part of 0.5.
 71      *
 72      * @param {number} fraction The fractional part of the Julian date
 73      */
 74     setDayFraction: function(fraction) {
 75         var t = Math.floor(fraction);
 76         this.frac = fraction - t;
 77         this.jd = this.days + this.frac;
 78     },
 79 
 80     /**
 81      * Return the Julian Day expressed as a floating point number.
 82      * @return {number} the Julian Day as a number
 83      */
 84     getDate: function () {
 85         return this.jd;
 86     },
 87 
 88     /**
 89      * Set the date of this Julian Day instance.
 90      *
 91      * @param {number} num the numeric Julian Day to set into this instance
 92      */
 93     setDate: function (num) {
 94         this.jd = num;
 95     },
 96 
 97     /**
 98      * Add an offset to the current date instance. The offset should be expressed in
 99      * terms of Julian days. That is, each integral unit represents one day of time, and
100      * fractional part represents a fraction of a regular 24-hour day.
101      *
102      * @param {number} offset an amount to add (or subtract) to the current result instance.
103      */
104     addDate: function(offset) {
105         if (typeof(offset) === 'number') {
106             this.jd += offset;
107             this.days = Math.floor(this.jd);
108             this.frac = this.jd - this.days;
109         }
110     }
111 };
112 
113 module.exports = JulianDay;
114