natural-time-js 1.0.2 → 1.0.4

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.
Files changed (3) hide show
  1. package/context.js +40 -12
  2. package/index.js +2 -2
  3. package/package.json +4 -3
package/context.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { NaturalDate } from './index.js';
2
- import { Body, Observer, SearchHourAngle, SearchRiseSet, SearchAltitude, MoonPhase, Equator, Horizon } from 'astronomy-engine/astronomy';
2
+ import { Body, Observer, SearchHourAngle, SearchRiseSet, SearchAltitude, MoonPhase, Equator, Horizon, Seasons } from 'astronomy-engine';
3
3
 
4
- // Improve performance with cached astronomical calculations
5
- const SUN_CACHE = {};
6
- const MOON_CACHE = {};
4
+ // Caching recuring astronomical calculations
5
+ const ASTRO_CACHE = {};
7
6
 
8
7
  /**
9
8
  * SUN ALTITUDE
@@ -39,10 +38,11 @@ export function NaturalSunAltitude(naturalDate, latitude) {
39
38
  */
40
39
  export function NaturalSunEvents(naturalDate, latitude) {
41
40
 
42
- let cache_id = naturalDate.toDateString() + latitude + naturalDate.longitude;
41
+ let cache_id = `SUN_${naturalDate.toDateString()}_${latitude}_${naturalDate.longitude}`;
43
42
 
44
- if(SUN_CACHE?.[cache_id])
45
- return SUN_CACHE[cache_id];
43
+ if (ASTRO_CACHE[cache_id]) {
44
+ return ASTRO_CACHE[cache_id];
45
+ }
46
46
 
47
47
  let observer = new Observer(latitude, naturalDate.longitude, 0);
48
48
  let nadir = new Date(naturalDate.nadir);
@@ -69,7 +69,7 @@ export function NaturalSunEvents(naturalDate, latitude) {
69
69
  let eveningGoldenHour = naturalDate.getTimeOfEvent(SearchAltitude(Body.Sun, observer, -1, nadir, 2, +6));
70
70
  if(!eveningGoldenHour) eveningGoldenHour = isSummer ? 360 : 180;
71
71
 
72
- return SUN_CACHE[cache_id] = {
72
+ return ASTRO_CACHE[cache_id] = {
73
73
  sunrise: sunrise,
74
74
  sunset: sunset,
75
75
  nightStart: nightStart,
@@ -117,10 +117,11 @@ export function NaturalMoonPosition(naturalDate, latitude) {
117
117
  */
118
118
  export function NaturalMoonEvents(naturalDate, latitude) {
119
119
 
120
- let cache_id = naturalDate.toDateString() + latitude + naturalDate.longitude;
120
+ let cache_id = `MOON_${naturalDate.toDateString()}_${latitude}_${naturalDate.longitude}`;
121
121
 
122
- if(MOON_CACHE?.[cache_id])
123
- return MOON_CACHE[cache_id];
122
+ if (ASTRO_CACHE[cache_id]) {
123
+ return ASTRO_CACHE[cache_id];
124
+ }
124
125
 
125
126
  let observer = new Observer(latitude, naturalDate.longitude, 0);
126
127
 
@@ -128,8 +129,35 @@ export function NaturalMoonEvents(naturalDate, latitude) {
128
129
  let moonrise = SearchRiseSet(Body.Moon, observer, +1, new Date(naturalDate.nadir), 1);
129
130
  let moonset = SearchRiseSet(Body.Moon, observer, -1, new Date(naturalDate.nadir), 1);
130
131
 
131
- return MOON_CACHE[cache_id] = {
132
+ return ASTRO_CACHE[cache_id] = {
132
133
  moonrise: naturalDate.getTimeOfEvent(moonrise),
133
134
  moonset: naturalDate.getTimeOfEvent(moonset),
134
135
  }
136
+ }
137
+
138
+ /**
139
+ * MUSTACHES RANGE
140
+ *
141
+ * The mustaches represents the shortest and longest day/night length
142
+ * @param {Number} latitude
143
+ * @returns Object { winterSunrise, winterSunset, summerSunrise, summerSunset }
144
+ */
145
+ export function MustachesRange(naturalDate, latitude) {
146
+ let cache_id = `MUSTACHES_${naturalDate.year}_${latitude}`;
147
+
148
+ if (ASTRO_CACHE[cache_id]) {
149
+ return ASTRO_CACHE[cache_id];
150
+ }
151
+
152
+ // Get the solstices for the current year
153
+ const currentSeasons = Seasons(new Date(naturalDate.unixTime).getFullYear());
154
+ let winterSolsticeSunEvents = NaturalSunEvents(new NaturalDate(currentSeasons.dec_solstice.date, 0), latitude);
155
+ let summerSolsticeSunEvents = NaturalSunEvents(new NaturalDate(currentSeasons.jun_solstice.date, 0), latitude);
156
+
157
+ return ASTRO_CACHE[cache_id] = {
158
+ winterSunrise: winterSolsticeSunEvents.sunrise,
159
+ winterSunset: winterSolsticeSunEvents.sunset,
160
+ summerSunrise: summerSolsticeSunEvents.sunrise,
161
+ summerSunset: summerSolsticeSunEvents.sunset,
162
+ };
135
163
  }
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Seasons } from 'astronomy-engine/astronomy';
1
+ import { Seasons } from 'astronomy-engine';
2
2
 
3
3
  const NT_CACHE = {};
4
4
 
@@ -188,7 +188,7 @@ export class NaturalDate {
188
188
  * @returns ex: "NT+95.4" "NT-78"
189
189
  */
190
190
  toLongitudeString(decimals = 1) {
191
- if(this.longitude == 0)
191
+ if(Math.abs(Math.round(this.longitude)) == 0)
192
192
  return "NTZ"
193
193
 
194
194
  return "NT" + (this.longitude > 0 ? "+" : "") + this.longitude.toFixed(decimals) + "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "natural-time-js",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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": {
@@ -14,7 +14,8 @@
14
14
  "Time",
15
15
  "Nature",
16
16
  "Earth",
17
- "Date"
17
+ "Date",
18
+ "Natural Time"
18
19
  ],
19
20
  "author": "Sylvain 441",
20
21
  "license": "CC0-1.0",
@@ -23,6 +24,6 @@
23
24
  },
24
25
  "homepage": "https://github.com/sylvain441/natural-time-js#readme",
25
26
  "dependencies": {
26
- "astronomy-engine": "^2.1.0"
27
+ "astronomy-engine": "^2.1.19"
27
28
  }
28
29
  }