Skip to content

Commit

Permalink
Merge pull request #3 from Louis-Aime/2017-6m-version
Browse files Browse the repository at this point in the history
2017 6m version: this version was tested.
  • Loading branch information
Louis-Aime authored Jun 13, 2017
2 parents 89758a6 + 7ea9d9b commit 2bf276b
Show file tree
Hide file tree
Showing 8 changed files with 575 additions and 292 deletions.
121 changes: 33 additions & 88 deletions MilesianCompose.js → CalendarCycleComputationEngine.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
/* Milesian Compose and decompose routines.
/* The Calendar Cycle Computation Engine (CCCE)
// (Formerly Milesian Compose and Decompose routines).
// Character set is UTF-8
//
// The function of this package performs decompositions suitable to calendar computations.
// The functions of this package performs intercalation computations for calendars
// that set intercalation elements following regular cycles.
// The Milesian calendar, given its regulars month, is able to use totally this principle.
// For other calendars, including Gregorian and Julian, this routines may be used to compute
// the rank of a day within a year, and then hours, minutes, seconds and milliseconds.
// Computations on months require more specific algorithms.
// The principles of these routines are explained in "L'heure milésienne",
// a book by Louis-Aimé de Fouquières.
//
// Version 2 : M2017-06-21
// Version 2:
// Changed the names of the functions
// The parameters are now in each package. Only very common parameters and variables are defined here.
//
*//////////////////////////////////////////////////////////////////////////////////////////////
/* Copyright Miletus 2016-2017 - Louis A. de Fouquières
// Permission is hereby granted, free of charge, to any person obtaining
Expand All @@ -30,12 +38,18 @@
// or the use or other dealings in the software.
// Inquiries: www.calendriermilesien.org
*////////////////////////////////////////////////////////////////////////////////
var Chronos = { // Set of chronological constants generally used when handling Unix dates.
DAY_UNIT : 86400000, // One day in Unix time units
HOUR_UNIT : 3600000,
MINUTE_UNIT : 60000,
SECOND_UNIT : 1000
}
/* Parameter object structure. Replace # with numbers or literals.
const decomposeParameterExample = {
var decomposeParameterExample = {
timeepoch : #, origin time in milliseconds (or in the suitable unit) to be used for the decomposition, with respect to 1/1/1970 00:00 UTC.
coeff : [ // This array holds the coefficient used to decompose a time stamp into time cycles like eras, quadrisaeculae, centuries etc.
{cyclelength : #, //length of the cycle, expressed in milliseconds.
ceiling : #, // Infinity, or the maximum number of cycles of this size in the upper cycle; the last cycle may hold an intercalary unit.
ceiling : #, // Infinity, or the maximum number of cycles of this size minus one in the upper cycle; the last cycle may hold an intercalary unit.
multiplier : #, // multiplies the number of cycle of this level to convert into target units.
targer : #, // the unit (e.g. "year") of the decomposition element at this level.
} ,
Expand All @@ -48,9 +62,13 @@ const decomposeParameterExample = {
} // End of array element (only two properties)
] // End of second array
} // End of object.
// Constraints:
// 1. The cycles and the canvas elements shall be definined from the larger to the smaller
// e.g. quadrisaeculum, then century, then quadriannum, then year, etc.
// 2. The same names shall bu used for the "coeff" and the "canvas" properties, elsewise applications may return "NaN".
//
*/
var // A selection of composition objects
// Former Unix_Day_Time_Coeff
var
Day_milliseconds = { // To convert a time or a duration to and from days + milliseconds in day.
timeepoch : 0,
coeff : [ // to be used with a Unix timestamp in ms. Decompose into days and milliseconds in day.
Expand All @@ -61,93 +79,20 @@ Day_milliseconds = { // To convert a time or a duration to and from days + mill
{name : "day_number", init : 0},
{name : "milliseconds_in_day", init : 0},
]
} ,
Milesian_time_params = { // To be used with a Unix timestamp in ms. Decompose into Milesian years, months, date, hours, minutes, seconds, ms
timeepoch : -188395804800000, // Unix timestamp of 1 1m -4000 00h00 UTC in ms
coeff : [
{cyclelength : 100982160000000, ceiling : Infinity, multiplier : 3200, target : "year"},
{cyclelength : 12622780800000, ceiling : Infinity, multiplier : 400, target : "year"},
{cyclelength : 3155673600000, ceiling : 3, multiplier : 100, target : "year"},
{cyclelength : 126230400000, ceiling : Infinity, multiplier : 4, target : "year"},
{cyclelength : 31536000000, ceiling : 3, multiplier : 1, target : "year"},
{cyclelength : 5270400000, ceiling : Infinity, multiplier : 2, target : "month"},
{cyclelength : 2592000000, ceiling : 1, multiplier : 1, target : "month"},
{cyclelength : 86400000, ceiling : Infinity, multiplier : 1, target : "date"},
{cyclelength : 3600000, ceiling : Infinity, multiplier : 1, target : "hours"},
{cyclelength : 60000, ceiling : Infinity, multiplier : 1, target : "minutes"},
{cyclelength : 1000, ceiling : Infinity, multiplier : 1, target : "seconds"},
{cyclelength : 1, ceiling : Infinity, multiplier : 1, target : "milliseconds"}
],
canvas : [
{name : "year", init : -4000},
{name : "month", init : 0},
{name : "date", init : 1},
{name : "hours", init : 0},
{name : "minutes", init : 0},
{name : "seconds", init : 0},
{name : "milliseconds", init : 0},
]
} ,
//
// Former Milesian_date_coeff and canvas
//
Milesian_date_JD_params = { // To be used with a timestamp in integer days. Decomposes into year, month, date.
timeepoch : 260081, // 1 1m -4000 in Julian Day
coeff : [
{cyclelength : 1168775, ceiling : Infinity, multiplier : 3200, target : "year"},
{cyclelength : 146097, ceiling : Infinity, multiplier : 400, target : "year"},
{cyclelength : 36524, ceiling : 3, multiplier : 100, target : "year"},
{cyclelength : 1461, ceiling : Infinity, multiplier : 4, target : "year"},
{cyclelength : 365, ceiling : 3, multiplier : 1, target : "year"},
{cyclelength : 61, ceiling : Infinity, multiplier : 2, target : "month"},
{cyclelength : 30, ceiling : 1, multiplier : 1, target : "month"},
{cyclelength : 1, ceiling : Infinity, multiplier : 1, target : "date"}
],
canvas : [ // Timestamp is 0 on 1 1m -4000, month is in the JS way (0 to 11), date starts at 1.
{name : "year", init : -4000},
{name : "month", init : 0},
{name : "date", init : 1}
]
} ,
CE_Moon_params = { // to be used with a Unix timestamp in ms. Decompose into moon years, moon months and moon age.
timeepoch : -62167873955000, // from the mean new moon of 3 1m 0 at 10:07:25 Terrestrial Time.
coeff : [
{cyclelength : 30617314500, ceiling : Infinity, multiplier : 1, target : "year"}, // this cycle length is 12 mean lunar months
{cyclelength : 2551442875, ceiling : Infinity, multiplier : 1, target : "month"}, // this cycle length is one mean lunar month
{cyclelength : 86400000, ceiling : Infinity, multiplier : 1, target : "age"},
{cyclelength : 1, ceiling : Infinity, multiplier : 1.157407407E-8, target : "age"}
],
canvas : [
{name : "year", init : 0},
{name : "month", init : 0},
{name : "age", init : 0}
]
} ,
Lunar_Year_Month_Params = { // to be used in order to change lunar calendar epoch, without changing lunar age.
// Usage of this parameter set: change between Common Era and Hegirian moon calendar, 7688 lunar month offset.
timeepoch : 0, // put the timeepoch in the parameter call.
coeff : [
{cyclelength : 12, ceiling : Infinity, multiplier : 1, target : "year"},
{cyclelength : 1, ceiling : Infinity, multiplier : 1, target : "month"}
],
canvas : [
{name : "year", init :0},
{name : "month", init : 0}
]
}
/////////////////////////////////////////////
// milesianDecompose : from time serie figure to decomposition
// ccceDecompose : from time serie figure to decomposition
/////////////////////////////////////////////
function milesianDecompose (quantity, params) { // from a chronological number, build an compound object holding the elements as required by cparams.
function ccceDecompose (quantity, params) { // from a chronological number, build an compound object holding the elements as required by cparams.
quantity -= params.timeepoch; // set quantity to decompose into cycles to the right value.
var result = new Object(); // Construct intitial result
for (let i = 0; i < params.canvas.length; i++) {
for (let i = 0; i < params.canvas.length; i++) { // Define property of result object (a date or date-time)
Object.defineProperty (result, params.canvas[i].name, {enumerable : true, writable : true, value : params.canvas[i].init});
}
for (let i = 0; i < params.coeff.length; ++i) {
let r = 0; // r is computed quotient for this level of decomposition
for (let i = 0; i < params.coeff.length; ++i) { // Perform decomposition by dividing by the successive cycle length
let r = 0; // r is the computed quotient for this level of decomposition
if (params.coeff[i].cyclelength == 1) r = quantity; // avoid performing a trivial divison by 1.
else {
else { // at each level, search at the same time the quotient (r) and the modulus (quantity)
while (quantity < 0) {
--r;
quantity += params.coeff[i].cyclelength;
Expand All @@ -162,9 +107,9 @@ function milesianDecompose (quantity, params) { // from a chronological number,
return result;
}
////////////////////////////////////////////
// milesianCompose: from compound object to time serie figure.
// ccceCompose: from compound object to time serie figure.
////////////////////////////////////////////
function milesianCompose (cells, params) { // from an object structured as params.canvas, compute the chronological number
function ccceCompose (cells, params) { // from an object structured as params.canvas, compute the chronological number
var quantity = params.timeepoch ; // initialise quantity
for (let i = 0; i < params.canvas.length; i++) { // cells value shifted as to have all 0 if at epoch
cells[params.canvas[i].name] -= params.canvas[i].init
Expand All @@ -177,4 +122,4 @@ function milesianCompose (cells, params) { // from an object structured as param
quantity += Math.floor(cells[params.coeff[i].target] / params.coeff[i].multiplier) * factor;
}
return quantity ;
}
}
111 changes: 111 additions & 0 deletions IsoWeekCalendarDateProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* ISO week calendar properties added to Date object
// Character set is UTF-8
// This code, to be manually imported, set properties to object Date for the ISO week calendar, which is the calendar implied by ISO 8601.
// Version M2017-06-22
// Package CalendarCycleComputationEngine is used.
// Package MilesianDateProperties is used (in order to compute quickly year of date to be converted into ISO week calendar)
// getIsoWeekCalDate : the day date as a three elements object: .year, .month, .date; .month is 0 to 11. Conversion is in local time.
// getIsoWeekCalUTCDate : same as above, in UTC time.
// setTimeFromIsoWeekCal (year, month, date, hours, minutes, seconds, milliseconds) : set Time from milesian date + local hour.
// setUTCTimeFromIsoWeekCal (year, month, date, hours, minutes, seconds, milliseconds) : same but from UTC time zone.
// toIsoWeekCalDateString : return a string with the date elements in IsoWeekCal: yyyy-Www-dd
// toUTCIsoWeekCalDateString : same as above, in UTC time zone.
*/////////////////////////////////////////////////////////////////////////////////////////////
/* Copyright Miletus 2016-2017 - Louis A. de Fouquières
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 1. The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
// 2. Changes with respect to any former version shall be documented.
//
// The software is provided "as is", without warranty of any kind,
// express of implied, including but not limited to the warranties of
// merchantability, fitness for a particular purpose and noninfringement.
// In no event shall the authors of copyright holders be liable for any
// claim, damages or other liability, whether in an action of contract,
// tort or otherwise, arising from, out of or in connection with the software
// or the use or other dealings in the software.
// Inquiries: www.calendriermilesien.org
*////////////////////////////////////////////////////////////////////////////////
//
// 1. Basic tools of this package
/* Import CalendarCycleComputationEngine, or make visible. */
/* Import MilseianDateProperties, or make visible. */
var
isoWeekCalendar_params = { // To be used with a Unix timestamp in ms. Decompose a delay within a year in week number and day.
timeepoch : 0, //
coeff : [
{cyclelength : 604800000, ceiling : Infinity, multiplier : 1, target : "week"},
{cyclelength : 86400000, ceiling : Infinity, multiplier : 1, target : "day"},
{cyclelength : 3600000, ceiling : Infinity, multiplier : 1, target : "hours"},
{cyclelength : 60000, ceiling : Infinity, multiplier : 1, target : "minutes"},
{cyclelength : 1000, ceiling : Infinity, multiplier : 1, target : "seconds"},
{cyclelength : 1, ceiling : Infinity, multiplier : 1, target : "milliseconds"}
],
canvas : [
{name : "week", init : 1},
{name : "day", init : 1},
{name : "hours", init : 0},
{name : "minutes", init : 0},
{name : "seconds", init : 0},
{name : "milliseconds", init : 0},
]
};
//
// 2. Properties added to Date object for ISO week calendar
//
function isoWeekCalendarBase (year) { // yields the monday 00:00 first moment of the first ISO week in given year.
let referenceDate = new Date (Date.UTC(year, 0, 4)); // 4th January is always in first ISO week of year.
referenceDate.setFullYear(year); // Year is always a full year, 98 means 98 years A.D., not 1998.
return referenceDate.valueOf() - ((referenceDate.getUTCDay() + 6) % 7)* Chronos.DAY_UNIT // Substract weekday number - 1 (+6) modulo 7 in order to come back to monday
}
Date.prototype.getIsoWeekCalDate = function () {
let year = this.getMilesianDate().year; // Milesian year begins always before ISO week year, therefore ISO week calendar year is either Milesian year, either - 1.
let base = new Date (isoWeekCalendarBase(year)); // This is the first day of ISO week year.
if (base.valueOf() > this.valueOf()-(this.getTimezoneOffset() * Chronos.MINUTE_UNIT)) base.setTime (isoWeekCalendarBase(--year));
let compoundDate = ccceDecompose (this.valueOf() - this.getTimezoneOffset() * Chronos.MINUTE_UNIT - base.valueOf(), isoWeekCalendar_params);
Object.defineProperty (compoundDate, "year", {enumerable : true, writable : true, value : year});
return compoundDate;
}
Date.prototype.getIsoWeekCalUTCDate = function () {
let year = this.getMilesianUTCDate().year; // Milesian year begins always before ISO week year, therefore ISO week calendar year is either Milesian year, either - 1.
let base = new Date (isoWeekCalendarBase(year)); // This is the first day of ISO week year.
if (base.valueOf() > this.valueOf()) base.setTime (isoWeekCalendarBase(--year));
let compoundDate = ccceDecompose (this.valueOf() - base.valueOf(), isoWeekCalendar_params);
Object.defineProperty (compoundDate, "year", {enumerable : true, writable : true, value : year});
return compoundDate;
}
Date.prototype.setTimeFromIsoWeekCal = function (year, week, day,
hours = this.getHours(), minutes = this.getMinutes(), seconds = this.getSeconds(),
milliseconds = this.getMilliseconds()) { // set time from date expressed in ISO week calendar
this.setTime(isoWeekCalendarBase(year) + ccceCompose({ // Set ISO week calendar date at 00:00 UTC. The year begins at isoWeekCalendarBase.
'week' : week, 'day' : day, 'hours' : 0, 'minutes' : 0, 'seconds' : 0, 'milliseconds' : 0 }, isoWeekCalendar_params));
this.setHours (hours, minutes, seconds, milliseconds); // Then set hour of the day
return this.valueOf();
}
Date.prototype.setUTCTimeFromIsoWeekCal = function (year, week, day,
hours = this.getUTCHours(), minutes = this.getUTCMinutes(), seconds = this.getUTCSeconds(),
milliseconds = this.getUTCMilliseconds()) { // set time from date expressed in ISO week calendar
this.setTime(isoWeekCalendarBase (year) + ccceCompose({
'week' : week, 'day' : day, 'hours' : hours, 'minutes' : minutes, 'seconds' : seconds,
'milliseconds' : milliseconds
}, isoWeekCalendar_params));
return this.valueOf();
}
Date.prototype.toUTCIsoWeekCalString = function () { //return a string with the date elements in IsoWeekCal: yyyy-Www-dd
var dateElements = this.getIsoWeekCalUTCDate();
let absYear = Math.abs(dateElements.year)
return ((dateElements.year < 0) ? "-": "")
+ ((absYear < 100) ? "0" : "") + ((absYear < 10) ? "0" : "") + absYear
+"-W" + ((dateElements.week < 10) ? "0" : "") + (dateElements.week) + "-0"+dateElements.day
+"T"+((dateElements.hours < 10) ? "0" : "") + dateElements.hours + ":"
+ ((dateElements.minutes < 10) ? "0" : "") + dateElements.minutes + ":"
+ ((dateElements.seconds < 10) ? "0" : "") + dateElements.seconds + ":"
+ ((dateElements.milliseconds < 100) ? "0" : "") + ((dateElements.milliseconds < 10) ? "0" : "")
+ dateElements.milliseconds + "Z";
}
Loading

0 comments on commit 2bf276b

Please sign in to comment.