diff --git a/README.md b/README.md index 20167d1..e5f8231 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/calendar.go b/calendar.go index 0b9f9af..ca73065 100644 --- a/calendar.go +++ b/calendar.go @@ -16,7 +16,6 @@ type calendar struct { type Calendar interface { Lunar() *Lunar Solar() *Solar - LunarDate() string } type CalendarData interface { @@ -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() -} diff --git a/lunar.go b/lunar.go index 83c3f4a..3d8c11b 100644 --- a/lunar.go +++ b/lunar.go @@ -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, "") } @@ -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) { @@ -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 += "閏" diff --git a/lunar_test.go b/lunar_test.go index 0922ddf..81adaee 100644 --- a/lunar_test.go +++ b/lunar_test.go @@ -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()) }