1 | import computeΔT from '@robireton/delta-t'
|
2 |
|
3 | export function JulianDay (date, gregorian = true) {
|
4 | // c.f. Chapter 7 of Astronomical Algorithms by Jean Meeus
|
5 | // the Julian Day number for the given date
|
6 | let Y = date.getUTCFullYear()
|
7 | let M = 1 + date.getUTCMonth()
|
8 | if (M < 3) {
|
9 | Y--
|
10 | M += 12
|
11 | }
|
12 | const D = date.getUTCDate() + date.getUTCHours() / 24 + date.getUTCMinutes() / 1440 + date.getUTCSeconds() / 86400 + date.getUTCMilliseconds() / 86400000
|
13 |
|
14 | const A = Math.floor(Y / 100)
|
15 | const B = gregorian ? 2 - A + Math.floor(A / 4) : 0
|
16 |
|
17 | const JD = Math.floor(365.25 * (Y + 4716)) + Math.floor(30.6001 * (M + 1)) + D + B - 1524.5
|
18 | if (JD < 0) throw new Error('method not valid for negative Julian Day numbers')
|
19 |
|
20 | return JD // the Julian Day Number for the given date
|
21 | }
|
22 |
|
23 | export default function JulianEphemerisDay (date, gregorian = true) {
|
24 | // c.f. Chapter 9 of Astronomical Algorithms by Jean Meeus
|
25 | // the Julian Ephemeris Day number for the given date
|
26 | return JulianDay(date, gregorian) + computeΔT(date.getUTCFullYear(), 1 + date.getUTCMonth()) / 86400
|
27 | }
|
28 |
|
29 | // Number.prototype.JDtoDate = function() {
|
30 | // if (this.valueOf() < 0) throw new Error('method not valid for negative Julian Day numbers')
|
31 | // const JD = this.valueOf() + 0.5
|
32 | // const Z = Math.floor(JD)
|
33 | // const F = JD - Z
|
34 | // let A = Z
|
35 | // if ( Z >= 2299161 ) {
|
36 | // const α = Math.floor( (Z - 1867216.25) / 36524.25 )
|
37 | // A = Z + 1 + α - Math.floor( α / 4 )
|
38 | // }
|
39 | // const B = A + 1524
|
40 | // const C = Math.floor( (B - 122.1) / 365.25 )
|
41 | // const D = Math.floor( 365.25 * C )
|
42 | // const E = Math.floor( (B - D) / 30.6001 )
|
43 | //
|
44 | // const month = (E < 14) ? (E - 1) : (E - 13)
|
45 | // const year = (month > 2) ? (C - 4716) : (C - 4715)
|
46 | // let X = B - D - Math.floor( 30.6001 * E ) + F
|
47 | // const date = Math.floor(X)
|
48 | // X -= date
|
49 | // X *= 24
|
50 | // const hours = Math.floor(X)
|
51 | // X -= hours
|
52 | // X *= 60
|
53 | // const minutes = Math.floor(X)
|
54 | // X -= minutes
|
55 | // X *= 60
|
56 | // const seconds = Math.floor(X)
|
57 | // X -= seconds
|
58 | // X *= 1000
|
59 | //
|
60 | // return new Date( Date.UTC(year, month - 1, date, hours, minutes, seconds, Math.floor(X)) )
|
61 | // }
|