1 /*
  2  * GregorianCal.js - Represent a Gregorian calendar object.
  3  *
  4  * Copyright © 2012-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 
 21 var Utils = require("./Utils.js");
 22 var MathUtils = require("./MathUtils.js");
 23 var Calendar = require("./Calendar.js");
 24 
 25 /**
 26  * @class
 27  * Construct a new Gregorian calendar object. This class encodes information about
 28  * a Gregorian calendar.<p>
 29  *
 30  *
 31  * @constructor
 32  * @param {{noinstance:boolean}=} options
 33  * @extends Calendar
 34  */
 35 var GregorianCal = function(options) {
 36     if (!options || !options.noinstance) {
 37         this.type = "gregorian";
 38     }
 39 
 40     if (options && typeof(options.onLoad) === "function") {
 41         options.onLoad(this);
 42     }
 43 };
 44 
 45 /**
 46  * the lengths of each month
 47  * @private
 48  * @const
 49  * @type Array.<number>
 50  */
 51 GregorianCal.monthLengths = [
 52     31,  /* Jan */
 53     28,  /* Feb */
 54     31,  /* Mar */
 55     30,  /* Apr */
 56     31,  /* May */
 57     30,  /* Jun */
 58     31,  /* Jul */
 59     31,  /* Aug */
 60     30,  /* Sep */
 61     31,  /* Oct */
 62     30,  /* Nov */
 63     31   /* Dec */
 64 ];
 65 
 66 /**
 67  * Return the number of months in the given year. The number of months in a year varies
 68  * for some luni-solar calendars because in some years, an extra month is needed to extend the
 69  * days in a year to an entire solar year. The month is represented as a 1-based number
 70  * where 1=first month, 2=second month, etc.
 71  *
 72  * @param {number} year a year for which the number of months is sought
 73  * @return {number} The number of months in the given year
 74  */
 75 GregorianCal.prototype.getNumMonths = function(year) {
 76     return 12;
 77 };
 78 
 79 /**
 80  * Return the number of days in a particular month in a particular year. This function
 81  * can return a different number for a month depending on the year because of things
 82  * like leap years.
 83  *
 84  * @param {number} month the month for which the length is sought
 85  * @param {number} year the year within which that month can be found
 86  * @return {number} the number of days within the given month in the given year
 87  */
 88 GregorianCal.prototype.getMonLength = function(month, year) {
 89     if (month !== 2 || !this.isLeapYear(year)) {
 90         return GregorianCal.monthLengths[month-1];
 91     } else {
 92         return 29;
 93     }
 94 };
 95 
 96 /**
 97  * Return true if the given year is a leap year in the Gregorian calendar.
 98  * The year parameter may be given as a number, or as a GregDate object.
 99  * @param {number|GregorianDate} year the year for which the leap year information is being sought
100  * @return {boolean} true if the given year is a leap year
101  */
102 GregorianCal.prototype.isLeapYear = function(year) {
103     var y = (typeof(year) === 'number' ? year : year.getYears());
104     var centuries = MathUtils.mod(y, 400);
105     return (MathUtils.mod(y, 4) === 0 && centuries !== 100 && centuries !== 200 && centuries !== 300);
106 };
107 
108 /**
109  * Return the type of this calendar.
110  *
111  * @return {string} the name of the type of this calendar
112  */
113 GregorianCal.prototype.getType = function() {
114     return this.type;
115 };
116 
117 /* register this calendar for the factory method */
118 Calendar._constructors["gregorian"] = GregorianCal;
119 
120 module.exports = GregorianCal;
121