1 /*
  2  * JulianCal.js - Represent a Julian calendar object.
  3  *
  4  * Copyright © 2012-2017, 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 MathUtils = require("./MathUtils.js");
 21 var Calendar = require("./Calendar.js");
 22 
 23 /**
 24  * @class
 25  * Construct a new Julian calendar object. This class encodes information about
 26  * a Julian calendar.<p>
 27  *
 28  * @param {Object=} options Options governing the construction of this instance
 29  * @constructor
 30  * @extends Calendar
 31  */
 32 var JulianCal = function(options) {
 33     this.type = "julian";
 34 
 35     if (options && typeof(options.onLoad) === "function") {
 36         options.onLoad(this);
 37     }
 38 };
 39 
 40 /* the lengths of each month */
 41 JulianCal.monthLengths = [
 42     31,  /* Jan */
 43     28,  /* Feb */
 44     31,  /* Mar */
 45     30,  /* Apr */
 46     31,  /* May */
 47     30,  /* Jun */
 48     31,  /* Jul */
 49     31,  /* Aug */
 50     30,  /* Sep */
 51     31,  /* Oct */
 52     30,  /* Nov */
 53     31   /* Dec */
 54 ];
 55 
 56 /**
 57  * the cumulative lengths of each month, for a non-leap year
 58  * @private
 59  * @const
 60  * @type Array.<number>
 61  */
 62 JulianCal.cumMonthLengths = [
 63     0,   /* Jan */
 64     31,  /* Feb */
 65     59,  /* Mar */
 66     90,  /* Apr */
 67     120, /* May */
 68     151, /* Jun */
 69     181, /* Jul */
 70     212, /* Aug */
 71     243, /* Sep */
 72     273, /* Oct */
 73     304, /* Nov */
 74     334, /* Dec */
 75     365
 76 ];
 77 
 78 /**
 79  * the cumulative lengths of each month, for a leap year
 80  * @private
 81  * @const
 82  * @type Array.<number>
 83  */
 84 JulianCal.cumMonthLengthsLeap = [
 85     0,   /* Jan */
 86     31,  /* Feb */
 87     60,  /* Mar */
 88     91,  /* Apr */
 89     121, /* May */
 90     152, /* Jun */
 91     182, /* Jul */
 92     213, /* Aug */
 93     244, /* Sep */
 94     274, /* Oct */
 95     305, /* Nov */
 96     335, /* Dec */
 97     366
 98 ];
 99 
100 /**
101  * Return the number of months in the given year. The number of months in a year varies
102  * for lunar calendars because in some years, an extra month is needed to extend the
103  * days in a year to an entire solar year. The month is represented as a 1-based number
104  * where 1=Jaunary, 2=February, etc. until 12=December.
105  *
106  * @param {number} year a year for which the number of months is sought
107  */
108 JulianCal.prototype.getNumMonths = function(year) {
109     return 12;
110 };
111 
112 /**
113  * Return the number of days in a particular month in a particular year. This function
114  * can return a different number for a month depending on the year because of things
115  * like leap years.
116  *
117  * @param {number} month the month for which the length is sought
118  * @param {number} year the year within which that month can be found
119  * @return {number} the number of days within the given month in the given year
120  */
121 JulianCal.prototype.getMonLength = function(month, year) {
122     if (month !== 2 || !this.isLeapYear(year)) {
123         return JulianCal.monthLengths[month-1];
124     } else {
125         return 29;
126     }
127 };
128 
129 /**
130  * Return true if the given year is a leap year in the Julian calendar.
131  * The year parameter may be given as a number, or as a JulDate object.
132  * @param {number|JulianDate} year the year for which the leap year information is being sought
133  * @return {boolean} true if the given year is a leap year
134  */
135 JulianCal.prototype.isLeapYear = function(year) {
136     var y = (typeof(year) === 'number' ? year : year.year);
137     return MathUtils.mod(y, 4) === ((year > 0) ? 0 : 3);
138 };
139 
140 /**
141  * Return the type of this calendar.
142  *
143  * @return {string} the name of the type of this calendar
144  */
145 JulianCal.prototype.getType = function() {
146     return this.type;
147 };
148 
149 
150 /* register this calendar for the factory method */
151 Calendar._constructors["julian"] = JulianCal;
152 
153 module.exports = JulianCal;
154