natural-time-js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/LICENSE ADDED
@@ -0,0 +1,121 @@
1
+ Creative Commons Legal Code
2
+
3
+ CC0 1.0 Universal
4
+
5
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
+ HEREUNDER.
13
+
14
+ Statement of Purpose
15
+
16
+ The laws of most jurisdictions throughout the world automatically confer
17
+ exclusive Copyright and Related Rights (defined below) upon the creator
18
+ and subsequent owner(s) (each and all, an "owner") of an original work of
19
+ authorship and/or a database (each, a "Work").
20
+
21
+ Certain owners wish to permanently relinquish those rights to a Work for
22
+ the purpose of contributing to a commons of creative, cultural and
23
+ scientific works ("Commons") that the public can reliably and without fear
24
+ of later claims of infringement build upon, modify, incorporate in other
25
+ works, reuse and redistribute as freely as possible in any form whatsoever
26
+ and for any purposes, including without limitation commercial purposes.
27
+ These owners may contribute to the Commons to promote the ideal of a free
28
+ culture and the further production of creative, cultural and scientific
29
+ works, or to gain reputation or greater distribution for their Work in
30
+ part through the use and efforts of others.
31
+
32
+ For these and/or other purposes and motivations, and without any
33
+ expectation of additional consideration or compensation, the person
34
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
+ is an owner of Copyright and Related Rights in the Work, voluntarily
36
+ elects to apply CC0 to the Work and publicly distribute the Work under its
37
+ terms, with knowledge of his or her Copyright and Related Rights in the
38
+ Work and the meaning and intended legal effect of CC0 on those rights.
39
+
40
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
41
+ protected by copyright and related or neighboring rights ("Copyright and
42
+ Related Rights"). Copyright and Related Rights include, but are not
43
+ limited to, the following:
44
+
45
+ i. the right to reproduce, adapt, distribute, perform, display,
46
+ communicate, and translate a Work;
47
+ ii. moral rights retained by the original author(s) and/or performer(s);
48
+ iii. publicity and privacy rights pertaining to a person's image or
49
+ likeness depicted in a Work;
50
+ iv. rights protecting against unfair competition in regards to a Work,
51
+ subject to the limitations in paragraph 4(a), below;
52
+ v. rights protecting the extraction, dissemination, use and reuse of data
53
+ in a Work;
54
+ vi. database rights (such as those arising under Directive 96/9/EC of the
55
+ European Parliament and of the Council of 11 March 1996 on the legal
56
+ protection of databases, and under any national implementation
57
+ thereof, including any amended or successor version of such
58
+ directive); and
59
+ vii. other similar, equivalent or corresponding rights throughout the
60
+ world based on applicable law or treaty, and any national
61
+ implementations thereof.
62
+
63
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
64
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
65
+ irrevocably and unconditionally waives, abandons, and surrenders all of
66
+ Affirmer's Copyright and Related Rights and associated claims and causes
67
+ of action, whether now known or unknown (including existing as well as
68
+ future claims and causes of action), in the Work (i) in all territories
69
+ worldwide, (ii) for the maximum duration provided by applicable law or
70
+ treaty (including future time extensions), (iii) in any current or future
71
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
72
+ including without limitation commercial, advertising or promotional
73
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
+ member of the public at large and to the detriment of Affirmer's heirs and
75
+ successors, fully intending that such Waiver shall not be subject to
76
+ revocation, rescission, cancellation, termination, or any other legal or
77
+ equitable action to disrupt the quiet enjoyment of the Work by the public
78
+ as contemplated by Affirmer's express Statement of Purpose.
79
+
80
+ 3. Public License Fallback. Should any part of the Waiver for any reason
81
+ be judged legally invalid or ineffective under applicable law, then the
82
+ Waiver shall be preserved to the maximum extent permitted taking into
83
+ account Affirmer's express Statement of Purpose. In addition, to the
84
+ extent the Waiver is so judged Affirmer hereby grants to each affected
85
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
86
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
87
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
+ maximum duration provided by applicable law or treaty (including future
89
+ time extensions), (iii) in any current or future medium and for any number
90
+ of copies, and (iv) for any purpose whatsoever, including without
91
+ limitation commercial, advertising or promotional purposes (the
92
+ "License"). The License shall be deemed effective as of the date CC0 was
93
+ applied by Affirmer to the Work. Should any part of the License for any
94
+ reason be judged legally invalid or ineffective under applicable law, such
95
+ partial invalidity or ineffectiveness shall not invalidate the remainder
96
+ of the License, and in such case Affirmer hereby affirms that he or she
97
+ will not (i) exercise any of his or her remaining Copyright and Related
98
+ Rights in the Work or (ii) assert any associated claims and causes of
99
+ action with respect to the Work, in either case contrary to Affirmer's
100
+ express Statement of Purpose.
101
+
102
+ 4. Limitations and Disclaimers.
103
+
104
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
+ surrendered, licensed or otherwise affected by this document.
106
+ b. Affirmer offers the Work as-is and makes no representations or
107
+ warranties of any kind concerning the Work, express, implied,
108
+ statutory or otherwise, including without limitation warranties of
109
+ title, merchantability, fitness for a particular purpose, non
110
+ infringement, or the absence of latent or other defects, accuracy, or
111
+ the present or absence of errors, whether or not discoverable, all to
112
+ the greatest extent permissible under applicable law.
113
+ c. Affirmer disclaims responsibility for clearing rights of other persons
114
+ that may apply to the Work or any use thereof, including without
115
+ limitation any person's Copyright and Related Rights in the Work.
116
+ Further, Affirmer disclaims responsibility for obtaining any necessary
117
+ consents, permissions or other rights required for any use of the
118
+ Work.
119
+ d. Affirmer understands and acknowledges that Creative Commons is not a
120
+ party to this document and has no duty or obligation with respect to
121
+ this CC0 or use of the Work.
package/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # Natural time - JavaScript
2
+
3
+ Natural time is a fresh, elegant, and coherent way of measuring the movements of time here on the Earth.
4
+
5
+ This **new time standard** is based on common sense and the observation of natural cycles.
6
+
7
+ ### JavaScript library
8
+
9
+ This repository contains the JavaScript implementation of natural time.
10
+
11
+ `index.js`: This class translates artificial dates to natural dates.
12
+ `context.js`: Collection of utility functions for calculating sky context and events like moon phase, sunrise and sunset ...
13
+
14
+ ### White paper (specifications)
15
+
16
+ How are natural years, moons, days, and time defined ?
17
+
18
+ Read the white paper (specifications) of natural time here: [github.com/sylvain441/natural-time-white-paper](https://github.com/sylvain441/natural-time-white-paper)
19
+
20
+ ### Web application
21
+
22
+ You can play with natural time at [https://naturaltime.app/](https://naturaltime.app/). It both works online and offline thanks to this JavaScript library.
23
+
24
+ ![Natural time app](.github/natural-time-app.jpg)
25
+
26
+ # Javascript API
27
+
28
+ > If you know how to deal with typescript/typings/documentation this repo may appreciate your help. [Contribute here](#contribute)
29
+
30
+ ## Instalation
31
+
32
+ Install NPM package with your terminal:
33
+
34
+ ```console
35
+ npm install -i natural-time-js
36
+ ```
37
+
38
+ ## `NaturalDate` Class
39
+
40
+ The `NaturalDate` class translates artificial dates into natural dates.
41
+
42
+ ```javascript
43
+ import { NaturalDate } from 'natural-time-js';
44
+
45
+ let artificialDate = new Date();
46
+ let longitude = 5.2;
47
+
48
+ let naturalDate = new NaturalDate(artificialDate, longitude);
49
+ ```
50
+
51
+ Once instanciated, the `naturalDate` object holds many computed variables available for your usage:
52
+
53
+ ```javascript
54
+ unixTime; // Artificial gregorian date (UNIX timestamp)
55
+ longitude; // Longitude (between -180° to +180°)
56
+
57
+ year; // Current year (year 1: 2012/2013)
58
+ moon; // Current moon (between 1 and 14)
59
+
60
+ week; // Current week (between 1 and 53)
61
+ weekOfMoon; // Current week of the moon (between 1 and 4)
62
+
63
+ day; // Number of days passed since END_OF_ARTIFICIAL_TIME
64
+ dayOfYear; // Current day of the year (between 1 and 366)
65
+ dayOfMoon; // Current day of the moon (between 1 and 28)
66
+ dayOfWeek; // Current day of the week (between 1 and 7)
67
+
68
+ isRainbowDay; // True if current day is rainbow day
69
+
70
+ time; // Current time (between 0 and 359°999999...)
71
+
72
+ yearStart; // Beginning of the year at the current longitude (UNIX timestamp)
73
+ yearDuration; // Numbers of days in the current year (between 365 and 366)
74
+ nadir; // Beginning of the day at the current longitude (UNIX timestamp)
75
+
76
+ static END_OF_ARTIFICIAL_TIME = 1356091200000; // 2012-12-22 00:00:00 at +180° East;
77
+ static MILLISECONDS_PER_DAY = 86400000; // 24*60*60*1000
78
+ ```
79
+
80
+ **Utility functions**
81
+
82
+ ```javascript
83
+ naturalDate.toString() // "004)04)01 113°00 NT+5.2"
84
+ naturalDate.toDateString(separator = ')') // "004)04)01"
85
+ naturalDate.toTimeString(decimals = 2, rounding = 1) // "113°00"
86
+ naturalDate.toLongitudeString(decimals = 1) // "NT+5.2"
87
+ naturalDate.toYearString() // "004"
88
+ naturalDate.toMoonString() // "04"
89
+ naturalDate.toDayOfMoonString() // "01"
90
+ ```
91
+
92
+ More functions should be available later to make the NaturalDate class API mimic the native javascript Date class (ex: naturalDate.setYear(year)). Your help is welcome for this to happen ;-)
93
+
94
+ ## `NaturalDateContext` functions
95
+
96
+ The NaturalDateContext.js file contains utility functions to bring some astronomical context to the natural date (ex: altitude of the sun, phase of the moon).
97
+
98
+ ```javascript
99
+ import { NaturalSunAltitude, NaturalSunEvents, NaturalMoonPosition, NaturalMoonEvents } from 'natural-time-js/context';
100
+
101
+ // Gizeh pyramide
102
+ let latitude: 29.9791;
103
+ let longitude: 31.1341;
104
+
105
+ let naturalDate = new NaturalDate(new Date(), longitude);
106
+
107
+ NaturalSunAltitude(naturalDate, latitude);
108
+ // altitude: 31.779736404754367,
109
+ // highestAltitude: 62.32114911751647
110
+
111
+ NaturalSunEvents(naturalDate, latitude);
112
+ // sunrise: 70.56708493448893,
113
+ // sunset: 287.9105974344889,
114
+ // nightStart: 306.43507660115563,
115
+ // nightEnd: 52.13781826782227,
116
+ // morningGoldenHour: 80.8551891011556,
117
+ // eveningGoldenHour: 277.58920993448896
118
+
119
+ NaturalMoonPosition(naturalDate, latitude);
120
+ // position: 526.0620760764069,
121
+ // phase: 74.86175469141531,
122
+ // altitude: 64.32391838504756,
123
+ // highestAltitude: 69.53997976716451
124
+
125
+ NaturalMoonEvents(naturalDate, latitude);
126
+ // moonrise: 139.14544743448894,
127
+ // moonset: 10.624722434488932
128
+ ```
129
+
130
+ **Important !**
131
+ Utility functions return time-distance from 0° or false if the event does not occur during this day (ex: no sunrise at the north pole during winter).
132
+
133
+ ## Roadmap
134
+
135
+ 1. Test in real life
136
+ 2. Add typescript compatibility
137
+ 3. Make NaturalDate feel like the Javascript native Date Class by adding features like
138
+ - getTime()
139
+ - setMoon()
140
+ - ...
141
+ 4. Make NaturalTime available for other programming languages
142
+
143
+ # Contribute
144
+
145
+ Natural time is open to contributions from free-thinking minds. Find your way through Github or email at: [sylvain441@proton.me](mailto:sylvain441@proton.me) to get in touch.
146
+
147
+ # License
148
+
149
+ Read [full license](./LICENSE) (Creative Common Zero)
150
+
151
+ Natural time is completely free to use, play, transform, improve... It operates under the law of Love. Follow your heart, fork it, spoon it! There is no need to ask for permission to do anything with it.
152
+
153
+ Natural time is supposed to bring joy and ease to human life, not the opposite. What you make out of it is up to you and the assistance of life forces.
154
+
155
+ # Acknowledgments
156
+
157
+ Natural time has been baking for a long time in a goat's mind while traveling around the world. Infinite gratitude to my beloved friends Uncle Skywalker and Ik: your precious insights into the Mayan universe were so inspiring.
158
+
159
+ Special thanks to Don Cross, author of the Astronomy Library for calculating celestial body events.
160
+ [https://github.com/cosinekitty/astronomy](https://github.com/cosinekitty/astronomy)
161
+
162
+ It was a real pleasure to give birth of natural time during the spring/summer of 2022 in the peaceful French alps.
163
+
164
+ 🌍 One love 🌎
165
+
166
+ 🏔 🐐 🌞 🌈
package/context.js ADDED
@@ -0,0 +1,139 @@
1
+ import { NaturalDate } from './index.js';
2
+ import { Body, Observer, SearchHourAngle, SearchRiseSet, SearchAltitude, MoonPhase, Equator, Horizon } from 'astronomy-engine/astronomy';
3
+
4
+ // Improve performance with cached astronomical calculations
5
+ const SUN_CACHE = {};
6
+ const MOON_CACHE = {};
7
+
8
+ /**
9
+ * SUN ALTITUDE
10
+ *
11
+ * Determine the sun altitude and max altitude from a specific naturalDate at given lat/long coordinates
12
+ * @param {NaturalDate} naturalDate
13
+ * @param {Number} latitude
14
+ * @returns Object { altitude, highestAltitude }
15
+ */
16
+ export function NaturalSunAltitude(naturalDate, latitude) {
17
+
18
+ let observer = new Observer(latitude, naturalDate.longitude, 0);
19
+ let date = new Date(naturalDate.unixTime);
20
+
21
+ let sun = Equator(Body.Sun, date, observer, true, false);
22
+ let sunAltitude = Math.max(Horizon(date, observer, sun.ra, sun.dec).altitude, 0);
23
+ let midday = SearchHourAngle(Body.Sun, observer, 0, new Date(naturalDate.nadir));
24
+
25
+ return {
26
+ altitude: sunAltitude, // Current sun altitude
27
+ highestAltitude: midday.hor.altitude // Highest altitude of the sun during the day
28
+ }
29
+ }
30
+
31
+
32
+ /**
33
+ * SUN EVENTS
34
+ *
35
+ * Determine natural day's main sun events like sunrise, sunset, nightStart ...
36
+ * @param {NaturalDate} naturalDate
37
+ * @param {Number} latitude
38
+ * @returns Object { sunrise, sunset, nightStart, nightEnd, morningGoldenHour, eveningGoldenHour }
39
+ */
40
+ export function NaturalSunEvents(naturalDate, latitude) {
41
+
42
+ let cache_id = naturalDate.toDateString() + latitude + naturalDate.longitude;
43
+
44
+ if(SUN_CACHE?.[cache_id])
45
+ return SUN_CACHE[cache_id];
46
+
47
+ let observer = new Observer(latitude, naturalDate.longitude, 0);
48
+ let nadir = new Date(naturalDate.nadir);
49
+
50
+ let isSummer =
51
+ latitude >= 0 && (naturalDate.dayOfYear >= 91 && naturalDate.dayOfYear <= 273) || // Northern hemisphere summer
52
+ latitude <= 0 && (naturalDate.dayOfYear <= 91 || naturalDate.dayOfYear >= 273); // Southern hemisphere summer
53
+
54
+ // SUNRISE & SUNSET
55
+ let sunrise = naturalDate.getTimeOfEvent(SearchRiseSet(Body.Sun, observer, +1, nadir, 1));
56
+ if(!sunrise) sunrise = isSummer ? 0 : 180;
57
+ let sunset = naturalDate.getTimeOfEvent(SearchRiseSet(Body.Sun, observer, -1, nadir, 1));
58
+ if(!sunset) sunset = isSummer ? 360 : 180;
59
+
60
+ // NIGHT (-12° altitude)
61
+ let nightStart = naturalDate.getTimeOfEvent(SearchAltitude(Body.Sun, observer, -1, nadir, 2, -12));
62
+ if(!nightStart) nightStart = isSummer ? 360 : 180;
63
+ let nightEnd = naturalDate.getTimeOfEvent(SearchAltitude(Body.Sun, observer, +1, nadir, 2, -12));
64
+ if(!nightEnd) nightEnd = isSummer ? 0 : 180;
65
+
66
+ // GOLDEN HOUR (+6° altitude)
67
+ let morningGoldenHour = naturalDate.getTimeOfEvent(SearchAltitude(Body.Sun, observer, +1, nadir, 2, +6));
68
+ if(!morningGoldenHour) morningGoldenHour = isSummer ? 0 : 180;
69
+ let eveningGoldenHour = naturalDate.getTimeOfEvent(SearchAltitude(Body.Sun, observer, -1, nadir, 2, +6));
70
+ if(!eveningGoldenHour) eveningGoldenHour = isSummer ? 360 : 180;
71
+
72
+ return SUN_CACHE[cache_id] = {
73
+ sunrise: sunrise,
74
+ sunset: sunset,
75
+ nightStart: nightStart,
76
+ nightEnd: nightEnd,
77
+ morningGoldenHour: morningGoldenHour,
78
+ eveningGoldenHour: eveningGoldenHour
79
+ }
80
+ }
81
+
82
+ /**
83
+ * MOON POSITION
84
+ *
85
+ * Determines moon's position in the sky at specific natural date and lat/long position
86
+ * @param {NaturalDate} naturalDate
87
+ * @param {Number} latitude
88
+ * @returns Object { position, phase, altitude, highestAltitude }
89
+ */
90
+ export function NaturalMoonPosition(naturalDate, latitude) {
91
+
92
+ let observer = new Observer(latitude, naturalDate.longitude, 0);
93
+ let date = new Date(naturalDate.unixTime);
94
+
95
+ // PHASE & POSITION
96
+ let moonPhase = MoonPhase(date);
97
+
98
+ let moon = Equator(Body.Moon, date, observer, true, false);
99
+ let moonHorizon = Horizon(date, observer, moon.ra, moon.dec);
100
+
101
+ // POSITION & ALTITUDE
102
+ let moonPosition = moonHorizon.azimuth;
103
+ let moonAltitude = Math.max(moonHorizon.altitude, 0);
104
+ let midmoon = SearchHourAngle(Body.Moon, observer, 0, new Date(naturalDate.nadir));
105
+
106
+ return {
107
+ position: moonPosition, // Moon needle position in time-degree (does not match exactly the position in the sky)
108
+ phase: moonPhase, // 0 = new-moon, 90 = first quarter, 180 = full-moon, 270 = last quarter
109
+ altitude: moonAltitude, // Current moon altitude
110
+ highestAltitude: midmoon.hor.altitude // Highest altitude of the moon during the day
111
+ }
112
+ }
113
+
114
+ /**
115
+ * MOON EVENTS
116
+ *
117
+ * Determine natural day's moonrise and moonset
118
+ * @param {NaturalDate} naturalDate
119
+ * @param {Number} latitude
120
+ * @returns Object { moonrise, moonset }
121
+ */
122
+ export function NaturalMoonEvents(naturalDate, latitude) {
123
+
124
+ let cache_id = naturalDate.toDateString() + latitude + naturalDate.longitude;
125
+
126
+ if(MOON_CACHE?.[cache_id])
127
+ return MOON_CACHE[cache_id];
128
+
129
+ let observer = new Observer(latitude, naturalDate.longitude, 0);
130
+
131
+ // MOONRISE & MOONSET
132
+ let moonrise = SearchRiseSet(Body.Moon, observer, +1, new Date(naturalDate.nadir), 1);
133
+ let moonset = SearchRiseSet(Body.Moon, observer, -1, new Date(naturalDate.nadir), 1);
134
+
135
+ return MOON_CACHE[cache_id] = {
136
+ moonrise: naturalDate.getTimeOfEvent(moonrise),
137
+ moonset: naturalDate.getTimeOfEvent(moonset),
138
+ }
139
+ }
package/index.js ADDED
@@ -0,0 +1,220 @@
1
+ import { Seasons } from 'astronomy-engine/astronomy';
2
+
3
+ const NT_CACHE = {};
4
+
5
+ /**
6
+ * NATURAL TIME
7
+ *
8
+ * Natural time is a fresh, elegant, and coherent way of measuring the movements of time here on the Earth.
9
+ * This new time standard is based on common sense and the observation of natural cycles.
10
+ * Learn more: https://naturaltime.app
11
+ *
12
+ * This JavaScript Class translates gregorian artificial datetime to natural datetime
13
+ * https://github.com/sylvain441/natural-time-js
14
+ *
15
+ * Special thanks to Don Cross, author of the Astronomy Library to calculate celestial body events
16
+ * https://github.com/cosinekitty/astronomy
17
+ *
18
+ * @author Biquette 🐐
19
+ * @contact sylvain441@proton.me
20
+ *
21
+ */
22
+ export class NaturalDate {
23
+
24
+ static END_OF_ARTIFICIAL_TIME = 1356091200000; // 2012-12-22 00:00:00 at +180°;
25
+ static MILLISECONDS_PER_DAY = 86400000; // 24*60*60*1000
26
+
27
+ unixTime; // Artificial gregorian date (UNIX timestamp)
28
+ longitude; // Longitude (between -180° to +180°)
29
+
30
+ year; // Current year (year 1: 2012/2013)
31
+ moon; // Current moon (between 1 and 14)
32
+
33
+ week; // Current week (between 1 and 53)
34
+ weekOfMoon; // Current week of the moon (between 1 and 4)
35
+
36
+ day; // Number of days passed since END_OF_ARTIFICIAL_TIME
37
+ dayOfYear; // Current day of the year (between 1 and 366)
38
+ dayOfMoon; // Current day of the moon (between 1 and 28)
39
+ dayOfWeek; // Current day of the week (between 1 and 7)
40
+
41
+ isRainbowDay; // True if current day is rainbow day
42
+
43
+ time; // Current time (between 0 and 359°999999...)
44
+
45
+ yearStart; // Beginning of the year at the current longitude (UNIX timestamp)
46
+ yearDuration; // Numbers of days in the current year (between 365 and 366)
47
+ nadir; // Beginning of the day at the current longitude (UNIX timestamp)
48
+
49
+ /**
50
+ * Computes natural date from gregorian date and longitude
51
+ * @param {Date} date Date object or Unix timestamp
52
+ * @param {Number} longitude From -180 to + 180
53
+ */
54
+ constructor(date, longitude) {
55
+
56
+ date = new Date(date);
57
+ this.unixTime = date.getTime();
58
+ this.longitude = longitude;
59
+
60
+ if (Number.isFinite(this.unixTime)) {
61
+
62
+ const getYearContext = (artificialYear, longitude) => {
63
+
64
+ // We cache astronomical calculations
65
+ let cache_id = `${artificialYear}_${longitude}`;
66
+ if(NT_CACHE?.[cache_id])
67
+ return NT_CACHE[cache_id];
68
+
69
+ // Search for December solstices
70
+ let startSolstice = Seasons(artificialYear).dec_solstice.date;
71
+ let endSolstice = Seasons(artificialYear + 1).dec_solstice.date;
72
+
73
+ // Calculate next time it's midnight at the 180th meridian after solstice (mean time)
74
+ // NB: Midnight at +180° equals midday the previous day at +0°
75
+ let startNewYear = Date.UTC(
76
+ startSolstice.getUTCFullYear(),
77
+ startSolstice.getUTCMonth(),
78
+ startSolstice.getUTCDate() + (startSolstice.getUTCHours() >= 12 ? 1 : 0),
79
+ 12, 0, 0);
80
+ let endNewYear = Date.UTC(
81
+ endSolstice.getUTCFullYear(),
82
+ endSolstice.getUTCMonth(),
83
+ endSolstice.getUTCDate() + (endSolstice.getUTCHours() >= 12 ? 1 : 0),
84
+ 12, 0, 0);
85
+
86
+ return NT_CACHE[cache_id] = {
87
+ start: parseInt(startNewYear + (-longitude + 180) * NaturalDate.MILLISECONDS_PER_DAY/360),
88
+ duration: (endNewYear - startNewYear) / NaturalDate.MILLISECONDS_PER_DAY
89
+ };
90
+ }
91
+
92
+ // YEAR START & DURATION
93
+ let yearContext = getYearContext(date.getUTCFullYear()-1, longitude);
94
+
95
+ // Correction if between the beginning of natural year and the end of the artificial year
96
+ if(this.unixTime - yearContext.start >= yearContext.duration * NaturalDate.MILLISECONDS_PER_DAY)
97
+ yearContext = getYearContext(date.getUTCFullYear(), longitude);
98
+
99
+ this.yearStart = yearContext.start;
100
+ this.yearDuration = yearContext.duration;
101
+
102
+ let timeSinceLocalYearStart = (this.unixTime - this.yearStart) / NaturalDate.MILLISECONDS_PER_DAY;
103
+
104
+ // YEAR
105
+ this.year = new Date(this.yearStart).getUTCFullYear() - new Date(NaturalDate.END_OF_ARTIFICIAL_TIME).getUTCFullYear()+ 1;
106
+
107
+ // MOON
108
+ this.moon = parseInt(timeSinceLocalYearStart / 28) + 1;
109
+
110
+ // HEPTAD
111
+ this.week = parseInt(timeSinceLocalYearStart / 7) + 1;
112
+ this.weekOfMoon = parseInt(timeSinceLocalYearStart / 7) % 4 + 1;
113
+
114
+ // DAY
115
+ this.day = parseInt((this.unixTime - (NaturalDate.END_OF_ARTIFICIAL_TIME + (-longitude + 180) * NaturalDate.MILLISECONDS_PER_DAY/360)) / NaturalDate.MILLISECONDS_PER_DAY);
116
+ this.dayOfYear = parseInt(timeSinceLocalYearStart) + 1;
117
+ this.dayOfMoon = parseInt(timeSinceLocalYearStart) % 28 + 1;
118
+ this.dayOfWeek = parseInt(timeSinceLocalYearStart) % 7 + 1;
119
+
120
+ // NADIR (i.e day start, midnight)
121
+ this.nadir = this.yearStart + parseInt(timeSinceLocalYearStart) * NaturalDate.MILLISECONDS_PER_DAY;
122
+
123
+ // TIME
124
+ this.time = (this.unixTime - this.nadir) * 360 / NaturalDate.MILLISECONDS_PER_DAY;
125
+
126
+ // RAINBOW DAY
127
+ this.isRainbowDay = this.dayOfYear > 13*28;
128
+
129
+ return;
130
+ }
131
+
132
+ throw 'Argument must be a Date object or a Unix timestamp';
133
+ }
134
+
135
+ /**
136
+ * Returns the time at specified event's date
137
+ * @param {Date} event
138
+ * @returns Time on a 360° scale or null if out of range
139
+ */
140
+ getTimeOfEvent(event) {
141
+ // Make sure it's a unix timestamp
142
+ event = new Date(event).getTime();
143
+
144
+ // Check if not out of range
145
+ if(event < this.nadir || event > this.nadir + NaturalDate.MILLISECONDS_PER_DAY)
146
+ return false;
147
+
148
+ return (event - this.nadir) * (360 / NaturalDate.MILLISECONDS_PER_DAY);
149
+ }
150
+
151
+ /**
152
+ * Exports current date as a full ISO formatted string
153
+ * @returns ex: "004)04)01 113°00 NT+95"
154
+ */
155
+ toString() {
156
+ return this.toDateString() + " " + this.toTimeString() + " " + this.toLongitudeString();
157
+ }
158
+
159
+ /**
160
+ * Exports current date as a formatted string
161
+ * @param {String} separator Default: ')'
162
+ * @returns ex: "004)04)01"
163
+ */
164
+ toDateString(separator = ')') {
165
+ if(this.isRainbowDay)
166
+ return `${this.toYearString()}${separator}RAINBOW${this.dayOfYear == 366 ? "+" : ""}`;
167
+ else
168
+ return `${this.toYearString()}${separator}${this.toMoonString()}${separator}${this.toDayOfMoonString()}`;
169
+ }
170
+
171
+ /**
172
+ * Exports current time as a formatted string
173
+ * @param {*} decimals Number of decimals (precision) Default: 2
174
+ * @param {*} rounding Allows granular incrementation of time (ex: 100°10 => 100°15 => 100°20) Default: 1
175
+ * @returns ex: "113°00" "202°" "63°1234"
176
+ */
177
+ toTimeString(decimals = 2, rounding = 1) {
178
+ let timeUnity = parseInt(this.time);
179
+ let timeDecimals = parseInt((this.time % 1) * Math.pow(10, decimals));
180
+ timeDecimals = Math.floor(timeDecimals / rounding) * rounding;
181
+
182
+ return String(timeUnity).padStart(3, '0') + "°" + (decimals ? String(timeDecimals).padStart(decimals, '0') : '');
183
+ }
184
+
185
+ /**
186
+ * Exports current longitude as a formatted string
187
+ * @param {Number} decimals Number of decimals (precision) Default: 1
188
+ * @returns ex: "NT+95.4" "NT-78"
189
+ */
190
+ toLongitudeString(decimals = 1) {
191
+ if(this.longitude == 0)
192
+ return "NTZ"
193
+
194
+ return "NT" + (this.longitude > 0 ? "+" : "") + this.longitude.toFixed(decimals) + "";
195
+ }
196
+
197
+ /**
198
+ * Exports current year as a formatted string
199
+ * @returns ex: "004"
200
+ */
201
+ toYearString() {
202
+ return (this.year < 0 ? "-" : "") + String(Math.abs(this.year)).padStart(3, '0');
203
+ }
204
+
205
+ /**
206
+ * Exports current moon as a formatted string
207
+ * @returns ex: "04"
208
+ */
209
+ toMoonString() {
210
+ return String(this.moon).padStart(2, '0');
211
+ }
212
+
213
+ /**
214
+ * Exports current day of the moon as a formatted string
215
+ * @returns ex: "01"
216
+ */
217
+ toDayOfMoonString() {
218
+ return String(this.dayOfMoon).padStart(2, '0');
219
+ }
220
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "natural-time-js",
3
+ "version": "1.0.0",
4
+ "description": "Natural time is a fresh, elegant, and coherent way of measuring the movements of time here on the Earth. This new time standard is based on common sense and the observation of natural cycles. Learn more: https://naturaltime.app. This JavaScript Class translates gregorian artificial datetime to natural datetime",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/sylvain441/natural-time-js.git"
12
+ },
13
+ "keywords": [
14
+ "Time",
15
+ "Nature",
16
+ "Earth",
17
+ "Date"
18
+ ],
19
+ "author": "Sylvain 441",
20
+ "license": "CC0-1.0",
21
+ "bugs": {
22
+ "url": "https://github.com/sylvain441/natural-time-js/issues"
23
+ },
24
+ "homepage": "https://github.com/sylvain441/natural-time-js#readme",
25
+ "dependencies": {
26
+ "astronomy-engine": "^2.1.0"
27
+ }
28
+ }