natural-time-js 1.0.6 → 1.1.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.
- package/context.js +36 -1
- package/index.js +21 -11
- package/package.json +1 -1
- package/constants.js +0 -52
package/context.js
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
|
|
6
6
|
import { NaturalDate } from './index.js';
|
|
7
7
|
import { Body, Observer, SearchHourAngle, SearchRiseSet, SearchAltitude, MoonPhase, Equator, Horizon, Seasons } from 'astronomy-engine';
|
|
8
|
-
import { HEMISPHERES, SEASONS, ANGLES } from './constants.js';
|
|
9
8
|
import {
|
|
10
9
|
isValidLatitude,
|
|
11
10
|
isValidLongitude,
|
|
@@ -13,6 +12,42 @@ import {
|
|
|
13
12
|
throwValidationError
|
|
14
13
|
} from './utils/validators.js';
|
|
15
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Hemisphere identifiers for geographical calculations
|
|
17
|
+
* @constant
|
|
18
|
+
* @enum {string}
|
|
19
|
+
* @readonly
|
|
20
|
+
*/
|
|
21
|
+
export const HEMISPHERES = {
|
|
22
|
+
NORTH: 'NORTH',
|
|
23
|
+
SOUTH: 'SOUTH'
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Day numbers marking the start and end of summer season
|
|
28
|
+
* Day 1 represents January 1st
|
|
29
|
+
* @constant
|
|
30
|
+
* @enum {number}
|
|
31
|
+
* @readonly
|
|
32
|
+
*/
|
|
33
|
+
export const SEASONS = {
|
|
34
|
+
/** Day 91 (typically April 1st) */
|
|
35
|
+
SUMMER_START_DAY: 91,
|
|
36
|
+
/** Day 273 (typically September 30th) */
|
|
37
|
+
SUMMER_END_DAY: 273
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Solar altitude thresholds in degrees for different daylight conditions
|
|
42
|
+
* @constant
|
|
43
|
+
* @enum {number}
|
|
44
|
+
* @readonly
|
|
45
|
+
*/
|
|
46
|
+
export const ANGLES = {
|
|
47
|
+
NIGHT_ALTITUDE: -12,
|
|
48
|
+
GOLDEN_HOUR_ALTITUDE: 6
|
|
49
|
+
};
|
|
50
|
+
|
|
16
51
|
// Move cache to its own module for better separation of concerns
|
|
17
52
|
const astroCache = new Map();
|
|
18
53
|
|
package/index.js
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { Seasons } from 'astronomy-engine';
|
|
7
|
-
import { MILLISECONDS_PER_DAY, END_OF_ARTIFICIAL_TIME } from './constants.js';
|
|
8
7
|
|
|
9
8
|
const yearContextCache = new Map();
|
|
10
9
|
|
|
@@ -43,8 +42,8 @@ const calculateYearContext = (artificialYear, longitude) => {
|
|
|
43
42
|
);
|
|
44
43
|
|
|
45
44
|
const context = {
|
|
46
|
-
start: parseInt(startNewYear + (-longitude + 180) * MILLISECONDS_PER_DAY/360),
|
|
47
|
-
duration: (endNewYear - startNewYear) / MILLISECONDS_PER_DAY
|
|
45
|
+
start: parseInt(startNewYear + (-longitude + 180) * NaturalDate.MILLISECONDS_PER_DAY/360),
|
|
46
|
+
duration: (endNewYear - startNewYear) / NaturalDate.MILLISECONDS_PER_DAY
|
|
48
47
|
};
|
|
49
48
|
|
|
50
49
|
yearContextCache.set(cacheKey, context);
|
|
@@ -56,6 +55,17 @@ const calculateYearContext = (artificialYear, longitude) => {
|
|
|
56
55
|
* @class
|
|
57
56
|
*/
|
|
58
57
|
export class NaturalDate {
|
|
58
|
+
/**
|
|
59
|
+
* Number of milliseconds in a day
|
|
60
|
+
* @constant {number}
|
|
61
|
+
*/
|
|
62
|
+
static MILLISECONDS_PER_DAY = 86400000; // 24 * 60 * 60 * 1000
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* End date of the artificial time era (December 21, 2012 12:00 UTC)
|
|
66
|
+
* @constant {number}
|
|
67
|
+
*/
|
|
68
|
+
static END_OF_ARTIFICIAL_TIME = Date.UTC(2012, 11, 21, 12, 0, 0);
|
|
59
69
|
|
|
60
70
|
unixTime; // Artificial gregorian date (UNIX timestamp)
|
|
61
71
|
longitude; // Longitude (between -180° to +180°)
|
|
@@ -104,16 +114,16 @@ export class NaturalDate {
|
|
|
104
114
|
let yearContext = calculateYearContext(date.getUTCFullYear()-1, longitude);
|
|
105
115
|
|
|
106
116
|
// Correction if between the beginning of natural year and the end of the artificial year
|
|
107
|
-
if(this.unixTime - yearContext.start >= yearContext.duration * MILLISECONDS_PER_DAY)
|
|
117
|
+
if(this.unixTime - yearContext.start >= yearContext.duration * NaturalDate.MILLISECONDS_PER_DAY)
|
|
108
118
|
yearContext = calculateYearContext(date.getUTCFullYear(), longitude);
|
|
109
119
|
|
|
110
120
|
this.yearStart = yearContext.start;
|
|
111
121
|
this.yearDuration = yearContext.duration;
|
|
112
122
|
|
|
113
|
-
let timeSinceLocalYearStart = (this.unixTime - this.yearStart) / MILLISECONDS_PER_DAY;
|
|
123
|
+
let timeSinceLocalYearStart = (this.unixTime - this.yearStart) / NaturalDate.MILLISECONDS_PER_DAY;
|
|
114
124
|
|
|
115
125
|
// YEAR
|
|
116
|
-
this.year = new Date(this.yearStart).getUTCFullYear() - new Date(END_OF_ARTIFICIAL_TIME).getUTCFullYear()+ 1;
|
|
126
|
+
this.year = new Date(this.yearStart).getUTCFullYear() - new Date(NaturalDate.END_OF_ARTIFICIAL_TIME).getUTCFullYear()+ 1;
|
|
117
127
|
|
|
118
128
|
// MOON
|
|
119
129
|
this.moon = parseInt(timeSinceLocalYearStart / 28) + 1;
|
|
@@ -123,16 +133,16 @@ export class NaturalDate {
|
|
|
123
133
|
this.weekOfMoon = parseInt(timeSinceLocalYearStart / 7) % 4 + 1;
|
|
124
134
|
|
|
125
135
|
// DAY
|
|
126
|
-
this.day = parseInt((this.unixTime - (END_OF_ARTIFICIAL_TIME + (-longitude + 180) * MILLISECONDS_PER_DAY/360)) / MILLISECONDS_PER_DAY);
|
|
136
|
+
this.day = parseInt((this.unixTime - (NaturalDate.END_OF_ARTIFICIAL_TIME + (-longitude + 180) * NaturalDate.MILLISECONDS_PER_DAY/360)) / NaturalDate.MILLISECONDS_PER_DAY);
|
|
127
137
|
this.dayOfYear = parseInt(timeSinceLocalYearStart) + 1;
|
|
128
138
|
this.dayOfMoon = parseInt(timeSinceLocalYearStart) % 28 + 1;
|
|
129
139
|
this.dayOfWeek = parseInt(timeSinceLocalYearStart) % 7 + 1;
|
|
130
140
|
|
|
131
141
|
// NADIR (i.e day start, midnight)
|
|
132
|
-
this.nadir = this.yearStart + parseInt(timeSinceLocalYearStart) * MILLISECONDS_PER_DAY;
|
|
142
|
+
this.nadir = this.yearStart + parseInt(timeSinceLocalYearStart) * NaturalDate.MILLISECONDS_PER_DAY;
|
|
133
143
|
|
|
134
144
|
// TIME
|
|
135
|
-
this.time = (this.unixTime - this.nadir) * 360 / MILLISECONDS_PER_DAY;
|
|
145
|
+
this.time = (this.unixTime - this.nadir) * 360 / NaturalDate.MILLISECONDS_PER_DAY;
|
|
136
146
|
|
|
137
147
|
// RAINBOW DAY
|
|
138
148
|
this.isRainbowDay = this.dayOfYear > 13*28;
|
|
@@ -153,10 +163,10 @@ export class NaturalDate {
|
|
|
153
163
|
event = new Date(event).getTime();
|
|
154
164
|
|
|
155
165
|
// Check if not out of range
|
|
156
|
-
if(event < this.nadir || event > this.nadir + MILLISECONDS_PER_DAY)
|
|
166
|
+
if(event < this.nadir || event > this.nadir + NaturalDate.MILLISECONDS_PER_DAY)
|
|
157
167
|
return false;
|
|
158
168
|
|
|
159
|
-
return (event - this.nadir) * (360 / MILLISECONDS_PER_DAY);
|
|
169
|
+
return (event - this.nadir) * (360 / NaturalDate.MILLISECONDS_PER_DAY);
|
|
160
170
|
}
|
|
161
171
|
|
|
162
172
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "natural-time-js",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
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
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
package/constants.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module constants
|
|
3
|
-
* @description Collection of constants used for natural time and astronomical calculations
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Number of milliseconds in a day
|
|
8
|
-
* @constant {number}
|
|
9
|
-
*/
|
|
10
|
-
export const MILLISECONDS_PER_DAY = 86400000; // 24 * 60 * 60 * 1000
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* End date of the artificial time era (December 21, 2012 12:00 UTC)
|
|
14
|
-
* @constant {number}
|
|
15
|
-
*/
|
|
16
|
-
export const END_OF_ARTIFICIAL_TIME = Date.UTC(2012, 11, 21, 12, 0, 0);
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Hemisphere identifiers for geographical calculations
|
|
20
|
-
* @constant
|
|
21
|
-
* @enum {string}
|
|
22
|
-
* @readonly
|
|
23
|
-
*/
|
|
24
|
-
export const HEMISPHERES = {
|
|
25
|
-
NORTH: 'NORTH',
|
|
26
|
-
SOUTH: 'SOUTH'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Day numbers marking the start and end of summer season
|
|
31
|
-
* Day 1 represents January 1st
|
|
32
|
-
* @constant
|
|
33
|
-
* @enum {number}
|
|
34
|
-
* @readonly
|
|
35
|
-
*/
|
|
36
|
-
export const SEASONS = {
|
|
37
|
-
/** Day 91 (typically April 1st) */
|
|
38
|
-
SUMMER_START_DAY: 91,
|
|
39
|
-
/** Day 273 (typically September 30th) */
|
|
40
|
-
SUMMER_END_DAY: 273
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Solar altitude thresholds in degrees for different daylight conditions
|
|
45
|
-
* @constant
|
|
46
|
-
* @enum {number}
|
|
47
|
-
* @readonly
|
|
48
|
-
*/
|
|
49
|
-
export const ANGLES = {
|
|
50
|
-
NIGHT_ALTITUDE: -12,
|
|
51
|
-
GOLDEN_HOUR_ALTITUDE: 6
|
|
52
|
-
}
|