1 /*
  2  * JulianDate.js - Represent a date in the Julian calendar
  3  *
  4  * Copyright © 2012-2015, 2018, 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 var RataDie = require("./RataDie.js");
 21 var JulianCal = require("./JulianCal.js");
 22 
 23 /**
 24  * @class
 25  * Construct a new Julian RD date number object. The constructor parameters can
 26  * contain any of the following properties:
 27  *
 28  * <ul>
 29  * <li><i>unixtime<i> - sets the time of this instance according to the given
 30  * unix time. Unix time is the number of milliseconds since midnight on Jan 1, 1970.
 31  *
 32  * <li><i>julianday</i> - sets the time of this instance according to the given
 33  * Julian Day instance or the Julian Day given as a float
 34  *
 35  * <li><i>year</i> - any integer, including 0
 36  *
 37  * <li><i>month</i> - 1 to 12, where 1 means January, 2 means February, etc.
 38  *
 39  * <li><i>day</i> - 1 to 31
 40  *
 41  * <li><i>hour</i> - 0 to 23. A formatter is used to display 12 hour clocks, but this representation
 42  * is always done with an unambiguous 24 hour representation
 43  *
 44  * <li><i>minute</i> - 0 to 59
 45  *
 46  * <li><i>second</i> - 0 to 59
 47  *
 48  * <li><i>millisecond</i> - 0 to 999
 49  *
 50  * <li><i>date</i> - use the given intrinsic Javascript date to initialize this one.
 51  * </ul>
 52  *
 53  * If the constructor is called with another Julian date instance instead of
 54  * a parameter block, the other instance acts as a parameter block and its
 55  * settings are copied into the current instance.<p>
 56  *
 57  * If the constructor is called with no arguments at all or if none of the
 58  * properties listed above are present, then the RD is calculate based on
 59  * the current date at the time of instantiation. <p>
 60  *
 61  * If any of the properties from <i>year</i> through <i>millisecond</i> are not
 62  * specified in the params, it is assumed that they have the smallest possible
 63  * value in the range for the property (zero or one).<p>
 64  *
 65  *
 66  * @private
 67  * @constructor
 68  * @extends RataDie
 69  * @param {Object=} params parameters that govern the settings and behaviour of this Julian RD date
 70  */
 71 var JulianRataDie = function(params) {
 72     this.cal = params && params.cal || new JulianCal();
 73     this.rd = NaN;
 74     RataDie.call(this, params);
 75 };
 76 
 77 JulianRataDie.prototype = new RataDie();
 78 JulianRataDie.prototype.parent = RataDie;
 79 JulianRataDie.prototype.constructor = JulianRataDie;
 80 
 81 /**
 82  * The difference between a zero Julian day and the first Julian date
 83  * of Friday, July 16, 622 CE Julian.
 84  * @private
 85  * @type number
 86  */
 87 JulianRataDie.prototype.epoch = 1721422.5;
 88 
 89 /**
 90  * Calculate the Rata Die (fixed day) number of the given date from the
 91  * date components.
 92  *
 93  * @protected
 94  * @param {Object} date the date components to calculate the RD from
 95  */
 96 JulianRataDie.prototype._setDateComponents = function(date) {
 97     var year = date.year + ((date.year < 0) ? 1 : 0);
 98     var years = 365 * (year - 1) + Math.floor((year-1)/4);
 99     var dayInYear = (date.month > 1 ? JulianCal.cumMonthLengths[date.month-1] : 0) +
100         date.day +
101         (this.cal.isLeapYear(date.year) && date.month > 2 ? 1 : 0);
102     var rdtime = (date.hour * 3600000 +
103         date.minute * 60000 +
104         date.second * 1000 +
105         date.millisecond) /
106         86400000;
107 
108     /*
109     console.log("calcRataDie: converting " +  JSON.stringify(parts));
110     console.log("getRataDie: year is " +  years);
111     console.log("getRataDie: day in year is " +  dayInYear);
112     console.log("getRataDie: rdtime is " +  rdtime);
113     console.log("getRataDie: rd is " +  (years + dayInYear + rdtime));
114     */
115 
116     this.rd = years + dayInYear + rdtime;
117 };
118 
119 module.exports = JulianRataDie;