1 /*
  2  * IslamicRataDie.js - Represent an RD date in the Islamic 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 IslamicCal = require("./IslamicCal.js");
 22 
 23 /**
 24  * @class
 25  * Construct a new Islamic 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 Islamic 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 Islamic RD date
 70  */
 71 var IslamicRataDie = function(params) {
 72     this.cal = params && params.cal || new IslamicCal();
 73     this.rd = NaN;
 74     RataDie.call(this, params);
 75 };
 76 
 77 IslamicRataDie.prototype = new RataDie();
 78 IslamicRataDie.prototype.parent = RataDie;
 79 IslamicRataDie.prototype.constructor = IslamicRataDie;
 80 
 81 /**
 82  * The difference between a zero Julian day and the first Islamic date
 83  * of Friday, July 16, 622 CE Julian.
 84  * @private
 85  * @type number
 86  */
 87 IslamicRataDie.prototype.epoch = 1948439.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 IslamicRataDie.prototype._setDateComponents = function(date) {
 97     var days = (date.year - 1) * 354 +
 98         Math.ceil(29.5 * (date.month - 1)) +
 99         date.day +
100         Math.floor((3 + 11 * date.year) / 30) - 1;
101     var time = (date.hour * 3600000 +
102         date.minute * 60000 +
103         date.second * 1000 +
104         date.millisecond) /
105         86400000;
106 
107     //console.log("getRataDie: converting " +  JSON.stringify(date));
108     //console.log("getRataDie: days is " +  days);
109     //console.log("getRataDie: time is " +  time);
110     //console.log("getRataDie: rd is " +  (days + time));
111 
112     this.rd = days + time;
113 };
114 
115 module.exports = IslamicRataDie;