1 /*
  2  * EthiopicCal.js - Represent a Ethiopic calendar object.
  3  *
  4  * Copyright © 2015-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 
 22 var Calendar = require("./Calendar.js");
 23 
 24 /**
 25  * @class
 26  * Construct a new Ethiopic calendar object. This class encodes information about
 27  * a Ethiopic calendar.<p>
 28  *
 29  * @param {Object=} options Options governing the construction of this instance
 30  * @constructor
 31  * @extends Calendar
 32  */
 33 var EthiopicCal = function(options) {
 34     this.type = "ethiopic";
 35 
 36     if (options && typeof(options.onLoad) === "function") {
 37         options.onLoad(this);
 38     }
 39 };
 40 
 41 /**
 42  * Return the number of months in the given year. The number of months in a year varies
 43  * for lunar calendars because in some years, an extra month is needed to extend the
 44  * days in a year to an entire solar year. The month is represented as a 1-based number
 45  * where 1=Maskaram, 2=Teqemt, etc. until 13=Paguemen.
 46  *
 47  * @param {number} year a year for which the number of months is sought
 48  */
 49 EthiopicCal.prototype.getNumMonths = function(year) {
 50     return 13;
 51 };
 52 
 53 /**
 54  * Return the number of days in a particular month in a particular year. This function
 55  * can return a different number for a month depending on the year because of things
 56  * like leap years.
 57  *
 58  * @param {number|string} month the month for which the length is sought
 59  * @param {number} year the year within which that month can be found
 60  * @return {number} the number of days within the given month in the given year
 61  */
 62 EthiopicCal.prototype.getMonLength = function(month, year) {
 63     var m = month;
 64     switch (typeof(m)) {
 65         case "string":
 66             m = parseInt(m, 10);
 67             break;
 68         case "function":
 69         case "object":
 70         case "undefined":
 71             return 30;
 72     }
 73     if (m < 13) {
 74         return 30;
 75     } else {
 76         return this.isLeapYear(year) ? 6 : 5;
 77     }
 78 };
 79 
 80 /**
 81  * Return true if the given year is a leap year in the Ethiopic calendar.
 82  * The year parameter may be given as a number, or as a JulDate object.
 83  * @param {number|EthiopicDate|string} year the year for which the leap year information is being sought
 84  * @return {boolean} true if the given year is a leap year
 85  */
 86 EthiopicCal.prototype.isLeapYear = function(year) {
 87     var y = year;
 88      switch (typeof(y)) {
 89         case "string":
 90             y = parseInt(y, 10);
 91             break;
 92         case "object":
 93             if (typeof(y.year) !== "number") { // in case it is an IDate object
 94                 return false;
 95             }
 96             y = y.year;
 97             break;
 98         case "function":
 99         case "undefined":
100             return false;
101     }
102     return MathUtils.mod(y, 4) === 3;
103 };
104 
105 /**
106  * Return the type of this calendar.
107  *
108  * @return {string} the name of the type of this calendar
109  */
110 EthiopicCal.prototype.getType = function() {
111     return this.type;
112 };
113 
114 
115 /* register this calendar for the factory method */
116 Calendar._constructors["ethiopic"] = EthiopicCal;
117 
118 module.exports = EthiopicCal;
119