mirror of
https://github.com/acamarata/nrel-spa.git
synced 2026-07-03 04:10:38 +00:00
major fix for handling timezones
This commit is contained in:
parent
8c4c4991f2
commit
4a5c28261e
3 changed files with 35 additions and 21 deletions
11
index.js
11
index.js
|
|
@ -18,7 +18,7 @@ function adjustForCustomAngle(baseSpaData, zenithAngle) {
|
||||||
return adjustedData;
|
return adjustedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSpa(date, lat, lng, params = null, angles = []) {
|
function getSpa(date, lat, lng, tz, params = null, angles = []) {
|
||||||
let data = new spa.SpaData();
|
let data = new spa.SpaData();
|
||||||
data.year = date.getFullYear();
|
data.year = date.getFullYear();
|
||||||
data.month = date.getMonth() + 1; // JavaScript months are 0-indexed
|
data.month = date.getMonth() + 1; // JavaScript months are 0-indexed
|
||||||
|
|
@ -28,15 +28,14 @@ function getSpa(date, lat, lng, params = null, angles = []) {
|
||||||
data.second = date.getSeconds();
|
data.second = date.getSeconds();
|
||||||
data.longitude = lng;
|
data.longitude = lng;
|
||||||
data.latitude = lat;
|
data.latitude = lat;
|
||||||
|
data.timezone = tz;
|
||||||
|
|
||||||
// Set default values if optional parameters are not provided
|
// Set default values if optional parameters are not provided
|
||||||
data.elevation = params?.elevation ?? 10;
|
data.elevation = params?.elevation ?? 50;
|
||||||
data.pressure = params?.pressure ?? 1013.25;
|
data.pressure = params?.pressure ?? 1013.25;
|
||||||
data.temperature = params?.temperature ?? 15;
|
data.temperature = params?.temperature ?? 15;
|
||||||
data.timezone = params?.timezone ?? -5;
|
|
||||||
data.function = spa.SPA_ALL;
|
data.function = spa.SPA_ALL;
|
||||||
|
|
||||||
|
|
||||||
let result = spa.spa_calculate(data);
|
let result = spa.spa_calculate(data);
|
||||||
let output = {};
|
let output = {};
|
||||||
|
|
||||||
|
|
@ -65,8 +64,8 @@ function getSpa(date, lat, lng, params = null, angles = []) {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function calcSpa(date, lat, lng, params = null, angles = []) {
|
function calcSpa(date, lat, lng, tz, params = null, angles = []) {
|
||||||
let rawData = getSpa(date, lat, lng, params, angles);
|
let rawData = getSpa(date, lat, lng, tz, params, angles);
|
||||||
rawData.sunrise = fractalTime(rawData.sunrise);
|
rawData.sunrise = fractalTime(rawData.sunrise);
|
||||||
rawData.solarNoon = fractalTime(rawData.solarNoon);
|
rawData.solarNoon = fractalTime(rawData.solarNoon);
|
||||||
rawData.sunset = fractalTime(rawData.sunset);
|
rawData.sunset = fractalTime(rawData.sunset);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "nrel-spa",
|
"name": "nrel-spa",
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"description": "NREL SPA native implementation in JS",
|
"description": "NREL SPA native implementation in JS",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
@ -21,7 +21,5 @@
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/ussunnah/nrel-spa/issues"
|
"url": "https://github.com/ussunnah/nrel-spa/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/ussunnah/nrel-spa#readme",
|
"homepage": "https://github.com/ussunnah/nrel-spa#readme"
|
||||||
"devDependencies": {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
39
test.js
39
test.js
|
|
@ -1,17 +1,34 @@
|
||||||
const { getSpa, calcSpa } = require('./index');
|
const { getSpa, calcSpa } = require('./index');
|
||||||
|
|
||||||
// Constants for testing
|
const date = new Date();
|
||||||
const myLat = 40.7128; // Latitude for New York City
|
console.log(date)
|
||||||
const myLng = -74.006; // Longitude for New York City
|
|
||||||
const nyDate = new Date(new Date().toLocaleString("en-US", { timeZone: "America/New_York" }));
|
/* NYC - minimum params
|
||||||
const myDate = new Date(); // Date object for today with NY time zone
|
const city = "New York"
|
||||||
const myAngles = [63.435]; // Custom angles for twilight calculations
|
const lat = 40.7128;
|
||||||
|
const lng = -74.006;
|
||||||
|
const tz = -5;
|
||||||
|
const params = null
|
||||||
|
const angles = []
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Jakarta - all params
|
||||||
|
const city = "Jakarta"
|
||||||
|
const lat = -6.2088
|
||||||
|
const lng = 106.8456
|
||||||
|
const tz = 7
|
||||||
|
const elevation = 18
|
||||||
|
const temperature = 26.56
|
||||||
|
const pressure = 1017
|
||||||
|
const params = {elevation, temperature, pressure}
|
||||||
|
const angles = [63.435]
|
||||||
|
|
||||||
|
|
||||||
// Get results
|
// Get results
|
||||||
const get = getSpa(myDate, myLat, myLng, null, myAngles);
|
const get = getSpa(date, lat, lng, tz, params, angles);
|
||||||
const calc = calcSpa(myDate, myLat, myLng, null, myAngles);
|
const calc = calcSpa(date, lat, lng, tz, params, angles);
|
||||||
|
|
||||||
// Print results
|
// Print results
|
||||||
console.log("\nTest: Using NYC and current time:\n")
|
console.log(`\nTest: ${city} with current Date():\n`)
|
||||||
console.log("getSpa =", JSON.stringify(get, null, 2), "\n");
|
console.log("getSpa =", get, "\n");
|
||||||
console.log("calcSpa =", JSON.stringify(calc, null, 2), "\n");
|
console.log("calcSpa =", calc, "\n");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue