Skip to content

Commit

Permalink
Merge pull request #106 from observerly/feature/night/getNight
Browse files Browse the repository at this point in the history
feat: Added getNight() to night module in @observerly/astrometry.
  • Loading branch information
michealroberts authored Sep 4, 2023
2 parents b69a601 + b1901f9 commit 92eccf1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/night.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,46 @@ export const getSolarTransit = (
}

/*****************************************************************************************************************/

/**
*
* getNight()
*
* Calculates the start and end of the next night for the observer at the given date.
*
* @param date - The date to calculate the extent of the next night for.
* @param observer - The geographic coordinates of the observer.
* @param horizon - The horizon altitude to use for the calculation.
* @param temperature - The temperature to use for the calculation.
* @param pressure - The pressure to use for the calculation.
* @returns The start and end of the next night for the observer.
*
*/
export const getNight = (
datetime: Date,
observer: GeographicCoordinate,
horizon: number = -12,
temperature: number = 288.15,
pressure: number = 101325
): {
start: Date | null
end: Date | null
} => {
const { sunset } = getSolarTransit(datetime, observer, horizon, temperature, pressure)

const { sunrise } = getSolarTransit(
new Date(datetime.getTime() + 60000 * 60 * 24),
observer,
horizon,
temperature,
pressure
)

// The observer could be in perpetual daylight or perpetual night, e.g., the North Pole or South Pole:
return {
start: sunset,
end: sunrise
}
}

/*****************************************************************************************************************/
27 changes: 26 additions & 1 deletion tests/night.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { describe, expect, it } from 'vitest'

/*****************************************************************************************************************/

import { getSolarTransit } from '../src'
import { getSolarTransit, getNight } from '../src'

/*****************************************************************************************************************/

Expand Down Expand Up @@ -67,3 +67,28 @@ describe('getSolarTransit', () => {
})

/*****************************************************************************************************************/

describe('getNight', () => {
it('should be defined', () => {
expect(getNight).toBeDefined()
})

it('should return the correct night for the observer at a horizon of 0 degrees', () => {
const { start, end } = getNight(
datetime,
{
latitude,
longitude
},
0
)

expect(start).toBeInstanceOf(Date)
expect(end).toBeInstanceOf(Date)

expect(start?.toISOString()).toBe('2021-05-14T19:56:00.000Z')
expect(end?.toISOString()).toBe('2021-05-15T04:45:00.000Z')
})
})

/*****************************************************************************************************************/

0 comments on commit 92eccf1

Please sign in to comment.