-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from goark/julian-day-number
Add Julian Day Number package
- Loading branch information
Showing
8 changed files
with
337 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package jdn | ||
|
||
import ( | ||
"math/big" | ||
"time" | ||
) | ||
|
||
// GetJD returns Julian Day from time.Time. | ||
func GetJD(dt time.Time) *big.Rat { | ||
dt = dt.In(time.UTC) | ||
y := intRat(int64(dt.Year())) | ||
m := int64(dt.Month()) | ||
d := int64(dt.Day()) | ||
k := floorRat(quoInt(intRat(14-m), 12)) | ||
jdn := floorRat(mulRat(addInt(subRat(y, k), 4800), fracInt(1461, 4))) | ||
jdn = addRat(jdn, floorRat(mulRat(addInt(mulInt(k, 12), m-2), fracInt(367, 12)))) | ||
jdn = subRat(jdn, floorRat(mulRat(floorRat(quoInt(addInt(subRat(y, k), 4900), 100)), fracInt(3, 4)))) | ||
jdn = addInt(jdn, d-32075) | ||
jdn = addRat(jdn, subRat(quoRat(addRat(intRat(int64(dt.Second()+dt.Minute()*60+dt.Hour()*3600)), fracInt(int64(dt.Nanosecond()), 999999999)), floatRat((24*time.Hour).Seconds())), floatRat(0.5))) | ||
return jdn | ||
} | ||
|
||
// GetJDN returns Julian Day Number from time.Time. | ||
func GetJDN(dt time.Time) int64 { | ||
return floorRat(GetJD(dt)).Num().Int64() | ||
} | ||
|
||
// GetMJD returns Modified Julian Day from time.Time. | ||
func GetMJD(dt time.Time) *big.Rat { | ||
return subRat(GetJD(dt), floatRat(2400000.5)) | ||
} | ||
|
||
// GetMJDN returns Modified Julian Day Number from time.Time. | ||
func GetMJDN(dt time.Time) int64 { | ||
return floorRat(GetMJD(dt)).Num().Int64() | ||
} | ||
|
||
func intRat(x int64) *big.Rat { | ||
return fracInt(x, 1) | ||
} | ||
|
||
func floatRat(x float64) *big.Rat { | ||
return (&big.Rat{}).SetFloat64(x) | ||
} | ||
|
||
func fracInt(x, y int64) *big.Rat { | ||
return big.NewRat(x, y) | ||
} | ||
|
||
func addInt(x *big.Rat, y int64) *big.Rat { | ||
return addRat(x, intRat(y)) | ||
} | ||
|
||
// func subInt(x *big.Rat, y int64) *big.Rat { | ||
// return subRat(x, intRat(y)) | ||
// } | ||
|
||
func mulInt(x *big.Rat, y int64) *big.Rat { | ||
return mulRat(x, intRat(y)) | ||
} | ||
|
||
func quoInt(x *big.Rat, y int64) *big.Rat { | ||
return quoRat(x, intRat(y)) | ||
} | ||
|
||
func addRat(x, y *big.Rat) *big.Rat { | ||
return (&big.Rat{}).Add(x, y) | ||
} | ||
|
||
func subRat(x, y *big.Rat) *big.Rat { | ||
return (&big.Rat{}).Sub(x, y) | ||
} | ||
|
||
func mulRat(x, y *big.Rat) *big.Rat { | ||
return (&big.Rat{}).Mul(x, y) | ||
} | ||
|
||
func quoRat(x, y *big.Rat) *big.Rat { | ||
return (&big.Rat{}).Quo(x, y) | ||
} | ||
|
||
func floorRat(n *big.Rat) *big.Rat { | ||
return (&big.Rat{}).SetInt((&big.Int{}).Div(n.Num(), n.Denom())) | ||
} | ||
|
||
/* Copyright 2022 Spiegel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package jdn | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGetJDN(t *testing.T) { | ||
jst := time.FixedZone("JST", int((9 * time.Hour).Seconds())) // Japan standard Time | ||
testCases := []struct { | ||
inp time.Time | ||
outp1 *big.Rat | ||
outp2 int64 | ||
outp3 *big.Rat | ||
outp4 int64 | ||
}{ | ||
{inp: time.Date(2015, time.January, 1, 0, 0, 0, 0, time.UTC), outp1: floatRat(2457023.5), outp2: 2457023, outp3: floatRat(57023.0), outp4: 57023}, | ||
{inp: time.Date(2022, time.January, 1, 0, 0, 0, 0, jst), outp1: floatRat(2459580.125), outp2: 2459580, outp3: floatRat(59579.625), outp4: 59579}, | ||
} | ||
for _, tc := range testCases { | ||
jd := GetJD(tc.inp) | ||
if jd.Cmp(tc.outp1) != 0 { | ||
t.Errorf("GetJD(%v) is %v, want %v.", tc.inp, jd.FloatString(5), tc.outp1.FloatString(5)) | ||
} | ||
jdn := GetJDN(tc.inp) | ||
if jdn != tc.outp2 { | ||
t.Errorf("GetJDN(%v) is %v, want %v.", tc.inp, jdn, tc.outp2) | ||
} | ||
mjd := GetMJD(tc.inp) | ||
if mjd.Cmp(tc.outp3) != 0 { | ||
t.Errorf("GetMJD(%v) is %v, want %v.", tc.inp, mjd.FloatString(5), tc.outp3.FloatString(5)) | ||
} | ||
mjdn := GetMJDN(tc.inp) | ||
if mjdn != tc.outp4 { | ||
t.Errorf("GetMJDN(%v) is %v, want %v.", tc.inp, mjdn, tc.outp4) | ||
} | ||
} | ||
} | ||
|
||
func TestFloorRat(t *testing.T) { | ||
testCases := []struct { | ||
inp float64 | ||
outp float64 | ||
}{ | ||
{inp: 1.1, outp: 1}, | ||
{inp: 1.0, outp: 1}, | ||
{inp: 0.9, outp: 0}, | ||
{inp: 0.1, outp: 0}, | ||
{inp: 0.0, outp: 0}, | ||
{inp: -0.1, outp: -1}, | ||
{inp: -0.9, outp: -1}, | ||
{inp: -1.0, outp: -1}, | ||
{inp: -1.1, outp: -2}, | ||
} | ||
for _, tc := range testCases { | ||
f := floorRat(floatRat(tc.inp)) | ||
if ff, _ := f.Float64(); ff != tc.outp { | ||
t.Errorf("floorRat(%v) is %v, want %v.", tc.inp, f, tc.outp) | ||
} | ||
} | ||
} | ||
|
||
/* Copyright 2022 Spiegel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ |
Oops, something went wrong.