diff --git a/src/constants.ts b/src/constants.ts index 32672b6..ba1897f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -81,8 +81,8 @@ export const kChildCollectionAttributes = [ type: "numeric", hasToken: true, unit: "decimal hours", - formula: "hours(rawSunrise)+minutes(rawSunrise)/60", - description: "time in decimal hours" + description: "time in decimal hours", + precision: "4" }, { name: "Sunset", @@ -90,8 +90,8 @@ export const kChildCollectionAttributes = [ type: "numeric", hasToken: true, unit: "decimal hours", - formula: "hours(rawSunset)+minutes(rawSunset)/60", - description: "time in decimal hours" + description: "time in decimal hours", + precision: "4" }, { name: "Sunlight angle", diff --git a/src/hooks/useCodapData.ts b/src/hooks/useCodapData.ts index 7ac7d0a..c365eb6 100644 --- a/src/hooks/useCodapData.ts +++ b/src/hooks/useCodapData.ts @@ -69,6 +69,8 @@ export const useCodapData = () => { date: solarEvent.day, rawSunrise: solarEvent.rawSunrise, rawSunset: solarEvent.rawSunset, + "Sunrise": solarEvent.sunrise, + "Sunset": solarEvent.sunset, "Day length": solarEvent.dayLength, "Season": solarEvent.season, "Sunlight angle": solarEvent.sunlightAngle, diff --git a/src/types.ts b/src/types.ts index ad49c82..0e8c285 100644 --- a/src/types.ts +++ b/src/types.ts @@ -15,6 +15,8 @@ export interface DaylightInfo { day: string; // read into CODAP as an ISO date rawSunrise: string; // read into CODAP as an ISO date rawSunset: string; // read into CODAP as an ISO date + sunrise: number; // read into CODAP as a number + sunset: number; // read into CODAP as a number dayLength: number; season: string; sunlightAngle: number; diff --git a/src/utils/daylight-utils.ts b/src/utils/daylight-utils.ts index 270b714..1b71033 100644 --- a/src/utils/daylight-utils.ts +++ b/src/utils/daylight-utils.ts @@ -96,10 +96,23 @@ export function getSunrayAngleInDegrees(dayNum: number, earthTilt: number, lat:n } export function getMinutesSinceMidnight(time: Dayjs): number { - if (!time.isValid()) { - return 0; + return !time.isValid() ? 0 : time.hour() * 60 + time.minute(); +} + +export function getDecimalTime(time: Dayjs): number { + const hours = time.hour(); + const minutes = time.minute(); + const seconds = time.second(); + + let decimalTime = hours + minutes / 60 + seconds / 3600; + let roundedTime = Math.round(decimalTime * 100) / 100; + + // Adjust for edge cases + if (roundedTime % 1 === 0.99) { + roundedTime = Math.ceil(roundedTime); } - return time.hour() * 60 + time.minute(); + + return roundedTime; } export function getDayLightInfo(options: DaylightCalcOptions): DaylightInfo[] { @@ -152,6 +165,8 @@ export function getDayLightInfo(options: DaylightCalcOptions): DaylightInfo[] { day: currentDay.format(kDateFormats.asLocalISODate), rawSunrise: localSunriseObj.format(kDateWithTimeFormats.asLocalISOWithTZOffset), rawSunset: localSunsetObj.format(kDateWithTimeFormats.asLocalISOWithTZOffset), + sunrise: getDecimalTime(localSunriseObj), + sunset: getDecimalTime(localSunsetObj), dayLength: finalDayLength, season: seasonName, sunlightAngle: getSunrayAngleInDegrees(currentDay.dayOfYear(), kEarthTilt, latitude),