major fix for handling timezones

This commit is contained in:
Ali Camarata 2023-11-13 07:05:10 +07:00
parent 8c4c4991f2
commit 4a5c28261e
3 changed files with 35 additions and 21 deletions

View file

@ -18,7 +18,7 @@ function adjustForCustomAngle(baseSpaData, zenithAngle) {
return adjustedData;
}
function getSpa(date, lat, lng, params = null, angles = []) {
function getSpa(date, lat, lng, tz, params = null, angles = []) {
let data = new spa.SpaData();
data.year = date.getFullYear();
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.longitude = lng;
data.latitude = lat;
data.timezone = tz;
// 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.temperature = params?.temperature ?? 15;
data.timezone = params?.timezone ?? -5;
data.function = spa.SPA_ALL;
let result = spa.spa_calculate(data);
let output = {};
@ -65,8 +64,8 @@ function getSpa(date, lat, lng, params = null, angles = []) {
return output;
}
function calcSpa(date, lat, lng, params = null, angles = []) {
let rawData = getSpa(date, lat, lng, params, angles);
function calcSpa(date, lat, lng, tz, params = null, angles = []) {
let rawData = getSpa(date, lat, lng, tz, params, angles);
rawData.sunrise = fractalTime(rawData.sunrise);
rawData.solarNoon = fractalTime(rawData.solarNoon);
rawData.sunset = fractalTime(rawData.sunset);

View file

@ -1,6 +1,6 @@
{
"name": "nrel-spa",
"version": "1.1.0",
"version": "1.2.0",
"description": "NREL SPA native implementation in JS",
"main": "index.js",
"scripts": {
@ -21,7 +21,5 @@
"bugs": {
"url": "https://github.com/ussunnah/nrel-spa/issues"
},
"homepage": "https://github.com/ussunnah/nrel-spa#readme",
"devDependencies": {
}
"homepage": "https://github.com/ussunnah/nrel-spa#readme"
}

39
test.js
View file

@ -1,17 +1,34 @@
const { getSpa, calcSpa } = require('./index');
// Constants for testing
const myLat = 40.7128; // Latitude for New York City
const myLng = -74.006; // Longitude for New York City
const nyDate = new Date(new Date().toLocaleString("en-US", { timeZone: "America/New_York" }));
const myDate = new Date(); // Date object for today with NY time zone
const myAngles = [63.435]; // Custom angles for twilight calculations
const date = new Date();
console.log(date)
/* NYC - minimum params
const city = "New York"
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
const get = getSpa(myDate, myLat, myLng, null, myAngles);
const calc = calcSpa(myDate, myLat, myLng, null, myAngles);
const get = getSpa(date, lat, lng, tz, params, angles);
const calc = calcSpa(date, lat, lng, tz, params, angles);
// Print results
console.log("\nTest: Using NYC and current time:\n")
console.log("getSpa =", JSON.stringify(get, null, 2), "\n");
console.log("calcSpa =", JSON.stringify(calc, null, 2), "\n");
console.log(`\nTest: ${city} with current Date():\n`)
console.log("getSpa =", get, "\n");
console.log("calcSpa =", calc, "\n");