Skip to content

Commit

Permalink
fix(lunar_solar_converter): issuses #1, #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bububa committed Nov 13, 2023
1 parent f53de33 commit f106b4f
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 56 deletions.
25 changes: 20 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@ name: Go

on:
push:
branches: [ main ]
branches: [ master, main ]
pull_request:
branches: [ main ]
branches: [ master, main ]

jobs:

build:
runs-on: ubuntu-latest
env:
GOPATH: ${{ github.workspace }}
package: src/github.com/${{ github.repository }}
defaults:
run:
working-directory: ${{ env.GOPATH }}/${{ env.package }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
path: ${{ env.GOPATH }}/${{ env.package }}

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.16
go-version: '1.20'
cache-dependency-path: |
${{ env.GOPATH }}/${{ env.package }}/go.mod
${{ env.GOPATH }}/${{ env.package }}/go.sum
- name: Dependencies
run: go mod download

- name: Build
working-directory: ${{ env.GOPATH }}/${{ env.package }}
run: go build -v ./...

- name: Test
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/goformatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ jobs:
build:
name: Golang Formatter
runs-on: ubuntu-latest
env:
GOPATH: ${{ github.workspace }}
package: src/github.com/${{ github.repository }}
defaults:
run:
working-directory: ${{ env.GOPATH }}/${{ env.package }}
steps:
- uses: actions/checkout@master
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Golang Formatter
Expand Down
22 changes: 11 additions & 11 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ on:
jobs:
goreleaser:
runs-on: ubuntu-latest
env:
cache: ${{ github.workspace }}/go.sum
steps:
-
name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.16
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
go-version: '1.20'
cache-dependency-path: ${{ env.cache }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
Expand Down
13 changes: 7 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ before:
builds:
- skip: true
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
- id: timenlp
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func main() {
```

## Reference
python 版本https://github.com/sunfiyes/Time-NLPY

python3 版本 https://github.com/zhanzecheng/Time_NLP

Java 版本https://github.com/shinyke/Time-NLP
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/bububa/TimeNLP

go 1.16
go 1.20

require github.com/dlclark/regexp2 v1.4.0
require github.com/dlclark/regexp2 v1.10.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
19 changes: 10 additions & 9 deletions lunar_solar_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ type Solar struct {

// NewSolarFromInt 从timestamp新建阳历Solar
func NewSolarFromInt(g int) Solar {
y := int((10000*g + 14780) / 3652425)
ddd := g - (365*y + int(y/4) - int(y/100) + int(y/400))
y := (10000*g + 14780) / 3652425
ddd := g - (365*y + y/4 - y/100 + y/400)
if ddd < 0 {
y -= 1
ddd = g - (365*y + int(y/4) - int(y/100) + int(y/400))
ddd = g - (365*y + y/4 - y/100 + y/400)
}
mi := int((100*ddd + 52) / 3060)
mi := (100*ddd + 52) / 3060
mm := (mi+2)%12 + 1
y += int((mi + 2) / 12)
dd := ddd - int((mi*306+5)/10) + 1
y += (mi + 2) / 12
dd := ddd - (mi*306+5)/10 + 1
return Solar{
Year: y,
Month: mm,
Expand All @@ -38,8 +38,8 @@ func NewSolarFromInt(g int) Solar {
func (s Solar) ToInt() int {
m := (s.Month + 9) % 12
y := s.Year
y -= int(m / 10)
return 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (s.Day - 1)
y -= (m / 10)
return 365*y + y/4 - y/100 + y/400 + (m*306+5)/10 + (s.Day - 1)
}

// LunarSolarConverter 阴历阳历转换结构体
Expand Down Expand Up @@ -152,13 +152,14 @@ func (l *LunarSolarConverter) LunarToSolar(lunar Lunar) Solar {
}
}
var idx int
for idx <= loopEnd {
for idx < loopEnd {
v := l.GetBigInt(days, 1, 12-idx)
if v == 1 {
offset += 30
} else {
offset += 29
}
idx++
}
offset += lunar.Day
solarTmp := l.Solar[lunar.Year-l.Solar[0]]
Expand Down
38 changes: 19 additions & 19 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,27 @@ func TestLooseTime(t *testing.T) {
// TestSeason 测试节气
func TestSeason(t *testing.T) {
normalizer := NewTimeNormalizer(true)
target := "今年春分"
t.Log(target)
targets := []string{"今年春分", "大年初一", "明年初一"}
expectType := TIMESTAMP
expectPoints := []time.Time{
time.Date(timeBase.Year(), 3, 20, 0, 0, 0, 0, loc),
}
ret, err := normalizer.Parse(target, timeBase)
if ret != nil {
t.Log(ret.NormalizedString)
}
if err != nil {
t.Error(err)
} else if ret.Type != expectType {
t.Errorf("expect: %s, got: %s", expectType, ret.Type)
} else if len(ret.Points) != len(expectPoints) {
t.Errorf("expect: %d points, result: %d points", len(expectPoints), len(ret.Points))
} else {
for idx, v := range ret.Points {
if !v.Time.Equal(expectPoints[idx]) {
t.Errorf("expect: %v, got: %v", expectPoints[idx], v)
}
time.Date(timeBase.Year(), 3, 21, 0, 0, 0, 0, loc),
time.Date(timeBase.Year(), 1, 22, 0, 0, 0, 0, loc),
time.Date(timeBase.Year()+1, 2, 10, 0, 0, 0, 0, loc),
}
for idx, target := range targets {
t.Log(target)
ret, err := normalizer.Parse(target, timeBase)
if ret != nil {
t.Log(ret.NormalizedString)
}
if err != nil {
t.Error(err)
} else if ret.Type != expectType {
t.Errorf("expect: %s, got: %s", expectType, ret.Type)
} else if len(ret.Points) != 1 {
t.Errorf("expect: 1 points, result: %d points", len(ret.Points))
} else if !ret.Points[0].Time.Equal(expectPoints[idx]) {
t.Errorf("expect: %v, got: %v", expectPoints[idx], ret.Points[0])
}
}
}
Expand Down
1 change: 1 addition & 0 deletions time_normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (n *TimeNormalizer) preHandling(target string) string {
// Parse 是TimeNormalizer的构造方法,根据提供的待分析字符串和timeBase进行时间表达式提取
func (n *TimeNormalizer) Parse(target string, timeBase time.Time) (*Result, error) {
n.timeBase = timeBase
target = n.filter(target)
target = n.preHandling(target)
timeUnits := n.timeExt(target, timeBase)
ret := Result{
Expand Down
3 changes: 2 additions & 1 deletion time_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,8 @@ type SolarTermData struct {
// china24St 二十世纪和二十一世纪,24节气计算
// :param year: 年份
// :param china_st: 节气
// :return: 节气日期(月, 日)
//
// :return: 节气日期(月, 日)
func (t *TimeUnit) china24St(year int, chinaSt string) []int {
var stKey []float64
if year/100 == 19 || year == 2000 {
Expand Down

0 comments on commit f106b4f

Please sign in to comment.