1 /*
  2  * CopticDate.js - Represent a date in the Coptic calendar
  3  *
  4  * Copyright © 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 ilib = require("../index.js");
 21 var MathUtils = require("./MathUtils.js");
 22 
 23 var Locale = require("./Locale.js");
 24 var IDate = require("./IDate.js");
 25 
 26 var EthiopicDate = require("./EthiopicDate.js");
 27 var CopticCal = require("./CopticCal.js");
 28 var CopticRataDie = require("./CopticRataDie.js");
 29 
 30 /**
 31  * @class
 32  * Construct a new date object for the Coptic Calendar. The constructor can be called
 33  * with a parameter object that contains any of the following properties:
 34  *
 35  * <ul>
 36  * <li><i>unixtime<i> - sets the time of this instance according to the given
 37  * unix time. Unix time is the number of milliseconds since midnight on Jan 1, 1970 (Gregorian).
 38  * <li><i>julianday</i> - the Julian Day to set into this date
 39  * <li><i>year</i> - any integer
 40  * <li><i>month</i> - 1 to 13, where 1 means Thoout, 2 means Paope, etc., and 13 means Epagomene
 41  * <li><i>day</i> - 1 to 30
 42  * <li><i>hour</i> - 0 to 23. A formatter is used to display 12 hour clocks, but this representation
 43  * is always done with an unambiguous 24 hour representation
 44  * <li><i>minute</i> - 0 to 59
 45  * <li><i>second</i> - 0 to 59
 46  * <li><i>millisecond<i> - 0 to 999
 47  * <li><i>locale</i> - the TimeZone instance or time zone name as a string
 48  * of this coptic date. The date/time is kept in the local time. The time zone
 49  * is used later if this date is formatted according to a different time zone and
 50  * the difference has to be calculated, or when the date format has a time zone
 51  * component in it.
 52  * <li><i>timezone</i> - the time zone of this instance. If the time zone is not
 53  * given, it can be inferred from this locale. For locales that span multiple
 54  * time zones, the one with the largest population is chosen as the one that
 55  * represents the locale.
 56  *
 57  * <li><i>date</i> - use the given intrinsic Javascript date to initialize this one.
 58  * </ul>
 59  *
 60  * If called with another Coptic date argument, the date components of the given
 61  * date are copied into the current one.<p>
 62  *
 63  * If the constructor is called with no arguments at all or if none of the
 64  * properties listed above
 65  * from <i>unixtime</i> through <i>millisecond</i> are present, then the date
 66  * components are
 67  * filled in with the current date at the time of instantiation. Note that if
 68  * you do not give the time zone when defaulting to the current time and the
 69  * time zone for all of ilib was not set with <i>ilib.setTimeZone()</i>, then the
 70  * time zone will default to UTC ("Universal Time, Coordinated" or "Greenwich
 71  * Mean Time").<p>
 72  *
 73  *
 74  * @constructor
 75  * @extends EthiopicDate
 76  * @param {Object=} params parameters that govern the settings and behaviour of this Coptic date
 77  */
 78 var CopticDate = function(params) {
 79     this.rd = NaN; // clear these out so that the EthiopicDate constructor can set it
 80     var newparams = ilib.extend({}, params);
 81     newparams.onLoad = function(ed) {
 82         ed.cal = new CopticCal();
 83         if (typeof(params.onLoad) === "function") {
 84             params.onLoad(ed);
 85         }
 86     };
 87     EthiopicDate.call(this, params);
 88 };
 89 
 90 CopticDate.prototype = new EthiopicDate({noinstance: true});
 91 CopticDate.prototype.parent = EthiopicDate.prototype;
 92 CopticDate.prototype.constructor = CopticDate;
 93 
 94 /**
 95  * Return a new RD for this date type using the given params.
 96  * @protected
 97  * @param {Object=} params the parameters used to create this rata die instance
 98  * @returns {RataDie} the new RD instance for the given params
 99  */
100 CopticDate.prototype.newRd = function (params) {
101     return new CopticRataDie(params);
102 };
103 
104 /**
105  * Return the day of the week of this date. The day of the week is encoded
106  * as number from 0 to 6, with 0=Sunday, 1=Monday, etc., until 6=Saturday.
107  *
108  * @return {number} the day of the week
109  */
110 CopticDate.prototype.getDayOfWeek = function() {
111     var rd = Math.floor(this.rd.getRataDie() + (this.offset || 0));
112     return MathUtils.mod(rd-3, 7);
113 };
114 
115 /**
116  * Return the name of the calendar that governs this date.
117  *
118  * @return {string} a string giving the name of the calendar
119  */
120 CopticDate.prototype.getCalendar = function() {
121     return "coptic";
122 };
123 
124 //register with the factory method
125 IDate._constructors["coptic"] = CopticDate;
126 
127 module.exports = CopticDate;
128