Skip to content

Commit

Permalink
Refactoring and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
spiegel-im-spiegel committed May 2, 2022
1 parent 123a228 commit d47217e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,50 @@ Year 2021 is 辛丑
Day 2021-07-28 is 丁丑
```

### ユリウス日

```go
//go:build run
// +build run

package main

import (
"flag"
"fmt"
"os"

"github.com/goark/koyomi"
"github.com/goark/koyomi/jdn"
)

func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Fprintln(os.Stderr, os.ErrInvalid)
return
}
for _, s := range args {
t, err := koyomi.DateFrom(s)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
j := jdn.GetJDN(t.Time)
fmt.Printf("Julian Day Number of %v is %v\n", t.Format("2006-01-02"), j)
}
}
```

これを実行すると以下のような結果になります。

```
$ go run sample/sample4.go 2022-01-01
$ go run sample/sample4.go 2022-01-01
Julian Day Number of 2022-01-01 is 2459580
```

## Modules Requirement Graph

[![dependency.png](./dependency.png)](./dependency.png)
Expand Down
2 changes: 1 addition & 1 deletion jdn/jdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GetJD(dt time.Time) *big.Rat {
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(quoInt(intRat(int64(dt.Second()+dt.Minute()*60+dt.Hour()*3600)), 60*60*24), floatRat(0.5)))
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
}

Expand Down
5 changes: 2 additions & 3 deletions jdn/jdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"math/big"
"testing"
"time"

"github.com/goark/koyomi"
)

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
Expand All @@ -17,7 +16,7 @@ func TestGetJDN(t *testing.T) {
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, koyomi.JST), outp1: floatRat(2459580.125), outp2: 2459580, outp3: floatRat(59579.625), outp4: 59579},
{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)
Expand Down
46 changes: 46 additions & 0 deletions sample/sample4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build run
// +build run

package main

import (
"flag"
"fmt"
"os"

"github.com/goark/koyomi"
"github.com/goark/koyomi/jdn"
)

func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Fprintln(os.Stderr, os.ErrInvalid)
return
}
for _, s := range args {
t, err := koyomi.DateFrom(s)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
j := jdn.GetJDN(t.Time)
fmt.Printf("Julian Day Number of %v is %v\n", t.Format("2006-01-02"), j)
}
}

/* 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.
*/

0 comments on commit d47217e

Please sign in to comment.