Skip to content

Commit

Permalink
Merge pull request #10 from goark/debug
Browse files Browse the repository at this point in the history
Modified functions in jdn package
  • Loading branch information
spiegel-im-spiegel authored May 3, 2022
2 parents 7f7f896 + e3b4a8f commit b53dbc3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions jdn/jdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
Expand Down
15 changes: 13 additions & 2 deletions jdn/jdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}

Expand Down

0 comments on commit b53dbc3

Please sign in to comment.