From e3b4a8f03f3ed060abf61fdab1852bcf9dc835db Mon Sep 17 00:00:00 2001 From: Spiegel Date: Tue, 3 May 2022 09:13:01 +0900 Subject: [PATCH] Modified functions in jdn package --- README.md | 4 ++-- jdn/jdn.go | 16 ++++++++++++++-- jdn/jdn_test.go | 15 +++++++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ffc21f8..879847b 100644 --- a/README.md +++ b/README.md @@ -262,8 +262,8 @@ func main() { これを実行すると以下のような結果になります。 ``` -$ go run sample/sample4.go 2022-01-01 -Julian Day Number of 2022-01-01 is 2459580 +$ go run sample/sample4.go 2023-02-25 +Julian Day Number of 2023-02-25 is 2460000 ``` ## Modules Requirement Graph diff --git a/jdn/jdn.go b/jdn/jdn.go index 03aec3e..f0ea008 100644 --- a/jdn/jdn.go +++ b/jdn/jdn.go @@ -36,8 +36,8 @@ func GetMJDN(dt time.Time) int64 { } // FromJDN returns time.Time instance form Julian Day Number. -func FromJD(jn int64) time.Time { - l := intRat(jn + 68569) +func FromJDN(jdnum int64) time.Time { + l := intRat(jdnum + 68569) n := floorRat(mulInt(quoInt(l, 146097), 4)) l = subRat(l, floorRat(quoInt(addInt(mulInt(n, 146097), 3), 4))) i := floorRat(quoInt(mulInt(addInt(l, 1), 4000), 1461001)) @@ -50,6 +50,18 @@ func FromJD(jn int64) time.Time { return time.Date(int(year.Num().Int64()), time.Month(int(month.Num().Int64())), int(day.Num().Int64()), 12, 0, 0, 0, time.UTC) } +// FromJD returns time.Time instance form Julian Date. +func FromJD(jd float64) time.Time { + jdnum := int64(jd) + dt := FromJDN(jdnum) + return dt.Add(time.Duration((jd - float64(jdnum)) * float64(24*time.Hour))) +} + +// FromJD returns time.Time instance form Julian Date. +func FromMJD(mjd float64) time.Time { + return FromJD(mjd + 2400000.5) +} + func intRat(x int64) *big.Rat { return fracInt(x, 1) } diff --git a/jdn/jdn_test.go b/jdn/jdn_test.go index 4e9257b..d184f66 100644 --- a/jdn/jdn_test.go +++ b/jdn/jdn_test.go @@ -18,19 +18,25 @@ func TestGetJDN(t *testing.T) { }{ {inp: time.Date(2015, time.January, 1, 0, 0, 0, 0, time.UTC), outp1: floatRat(2457023.5), outp2: 2457023, outpDt: time.Date(2015, time.January, 0, 12, 0, 0, 0, time.UTC), outp3: floatRat(57023.0), outp4: 57023}, {inp: time.Date(2022, time.January, 1, 0, 0, 0, 0, jst), outp1: floatRat(2459580.125), outp2: 2459580, outpDt: time.Date(2022, time.January, 0, 12, 0, 0, 0, time.UTC), outp3: floatRat(59579.625), outp4: 59579}, + {inp: time.Date(2023, time.February, 24, 12, 0, 0, 0, time.UTC), outp1: floatRat(2460000.0), outp2: 2460000, outpDt: time.Date(2023, time.February, 24, 12, 0, 0, 0, time.UTC), outp3: floatRat(59999.5), outp4: 59999}, } 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)) } + fjd, _ := jd.Float64() + dt := FromJD(fjd) + if !dt.Equal(tc.inp) { + t.Errorf("FromJD(%v) is %v, want %v.", fjd, dt, tc.inp) + } jn := GetJDN(tc.inp) if jn != tc.outp2 { t.Errorf("GetJDN(%v) is %v, want %v.", tc.inp, jn, tc.outp2) } - dt := FromJD(jn) + dt = FromJDN(jn) if !dt.Equal(tc.outpDt) { - t.Errorf("FromJD(%v) is %v, want %v.", jn, dt, tc.outpDt) + t.Errorf("FromJDN(%v) is %v, want %v.", jn, dt, tc.outpDt) } mjd := GetMJD(tc.inp) if mjd.Cmp(tc.outp3) != 0 { @@ -40,6 +46,11 @@ func TestGetJDN(t *testing.T) { if mjdn != tc.outp4 { t.Errorf("GetMJDN(%v) is %v, want %v.", tc.inp, mjdn, tc.outp4) } + fmjd, _ := mjd.Float64() + dt = FromMJD(fmjd) + if !dt.Equal(tc.inp) { + t.Errorf("FromMJD(%v) is %v, want %v.", fjd, dt, tc.inp) + } } }