Skip to content

Commit

Permalink
example of slightly less steppy decimal hours
Browse files Browse the repository at this point in the history
  • Loading branch information
bacalj committed Aug 14, 2024
1 parent 675c6ec commit d43e3f2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ 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",
title: "Sunset",
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",
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useCodapData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 18 additions & 3 deletions src/utils/daylight-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit d43e3f2

Please sign in to comment.