Skip to content

Commit

Permalink
rename: sensible method
Browse files Browse the repository at this point in the history
  • Loading branch information
festum committed May 20, 2020
1 parent a1c8f7c commit 2373533
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ chronos.New() // Base on current date
chronos.New(time.Now()) // Time interface
d := chronos.New("2019/10/30 01:30") // string interface
l := d.Lunar() // Get lunar calendar
fmt.Println(l.Date())
fmt.Println(l.String())
// print: "己亥年十月初三"
fmt.Println(l.EightCharacter())
fmt.Println(l.EightChar())
// print: "己 亥 甲 戌 庚 子 丁 丑"
d.Solar() // Get solar calendar
```
5 changes: 0 additions & 5 deletions calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type calendar struct {
type Calendar interface {
Lunar() *Lunar
Solar() *Solar
LunarDate() string
}

type CalendarData interface {
Expand Down Expand Up @@ -57,7 +56,3 @@ func (c *calendar) Lunar() *Lunar {
func (c *calendar) Solar() *Solar {
return &Solar{time: c.time}
}

func (c *calendar) LunarDate() string {
return c.Lunar().Date()
}
6 changes: 3 additions & 3 deletions lunar.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (l *Lunar) Calendar() Calendar {
return New(t)
}

func (l *Lunar) EightCharacter() []string {
func (l *Lunar) EightChar() []string {
rlt := l.YearString(l.lichunOffset) + l.MonthString() + l.DayString() + l.HourString()
return strings.Split(rlt, "")
}
Expand Down Expand Up @@ -230,7 +230,7 @@ func daysBetween(d time.Time, s time.Time) int {
return int(subValue)
}

func Solar2Lunar(time time.Time) string {
func LunarString(time time.Time) string {
lunar := CalculateLunar(time.Format(_dateFormat))
result := StemBranchYear(lunar.year) + "年"
if lunar.leap && (lunar.month == lunar.leapMonth) {
Expand All @@ -241,7 +241,7 @@ func Solar2Lunar(time time.Time) string {
return result
}

func (lunar *Lunar) Date() string {
func (lunar *Lunar) String() string {
result := getChineseYear(lunar.year)
if lunar.isLeap() {
result += "閏"
Expand Down
34 changes: 17 additions & 17 deletions lunar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ func TestStemBranchHour(t *testing.T) {
}

func TestNewLunar(t *testing.T) {
assert.NotNil(t, chronos.New().Lunar().Date())
assert.NotNil(t, chronos.New().Lunar().String())
}

func TestCalculateLunar(t *testing.T) {
assert := assert.New(t)
// assert.Equal("庚子年四月十六日", )
assert.Equal(strings.Split("己 亥 甲 戌 壬 寅 庚 子", " "), chronos.New("2019/10/31 23:13").Lunar().EightCharacter())
assert.Equal(strings.Split("己 亥 甲 戌 壬 寅 庚 子", " "), chronos.New("2019/11/01 0:13").Lunar().EightCharacter())
assert.Equal(strings.Split("己 亥 甲 戌 壬 寅 辛 丑", " "), chronos.New("2019/11/01 1:13").Lunar().EightCharacter())
assert.Equal(strings.Split("己 亥 丁 醜 丁 丑 庚 子", " "), chronos.New("2020/02/03 23:13").Lunar().EightCharacter())
assert.Equal(strings.Split("庚 子 戊 寅 丁 丑 辛 丑", " "), chronos.New("2020/02/04 1:13").Lunar().EightCharacter())
assert.Equal(strings.Split("庚 子 戊 寅 丁 丑 己 酉", " "), chronos.New("2020/02/04 18:13").Lunar().EightCharacter())
assert.Equal("己亥年臘月三十", chronos.New("2020/01/24 0:40").LunarDate())
assert.Equal("庚子年正月初一", chronos.New("2020/01/25 0:40").LunarDate())
assert.Equal(strings.Split("戊 辰 乙 醜 丁 卯 己 酉", " "), chronos.New("1989/01/07 18:40").Lunar().EightCharacter())
assert.Equal("戊辰年十一月三十", chronos.New("1989/01/07 18:40").LunarDate())
assert.Equal("戊辰年十一月三十", chronos.New("1989/01/07 0:40").LunarDate())
assert.Equal("己亥年四月廿八", chronos.New("2019/06/01 0:40").LunarDate())
assert.Equal(strings.Split("己 亥 甲 戌 壬 寅 庚 子", " "), chronos.New("2019/10/31 23:13").Lunar().EightChar())
assert.Equal(strings.Split("己 亥 甲 戌 壬 寅 庚 子", " "), chronos.New("2019/11/01 0:13").Lunar().EightChar())
assert.Equal(strings.Split("己 亥 甲 戌 壬 寅 辛 丑", " "), chronos.New("2019/11/01 1:13").Lunar().EightChar())
assert.Equal(strings.Split("己 亥 丁 醜 丁 丑 庚 子", " "), chronos.New("2020/02/03 23:13").Lunar().EightChar())
assert.Equal(strings.Split("庚 子 戊 寅 丁 丑 辛 丑", " "), chronos.New("2020/02/04 1:13").Lunar().EightChar())
assert.Equal(strings.Split("庚 子 戊 寅 丁 丑 己 酉", " "), chronos.New("2020/02/04 18:13").Lunar().EightChar())
assert.Equal("己亥年臘月三十", chronos.New("2020/01/24 0:40").Lunar().String())
assert.Equal("庚子年正月初一", chronos.New("2020/01/25 0:40").Lunar().String())
assert.Equal(strings.Split("戊 辰 乙 醜 丁 卯 己 酉", " "), chronos.New("1989/01/07 18:40").Lunar().EightChar())
assert.Equal("戊辰年十一月三十", chronos.New("1989/01/07 18:40").Lunar().String())
assert.Equal("戊辰年十一月三十", chronos.New("1989/01/07 0:40").Lunar().String())
assert.Equal("己亥年四月廿八", chronos.New("2019/06/01 0:40").Lunar().String())
assert.Equal("己亥年十月初三", chronos.New("2019/10/30 23:00").Lunar().String())
}

func TestLunarEightCharacter(t *testing.T) {
assert := assert.New(t)
assert.Equal(strings.Split("己 亥 甲 戌 庚 子 丁 丑", " "), chronos.New("2019/10/30 01:30").Lunar().EightCharacter())
assert.Equal(strings.Split("己 亥 甲 戌 庚 子 丁 丑", " "), chronos.New("2019/10/30 01:30").Lunar().EightChar())
expected := strings.Split("己 亥 甲 戌 辛 丑 戊 子", " ")
assert.Equal(expected, chronos.New("2019/10/30 23:00").Lunar().EightCharacter())
assert.Equal(expected, chronos.New("2019/10/31 00:30").Lunar().EightCharacter())
assert.Equal("己亥年十月初三", chronos.New("2019/10/30 23:00").Lunar().Date())
assert.Equal(expected, chronos.New("2019/10/30 23:00").Lunar().EightChar())
assert.Equal(expected, chronos.New("2019/10/31 00:30").Lunar().EightChar())
}

0 comments on commit 2373533

Please sign in to comment.