From 4a5c28261efe0f21c293e5e721d0b700d6b55996 Mon Sep 17 00:00:00 2001 From: Ali Camarata Date: Mon, 13 Nov 2023 07:05:10 +0700 Subject: [PATCH] major fix for handling timezones --- index.js | 11 +++++------ package.json | 6 ++---- test.js | 39 ++++++++++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 30e9f09..7dbcc4a 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/package.json b/package.json index 2a95dbf..a32c0b3 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/test.js b/test.js index 96fb274..c3ffe54 100644 --- a/test.js +++ b/test.js @@ -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");