Skip to content

Commit

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

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

export const isNight = (
datetime: Date,
observer: GeographicCoordinate,
horizon: number = -12,
temperature: number = 288.15,
pressure: number = 101325
) => {
const when = new Date(
datetime.getFullYear(),
datetime.getMonth(),
datetime.getDate(),
datetime.getHours(),
datetime.getMinutes(),
datetime.getSeconds(),
datetime.getMilliseconds()
)

const { sunrise, sunset } = getSolarTransit(datetime, observer, horizon, temperature, pressure)

if (sunrise === null || sunset === null) {
return false
}

// If the datetime is before sunrise or after sunset, it is night:
return when.getTime() <= sunrise.getTime() || when.getTime() >= sunset.getTime()
}

/*****************************************************************************************************************/
75 changes: 74 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, getNight } from '../src'
import { getSolarTransit, getNight, isNight } from '../src'

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

Expand Down Expand Up @@ -92,3 +92,76 @@ describe('getNight', () => {
})

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

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

it('should return true for the observer at a horizon of 0 degrees', () => {
expect(
isNight(
datetime,
{
latitude,
longitude
},
0
)
).toBe(true)
})

it('should return true for the observer at a horizon of 0 degrees at midnight on the current day', () => {
expect(
isNight(
datetime,
{
latitude,
longitude
},
0
)
).toBe(true)
})

it('should return false for the observer at a horizon of 0 degrees after sunrise at ~7am on the current day', () => {
expect(
isNight(
new Date('2021-05-14T06:55:00.000+00:00'),
{
latitude,
longitude
},
0
)
).toBe(false)
})

it('should return false for the observer at a horizon of 0 degrees after sunrise at "noon" on the current day', () => {
expect(
isNight(
new Date('2021-05-14T12:00:00.000+00:00'),
{
latitude,
longitude
},
0
)
).toBe(false)
})

it('should return true for the observer at a horizon of 0 degrees after sunset at 9pm on the current day', () => {
expect(
isNight(
new Date('2021-05-14T21:00:00.000+00:00'),
{
latitude,
longitude
},
0
)
).toBe(true)
})
})

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

0 comments on commit 9f99f90

Please sign in to comment.