diff --git a/README.cn.md b/README.cn.md index 73e8cd4e..07316b56 100644 --- a/README.cn.md +++ b/README.cn.md @@ -52,11 +52,11 @@ import "gitee.com/golang-module/carbon" > 假设当前时间为 2020-08-05 13:14:15.999999999 +0800 CST -##### 设置默认值(全局生效) +##### 设置全局默认值 ```go carbon.SetDefault(carbon.Default{ - Layout: carbon.RFC3339Layout, + Layout: carbon.DateTimeLayout, Timezone: carbon.PRC, WeekStartsAt: carbon.Sunday, Locale: "zh-CN", @@ -65,6 +65,15 @@ carbon.SetDefault(carbon.Default{ > 如果没有设置,默认布局模板是 `2006-01-02 15:04:05`, 默认时区是 `Local`, 默认一周开始日期是 `Sunday(周日)`, 默认语言是 `en` +##### Carbon 和 time.Time 互转 + +```go +// 将标准 time.Time 转换成 Carbon +carbon.CreateFromStdTime(time.Now()) +// 将 Carbon 转换成标准 time.Time +carbon.Now().ToStdTime() +``` + ##### 昨天、今天、明天 ```go @@ -245,15 +254,6 @@ carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 20 carbon.ParseByLayout("2020-08-05 13:14:15", "2006-01-02 15:04:05", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15 ``` -##### Carbon 和 time.Time 互转 - -```go -// 将标准 time.Time 转换成 Carbon -carbon.CreateFromStdTime(time.Now()) -// 将 Carbon 转换成标准 time.Time -carbon.Now().ToStdTime() -``` - ##### 时间边界 ```go diff --git a/README.jp.md b/README.jp.md index 07a38369..cd987870 100644 --- a/README.jp.md +++ b/README.jp.md @@ -56,11 +56,11 @@ import "gitee.com/golang-module/carbon" > 現在時刻が 2020-08-05 13:14:15.999999999 +0800 CST であると仮定します。 -##### デフォルト値の設定 (グローバルに有効) +##### グローバル・デフォルトの設定 ```go carbon.SetDefault(carbon.Default{ - Layout: carbon.RFC3339Layout, + Layout: carbon.DateTimeLayout, Timezone: carbon.PRC, WeekStartsAt: carbon.Sunday, Locale: "jp", @@ -69,6 +69,15 @@ carbon.SetDefault(carbon.Default{ > 設定されていない場合,デフォルトのレイアウト テンプレートは `2006-01-02 15:04:05`,デフォルトのタイムゾーンは `Local`,デフォルトの週の開始日は `日曜日`,デフォルトの言語は `en` です。 +##### Carbon と time.Time 交換 + +```go +// 標準の time.Time を Carbon に変換します +carbon.CreateFromStdTime(time.Now()) +// Carbon を標準の time.Time に変換します +carbon.Now().ToStdTime() +``` + ##### 昨日、今日、明日 ```go @@ -245,15 +254,6 @@ carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 20 carbon.ParseByLayout("2020-08-05 13:14:15", "2006-01-02 15:04:05", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15 ``` -##### Carbon と time.Time 交換 - -```go -// 標準の time.Time を Carbon に変換します -carbon.CreateFromStdTime(time.Now()) -// Carbon を標準の time.Time に変換します -carbon.Now().ToStdTime() -``` - ##### 始まりと終わり ```go diff --git a/README.md b/README.md index bc2d5f80..06eceb24 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ import "gitee.com/golang-module/carbon" > Assuming the current time is 2020-08-05 13:14:15.999999999 +0800 CST -##### Set default values (globally effective) +##### Set globally default values ```go carbon.SetDefault(carbon.Default{ @@ -68,6 +68,15 @@ carbon.SetDefault(carbon.Default{ > If not set, the default layout is `2006-01-02 15:04:05`, the default timezone is `Local`, the default week start date is `Sunday` and the default language locale is `en` +##### Convert between Carbon and Time + +```go +// Convert standard Time.time into Carbon +carbon.CreateFromStdTime(time.Now()) +// Convert Carbon to standard Time.time +carbon.Now().ToStdTime() +``` + ##### Yesterday, today and tomorrow ```go @@ -243,15 +252,6 @@ carbon.ParseByLayout("It is 2020-08-05 13:14:15", "It is 2006-01-02 15:04:05").T carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 2006年01月02日15时04分05秒").ToDateTimeString() // 2020-08-05 13:14:15 ``` -##### Convert between Carbon and Time - -```go -// Convert standard Time.time into Carbon -carbon.CreateFromStdTime(time.Now()) -// Convert Carbon into standard Time.time -carbon.Now().ToStdTime() -``` - ##### Boundary ```go diff --git a/calendar.go b/calendar.go index 6b02016a..04384d84 100755 --- a/calendar.go +++ b/calendar.go @@ -5,37 +5,35 @@ import ( "github.com/golang-module/carbon/v2/calendar/lunar" ) -// Lunar converts gregorian calendar to lunar calendar. -// 将 公历 转化为 农历 +// Lunar converts Carbon instance to Lunar instance. +// 将 Carbon 实例转化为 Lunar 实例 func (c Carbon) Lunar() (l lunar.Lunar) { if c.Error != nil { l.Error = c.Error return l } - return lunar.NewGregorian(c.ToStdTime()).ToLunar() + return lunar.FromGregorian(c.StdTime()).ToLunar() } // CreateFromLunar creates a Carbon instance from Lunar date and time. // 从 农历日期 创建 Carbon 实例 func CreateFromLunar(year, month, day, hour, minute, second int, isLeapMonth bool) Carbon { - c := NewCarbon() - c.time = lunar.NewLunar(year, month, day, hour, minute, second, isLeapMonth).ToGregorian().Time - return c + t := lunar.FromLunar(year, month, day, hour, minute, second, isLeapMonth).ToGregorian().Time + return CreateFromStdTime(t) } -// Julian converts gregorian calendar to Julian calendar -// 将 公历 转化为 儒略历 +// Julian converts Carbon instance to Julian instance. +// 将 Carbon 实例转化为 Julian 实例 func (c Carbon) Julian() (j julian.Julian) { if c.Error != nil { return j } - return julian.NewGregorian(c.ToStdTime()).ToJulian() + return julian.FromGregorian(c.StdTime()).ToJulian() } -// CreateFromJulian creates a Carbon instance from Julian Day. -// 从 儒略历 创建 Carbon 实例 +// CreateFromJulian creates a Carbon instance from Julian Day or Modified Julian Day. +// 从 儒略日/简化儒略日 创建 Carbon 实例 func CreateFromJulian(f float64) Carbon { - c := NewCarbon() - c.time = julian.NewJulian(f).ToGregorian().Time - return c + t := julian.FromJulian(f).ToGregorian().Time + return CreateFromStdTime(t) } diff --git a/calendar/gregorian.go b/calendar/gregorian.go index 770c331c..ffc03d24 100644 --- a/calendar/gregorian.go +++ b/calendar/gregorian.go @@ -223,3 +223,16 @@ func (g Gregorian) String() string { func (g Gregorian) IsZero() bool { return g.Time.IsZero() } + +// IsLeapYear reports whether is a leap year. +// 是否是闰年 +func (g Gregorian) IsLeapYear() bool { + if g.IsZero() { + return false + } + year := g.Year() + if year%400 == 0 || (year%4 == 0 && year%100 != 0) { + return true + } + return false +} diff --git a/calendar/gregorian_test.go b/calendar/gregorian_test.go index c1f3a5ca..63a3f836 100755 --- a/calendar/gregorian_test.go +++ b/calendar/gregorian_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestSolar_Year(t *testing.T) { +func TestGregorian_Year(t *testing.T) { type args struct { g Gregorian } @@ -32,7 +32,7 @@ func TestSolar_Year(t *testing.T) { } } -func TestSolar_Month(t *testing.T) { +func TestGregorian_Month(t *testing.T) { type args struct { g Gregorian } @@ -56,7 +56,7 @@ func TestSolar_Month(t *testing.T) { } } -func TestSolar_Day(t *testing.T) { +func TestGregorian_Day(t *testing.T) { type args struct { g Gregorian } @@ -80,7 +80,7 @@ func TestSolar_Day(t *testing.T) { } } -func TestSolar_Hour(t *testing.T) { +func TestGregorian_Hour(t *testing.T) { type args struct { g Gregorian } @@ -104,7 +104,7 @@ func TestSolar_Hour(t *testing.T) { } } -func TestSolar_Minute(t *testing.T) { +func TestGregorian_Minute(t *testing.T) { type args struct { g Gregorian } @@ -128,7 +128,7 @@ func TestSolar_Minute(t *testing.T) { } } -func TestSolar_Second(t *testing.T) { +func TestGregorian_Second(t *testing.T) { type args struct { g Gregorian } @@ -152,7 +152,7 @@ func TestSolar_Second(t *testing.T) { } } -func TestSolar_String(t *testing.T) { +func TestGregorian_String(t *testing.T) { type args struct { g Gregorian } @@ -176,7 +176,7 @@ func TestSolar_String(t *testing.T) { } } -func TestSolar_Location(t *testing.T) { +func TestGregorian_Location(t *testing.T) { type args struct { g Gregorian } @@ -200,7 +200,7 @@ func TestSolar_Location(t *testing.T) { } } -func TestSolar_Date(t *testing.T) { +func TestGregorian_Date(t *testing.T) { type args struct { Time time.Time } @@ -234,7 +234,7 @@ func TestSolar_Date(t *testing.T) { } } -func TestSolar_Clock(t *testing.T) { +func TestGregorian_Clock(t *testing.T) { type args struct { Time time.Time } @@ -267,3 +267,55 @@ func TestSolar_Clock(t *testing.T) { }) } } + +func TestGregorian_IsZero(t *testing.T) { + type args struct { + g Gregorian + } + tests := []struct { + args args + want bool + }{ + { + args: args{NewGregorian(time.Time{})}, + want: true, + }, + { + args: args{NewGregorian(time.Date(2020, 8, 5, 13, 14, 15, 0, time.Local))}, + want: false, + }, + } + for index, tt := range tests { + t.Run(strconv.Itoa(index), func(t *testing.T) { + assert.Equalf(t, tt.want, tt.args.g.IsZero(), "args(%v)", tt.args.g) + }) + } +} + +func TestGregorian_IsLeapYear(t *testing.T) { + type args struct { + g Gregorian + } + tests := []struct { + args args + want bool + }{ + { + args: args{NewGregorian(time.Time{})}, + want: false, + }, + { + args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + want: true, + }, + { + args: args{NewGregorian(time.Date(2021, 8, 5, 0, 0, 0, 0, time.Local))}, + want: false, + }, + } + for index, tt := range tests { + t.Run(strconv.Itoa(index), func(t *testing.T) { + assert.Equalf(t, tt.want, tt.args.g.IsLeapYear(), "args(%v)", tt.args.g) + }) + } +} diff --git a/calendar/julian/julian.go b/calendar/julian/julian.go index 72c84087..52bd6224 100644 --- a/calendar/julian/julian.go +++ b/calendar/julian/julian.go @@ -12,6 +12,10 @@ var ( // julian day or modified julian day decimal precision // 儒略日或简化儒略日小数精度 decimalPrecision = 6 + + // difference between Julian Day and Modified Julian Day + // 儒略日和简化儒略日之间的差值 + diffJdFromMjd = 2400000.5 ) // Gregorian defines a Gregorian struct. @@ -20,33 +24,33 @@ type Gregorian struct { calendar.Gregorian } -// NewGregorian returns a new Gregorian instance. -// 初始化 Gregorian 结构体 -func NewGregorian(t time.Time) (g Gregorian) { - g.Time = t - return g -} - // Julian defines a Julian struct. // 定义 Julian 结构体 type Julian struct { jd, mjd float64 } -// NewJulian returns a new Julian instance. -// 初始化 Julian 结构体 -func NewJulian(f float64) (j Julian) { - // get length of the integer part of f - n := len(strconv.Itoa(int(math.Ceil(f)))) - switch n { +// FromGregorian creates a Gregorian instance from time.Time. +// 从标准 time.Time 创建 Gregorian 实例 +func FromGregorian(t time.Time) (g Gregorian) { + g.Time = t + return g +} + +// FromJulian creates a Julian instance from julian day or modified julian day. +// 从 儒略日 或 简化儒略日 创建 Julian 实例 +func FromJulian(f float64) (j Julian) { + // get length of the integer part + l := len(strconv.Itoa(int(math.Ceil(f)))) + switch l { // modified julian day case 5: j.mjd = f - j.jd = f + 2400000.5 + j.jd = f + diffJdFromMjd // julian day case 7: j.jd = f - j.mjd = f - 2400000.5 + j.mjd = f - diffJdFromMjd default: j.jd = 0 j.mjd = 0 @@ -54,8 +58,8 @@ func NewJulian(f float64) (j Julian) { return } -// ToJulian Convert Gregorian calendar to Julian calendar. -// 将 公历 转化为 儒略历 +// ToJulian converts Gregorian instance to Julian instance. +// 将 Gregorian 实例转化为 Julian 实例 func (g Gregorian) ToJulian() (j Julian) { if g.IsZero() { return j @@ -73,11 +77,11 @@ func (g Gregorian) ToJulian() (j Julian) { y-- } jd := float64(int(365.25*(float64(y)+4716))) + float64(int(30.6001*(float64(m)+1))) + d + float64(n) - 1524.5 - return NewJulian(jd) + return FromJulian(jd) } -// ToGregorian Convert Julian calendar to Gregorian calendar. -// 将 儒略历 转化为 公历 +// ToGregorian converts Julian instance to Gregorian instance. +// 将 Julian 实例转化为 Gregorian 实例 func (j Julian) ToGregorian() (g Gregorian) { if j.jd == 0 || j.mjd == 0 { return g @@ -111,7 +115,7 @@ func (j Julian) ToGregorian() (g Gregorian) { f -= float64(minute) f *= 60 second := int(math.Round(f)) - return NewGregorian(time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Local)) + return FromGregorian(time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Local)) } // JD gets julian day like 2460332.5 diff --git a/calendar/julian/julian_test.go b/calendar/julian/julian_test.go index bb249d21..eaa58709 100644 --- a/calendar/julian/julian_test.go +++ b/calendar/julian/julian_test.go @@ -20,16 +20,16 @@ func TestGregorian_ToJulian(t *testing.T) { want want }{ { - args: args{NewGregorian(time.Time{})}, - want: want{NewJulian(0)}, + args: args{FromGregorian(time.Time{})}, + want: want{FromJulian(0)}, }, { - args: args{NewGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, - want: want{NewJulian(2460332.5)}, + args: args{FromGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, + want: want{FromJulian(2460332.5)}, }, { - args: args{NewGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, - want: want{NewJulian(60332)}, + args: args{FromGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, + want: want{FromJulian(60332)}, }, } for index, tt := range tests { @@ -48,36 +48,36 @@ func TestJulian_ToGregorian(t *testing.T) { want string }{ { - args: args{NewJulian(0)}, + args: args{FromJulian(0)}, want: "", }, { - args: args{NewJulian(2460332.5)}, + args: args{FromJulian(2460332.5)}, want: "2024-01-23 00:00:00", }, { - args: args{NewJulian(60332)}, + args: args{FromJulian(60332)}, want: "2024-01-23 00:00:00", }, { - args: args{NewJulian(2460333)}, + args: args{FromJulian(2460333)}, want: "2024-01-23 12:00:00", }, { - args: args{NewJulian(60332.5)}, + args: args{FromJulian(60332.5)}, want: "2024-01-23 12:00:00", }, { - args: args{NewJulian(2460333.051563)}, + args: args{FromJulian(2460333.051563)}, want: "2024-01-23 13:14:15", }, { - args: args{NewJulian(60332.551563)}, + args: args{FromJulian(60332.551563)}, want: "2024-01-23 13:14:15", }, { - args: args{NewJulian(60232.5)}, + args: args{FromJulian(60232.5)}, want: "2023-10-15 12:00:00", }, } @@ -97,15 +97,15 @@ func TestGregorian_JD(t *testing.T) { want float64 }{ { - args: args{NewGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, want: 2460332.5, }, { - args: args{NewGregorian(time.Date(2024, 1, 23, 12, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 1, 23, 12, 0, 0, 0, time.Local))}, want: 2460333, }, { - args: args{NewGregorian(time.Date(2024, 1, 23, 13, 14, 15, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 1, 23, 13, 14, 15, 0, time.Local))}, want: 2460333.051563, }, } @@ -125,15 +125,15 @@ func TestGregorian_MJD(t *testing.T) { want float64 }{ { - args: args{NewGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 1, 23, 0, 0, 0, 0, time.Local))}, want: 60332, }, { - args: args{NewGregorian(time.Date(2024, 1, 23, 12, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 1, 23, 12, 0, 0, 0, time.Local))}, want: 60332.5, }, { - args: args{NewGregorian(time.Date(2024, 1, 23, 13, 14, 15, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 1, 23, 13, 14, 15, 0, time.Local))}, want: 60332.551563, }, } diff --git a/calendar/lunar/lunar.go b/calendar/lunar/lunar.go index 11c248d7..c06bfbfb 100644 --- a/calendar/lunar/lunar.go +++ b/calendar/lunar/lunar.go @@ -16,6 +16,7 @@ var ( animals = []string{"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"} festivals = map[string]string{ + // "month-day": "name" "1-1": "春节", "1-15": "元宵节", "2-2": "龙抬头", @@ -30,7 +31,7 @@ var ( "12-8": "腊八节", } - lunarTerms = []int{ + years = []int{ 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, // 1900-1909 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, // 1910-1919 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, // 1920-1929 @@ -55,7 +56,7 @@ var ( } invalidYearError = func() error { - return fmt.Errorf("invalid year, currently only 200 years from 1900 to 2100 are supported") + return fmt.Errorf("invalid year, year outside of range [1900,2100]") } ) @@ -73,31 +74,29 @@ type Lunar struct { Error error } -// NewGregorian returns a new Gregorian instance. -// 初始化 Gregorian 结构体 -func NewGregorian(t time.Time) (g Gregorian) { +// FromGregorian creates a Gregorian instance from time.Time. +// 从标准 time.Time 创建 Gregorian 实例 +func FromGregorian(t time.Time) (g Gregorian) { g.Time = t - return g + return } -// NewLunar returns a new Lunar instance. -// 初始化 Lunar 结构体 -func NewLunar(year, month, day, hour, minute, second int, isLeapMonth bool) (l Lunar) { +// FromLunar creates a Lunar instance from lunar datetime. +// 从 农历日期 创建 Lunar 实例 +func FromLunar(year, month, day, hour, minute, second int, isLeapMonth bool) (l Lunar) { l.year, l.month, l.day = year, month, day l.hour, l.minute, l.second = hour, minute, second l.isLeapMonth = isLeapMonth - return l + return } -// ToLunar Convert Gregorian calendar to Lunar calendar. -// 将 公历 转化为 农历 +// ToLunar converts Gregorian instance to Lunar instance. +// 将 Gregorian 实例转化为 Lunar 实例 func (g Gregorian) ToLunar() (l Lunar) { - // leapMonths:闰月总数,daysOfYear:年天数,daysOfMonth:月天数,leapMonth:闰月月份 daysInYear, daysInMonth, leapMonth := 365, 30, 0 - year := g.Year() - if year < minYear || year > maxYear { + if g.Year() < minYear || g.Year() > maxYear { l.Error = invalidYearError() - return l + return } // 与 1900-01-31 相差多少天 offset := g.diffInDays(time.Date(minYear, 1, 31, 0, 0, 0, 0, g.Location())) @@ -139,15 +138,15 @@ func (g Gregorian) ToLunar() (l Lunar) { } l.day = offset + 1 l.hour, l.minute, l.second = g.Clock() - return l + return } -// ToGregorian Convert Lunar calendar to Gregorian calendar. -// 将 农历 转化为 公历 +// ToGregorian converts Lunar instance to Gregorian instance. +// 将 Lunar 实例转化为 Gregorian 实例 func (l Lunar) ToGregorian() (g Gregorian) { if l.year < minYear || l.year > maxYear { g.Error = invalidYearError() - return g + return } days := l.getDaysInMonth() @@ -160,7 +159,7 @@ func (l Lunar) ToGregorian() (g Gregorian) { // https://github.com/golang-module/carbon/issues/219 ts := int64(offset+l.day)*86400 - int64(2206512000) + int64(l.hour)*3600 + int64(l.minute)*60 + int64(l.second) g.Time = time.Unix(ts, 0) - return g + return } func (g Gregorian) diffInDays(t time.Time) int { @@ -197,7 +196,7 @@ func (l Lunar) getOffsetInMonth() int { func (l Lunar) getDaysInYear() int { var days = 348 for i := 0x8000; i > 0x8; i >>= 1 { - if (lunarTerms[l.year-minYear] & i) != 0 { + if (years[l.year-minYear] & i) != 0 { days++ } } @@ -205,7 +204,7 @@ func (l Lunar) getDaysInYear() int { } func (l Lunar) getDaysInMonth() int { - if (lunarTerms[l.year-minYear] & (0x10000 >> uint(l.month))) != 0 { + if (years[l.year-minYear] & (0x10000 >> uint(l.month))) != 0 { return 30 } return 29 @@ -215,7 +214,7 @@ func (l Lunar) getDaysInLeapMonth() int { if l.LeapMonth() == 0 { return 0 } - if lunarTerms[l.year-minYear]&0x10000 != 0 { + if years[l.year-minYear]&0x10000 != 0 { return 30 } return 29 @@ -257,13 +256,13 @@ func (l Lunar) Month() int { return l.month } -// LeapMonth gets lunar leap month like 8. +// LeapMonth gets lunar leap month like 2. // 获取农历闰月月份 func (l Lunar) LeapMonth() int { if l.Error != nil { return 0 } - return lunarTerms[l.year-minYear] & 0xf + return years[l.year-minYear] & 0xf } // Day gets lunar day like 5. @@ -282,23 +281,23 @@ func (l Lunar) ToYearString() string { return "" } year := fmt.Sprintf("%d", l.year) - for i, replace := range numbers { - year = strings.Replace(year, fmt.Sprintf("%d", i), replace, -1) + for index, number := range numbers { + year = strings.Replace(year, fmt.Sprintf("%d", index), number, -1) } return year } // ToMonthString outputs a string in lunar month format like "正月". // 获取农历月字符串 -func (l Lunar) ToMonthString() string { +func (l Lunar) ToMonthString() (month string) { if l.Error != nil { return "" } - month := months[l.month-1] + "月" + month = months[l.month-1] + "月" if l.IsLeapMonth() { return "闰" + month } - return month + return } // ToDayString outputs a string in lunar day format like "廿一". diff --git a/calendar/lunar/lunar_test.go b/calendar/lunar/lunar_test.go index 86f83f8d..73916492 100644 --- a/calendar/lunar/lunar_test.go +++ b/calendar/lunar/lunar_test.go @@ -17,15 +17,15 @@ func TestSolarToLunar(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(2024, 1, 21, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 1, 21, 0, 0, 0, 0, time.Local))}, want: "2023-12-11 00:00:00", }, { - args: args{NewGregorian(time.Date(2023, 3, 2, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2023, 3, 2, 0, 0, 0, 0, time.Local))}, want: "2023-02-11 00:00:00", }, { - args: args{NewGregorian(time.Date(2023, 4, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2023, 4, 1, 0, 0, 0, 0, time.Local))}, want: "2023-02-11 00:00:00", }, } @@ -45,23 +45,23 @@ func TestLunarToGregorian(t *testing.T) { want string }{ { - args: args{NewLunar(2023, 12, 11, 0, 0, 0, false)}, + args: args{FromLunar(2023, 12, 11, 0, 0, 0, false)}, want: "2024-01-21 00:00:00", }, { - args: args{NewLunar(2023, 2, 11, 0, 0, 0, false)}, + args: args{FromLunar(2023, 2, 11, 0, 0, 0, false)}, want: "2023-03-02 00:00:00", }, { - args: args{NewLunar(2023, 2, 11, 0, 0, 0, true)}, + args: args{FromLunar(2023, 2, 11, 0, 0, 0, true)}, want: "2023-04-01 00:00:00", }, { - args: args{NewLunar(1800, 2, 11, 0, 0, 0, false)}, + args: args{FromLunar(1800, 2, 11, 0, 0, 0, false)}, want: "", }, { - args: args{NewLunar(2500, 2, 11, 0, 0, 0, false)}, + args: args{FromLunar(2500, 2, 11, 0, 0, 0, false)}, want: "", }, } @@ -81,75 +81,75 @@ func TestLunar_Animal(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: "鼠", }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: "鼠", }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: "牛", }, { - args: args{NewGregorian(time.Date(2010, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2010, 8, 5, 0, 0, 0, 0, time.Local))}, want: "虎", }, { - args: args{NewGregorian(time.Date(2011, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2011, 8, 5, 0, 0, 0, 0, time.Local))}, want: "兔", }, { - args: args{NewGregorian(time.Date(2012, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2012, 8, 5, 0, 0, 0, 0, time.Local))}, want: "龙", }, { - args: args{NewGregorian(time.Date(2013, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2013, 8, 5, 0, 0, 0, 0, time.Local))}, want: "蛇", }, { - args: args{NewGregorian(time.Date(2014, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2014, 8, 5, 0, 0, 0, 0, time.Local))}, want: "马", }, { - args: args{NewGregorian(time.Date(2015, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2015, 8, 5, 0, 0, 0, 0, time.Local))}, want: "羊", }, { - args: args{NewGregorian(time.Date(2016, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2016, 8, 5, 0, 0, 0, 0, time.Local))}, want: "猴", }, { - args: args{NewGregorian(time.Date(2017, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2017, 8, 5, 0, 0, 0, 0, time.Local))}, want: "鸡", }, { - args: args{NewGregorian(time.Date(2018, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2018, 8, 5, 0, 0, 0, 0, time.Local))}, want: "狗", }, { - args: args{NewGregorian(time.Date(2019, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2019, 8, 5, 0, 0, 0, 0, time.Local))}, want: "猪", }, { - args: args{NewGregorian(time.Date(2020, 5, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 23, 0, 0, 0, 0, time.Local))}, want: "鼠", }, { - args: args{NewGregorian(time.Date(2020, 6, 21, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 6, 21, 0, 0, 0, 0, time.Local))}, want: "鼠", }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: "鼠", }, { - args: args{NewGregorian(time.Date(2021, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 8, 5, 0, 0, 0, 0, time.Local))}, want: "牛", }, } @@ -169,51 +169,51 @@ func TestLunar_Festival(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2021, 2, 12, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 2, 12, 0, 0, 0, 0, time.Local))}, want: "春节", }, { - args: args{NewGregorian(time.Date(2021, 2, 26, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 2, 26, 0, 0, 0, 0, time.Local))}, want: "元宵节", }, { - args: args{NewGregorian(time.Date(2021, 6, 14, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 6, 14, 0, 0, 0, 0, time.Local))}, want: "端午节", }, { - args: args{NewGregorian(time.Date(2021, 8, 14, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 8, 14, 0, 0, 0, 0, time.Local))}, want: "七夕节", }, { - args: args{NewGregorian(time.Date(2021, 8, 22, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 8, 22, 0, 0, 0, 0, time.Local))}, want: "中元节", }, { - args: args{NewGregorian(time.Date(2021, 9, 21, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 9, 21, 0, 0, 0, 0, time.Local))}, want: "中秋节", }, { - args: args{NewGregorian(time.Date(2021, 10, 14, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 10, 14, 0, 0, 0, 0, time.Local))}, want: "重阳节", }, { - args: args{NewGregorian(time.Date(2021, 11, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 11, 5, 0, 0, 0, 0, time.Local))}, want: "寒衣节", }, { - args: args{NewGregorian(time.Date(2021, 11, 19, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 11, 19, 0, 0, 0, 0, time.Local))}, want: "下元节", }, { - args: args{NewGregorian(time.Date(2022, 1, 10, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 1, 10, 0, 0, 0, 0, time.Local))}, want: "腊八节", }, } @@ -233,15 +233,15 @@ func TestLunar_Year(t *testing.T) { want int }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: 0, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: 2020, }, { - args: args{NewGregorian(time.Date(2021, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 5, 1, 0, 0, 0, 0, time.Local))}, want: 2021, }, } @@ -261,55 +261,55 @@ func TestLunar_Month(t *testing.T) { want int }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: 0, }, { - args: args{NewGregorian(time.Date(2021, 3, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 3, 5, 0, 0, 0, 0, time.Local))}, want: 1, }, { - args: args{NewGregorian(time.Date(2021, 4, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 4, 5, 0, 0, 0, 0, time.Local))}, want: 2, }, { - args: args{NewGregorian(time.Date(2021, 5, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 5, 5, 0, 0, 0, 0, time.Local))}, want: 3, }, { - args: args{NewGregorian(time.Date(2021, 6, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 6, 5, 0, 0, 0, 0, time.Local))}, want: 4, }, { - args: args{NewGregorian(time.Date(2021, 7, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 5, 0, 0, 0, 0, time.Local))}, want: 5, }, { - args: args{NewGregorian(time.Date(2021, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 8, 5, 0, 0, 0, 0, time.Local))}, want: 6, }, { - args: args{NewGregorian(time.Date(2021, 9, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 9, 5, 0, 0, 0, 0, time.Local))}, want: 7, }, { - args: args{NewGregorian(time.Date(2021, 10, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 10, 5, 0, 0, 0, 0, time.Local))}, want: 8, }, { - args: args{NewGregorian(time.Date(2021, 10, 20, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 10, 20, 0, 0, 0, 0, time.Local))}, want: 9, }, { - args: args{NewGregorian(time.Date(2021, 11, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 11, 5, 0, 0, 0, 0, time.Local))}, want: 10, }, { - args: args{NewGregorian(time.Date(2022, 12, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 12, 5, 0, 0, 0, 0, time.Local))}, want: 11, }, { - args: args{NewGregorian(time.Date(2022, 1, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 1, 5, 0, 0, 0, 0, time.Local))}, want: 12, }, } @@ -329,15 +329,15 @@ func TestLunar_LeapMonth(t *testing.T) { want int }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: 0, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: 4, }, { - args: args{NewGregorian(time.Date(2021, 7, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 1, 0, 0, 0, 0, time.Local))}, want: 0, }, } @@ -357,23 +357,23 @@ func TestLunar_Day(t *testing.T) { want int }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: 0, }, { - args: args{NewGregorian(time.Date(2020, 8, 19, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 19, 0, 0, 0, 0, time.Local))}, want: 1, }, { - args: args{NewGregorian(time.Date(2020, 8, 20, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 20, 0, 0, 0, 0, time.Local))}, want: 2, }, { - args: args{NewGregorian(time.Date(2020, 8, 21, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 21, 0, 0, 0, 0, time.Local))}, want: 3, }, { - args: args{NewGregorian(time.Date(2020, 8, 22, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 22, 0, 0, 0, 0, time.Local))}, want: 4, }, } @@ -393,15 +393,15 @@ func TestLunar_ToYearString(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零", }, { - args: args{NewGregorian(time.Date(2021, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 5, 1, 0, 0, 0, 0, time.Local))}, want: "二零二一", }, } @@ -421,63 +421,63 @@ func TestLunar_ToMonthString(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, want: "腊月", }, { - args: args{NewGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, want: "正月", }, { - args: args{NewGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, want: "二月", }, { - args: args{NewGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, want: "三月", }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: "闰四月", }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: "闰四月", }, { - args: args{NewGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, want: "闰四月", }, { - args: args{NewGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, want: "五月", }, { - args: args{NewGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, want: "六月", }, { - args: args{NewGregorian(time.Date(2020, 9, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 9, 1, 0, 0, 0, 0, time.Local))}, want: "七月", }, { - args: args{NewGregorian(time.Date(2020, 10, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 10, 1, 0, 0, 0, 0, time.Local))}, want: "八月", }, { - args: args{NewGregorian(time.Date(2020, 11, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 11, 1, 0, 0, 0, 0, time.Local))}, want: "九月", }, { - args: args{NewGregorian(time.Date(2020, 12, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 12, 1, 0, 0, 0, 0, time.Local))}, want: "十月", }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: "十一月", }, } @@ -497,67 +497,67 @@ func TestLunar_ToDayString(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, want: "初七", }, { - args: args{NewGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, want: "初八", }, { - args: args{NewGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, want: "初八", }, { - args: args{NewGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, want: "初九", }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: "初九", }, { - args: args{NewGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, want: "初十", }, { - args: args{NewGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, want: "十一", }, { - args: args{NewGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, want: "十二", }, { - args: args{NewGregorian(time.Date(2020, 9, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 9, 1, 0, 0, 0, 0, time.Local))}, want: "十四", }, { - args: args{NewGregorian(time.Date(2020, 10, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 10, 1, 0, 0, 0, 0, time.Local))}, want: "十五", }, { - args: args{NewGregorian(time.Date(2020, 11, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 11, 1, 0, 0, 0, 0, time.Local))}, want: "十六", }, { - args: args{NewGregorian(time.Date(2020, 12, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 12, 1, 0, 0, 0, 0, time.Local))}, want: "十七", }, { - args: args{NewGregorian(time.Date(2021, 1, 3, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 3, 0, 0, 0, 0, time.Local))}, want: "二十", }, { - args: args{NewGregorian(time.Date(2021, 1, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 5, 0, 0, 0, 0, time.Local))}, want: "廿二", }, { - args: args{NewGregorian(time.Date(2021, 4, 11, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 4, 11, 0, 0, 0, 0, time.Local))}, want: "三十", }, } @@ -577,39 +577,39 @@ func TestLunar_String(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, want: "2019-12-07 00:00:00", }, { - args: args{NewGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, want: "2020-01-08 00:00:00", }, { - args: args{NewGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, want: "2020-02-08 00:00:00", }, { - args: args{NewGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, want: "2020-03-09 00:00:00", }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: "2020-04-09 00:00:00", }, { - args: args{NewGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, want: "2020-04-10 00:00:00", }, { - args: args{NewGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, want: "2020-05-11 00:00:00", }, { - args: args{NewGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, want: "2020-06-12 00:00:00", }, } @@ -629,55 +629,55 @@ func TestLunar_ToDateString(t *testing.T) { want string }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: "", }, { - args: args{NewGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local))}, want: "二零一九年腊月初七", }, { - args: args{NewGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 2, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年正月初八", }, { - args: args{NewGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 3, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年二月初八", }, { - args: args{NewGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年三月初九", }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年闰四月初九", }, { - args: args{NewGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 6, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年闰四月初十", }, { - args: args{NewGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年五月十一", }, { - args: args{NewGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年六月十二", }, { - args: args{NewGregorian(time.Date(2020, 9, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 9, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年七月十四", }, { - args: args{NewGregorian(time.Date(2020, 10, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 10, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年八月十五", }, { - args: args{NewGregorian(time.Date(2020, 11, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 11, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年九月十六", }, { - args: args{NewGregorian(time.Date(2020, 12, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 12, 1, 0, 0, 0, 0, time.Local))}, want: "二零二零年十月十七", }, } @@ -697,31 +697,31 @@ func TestLunar_IsLeapYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, } @@ -741,31 +741,31 @@ func TestLunar_IsLeapMonth(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, } @@ -785,31 +785,31 @@ func TestLunar_IsRatYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, } @@ -829,31 +829,31 @@ func TestLunar_IsOxYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: true, }, { - args: args{NewGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, } @@ -873,31 +873,31 @@ func TestLunar_IsTigerYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2022, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -917,31 +917,31 @@ func TestLunar_IsRabbitYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2023, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2023, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -961,31 +961,31 @@ func TestLunar_IsDragonYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2024, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2024, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -1005,31 +1005,31 @@ func TestLunar_IsSnakeYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2025, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2025, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -1049,31 +1049,31 @@ func TestLunar_IsHorseYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2026, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2026, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -1093,31 +1093,31 @@ func TestLunar_IsGoatYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2027, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2027, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -1137,31 +1137,31 @@ func TestLunar_IsMonkeyYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2028, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2028, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -1181,31 +1181,31 @@ func TestLunar_IsRoosterYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2029, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2029, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -1225,31 +1225,31 @@ func TestLunar_IsDogYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2030, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2030, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } @@ -1269,31 +1269,31 @@ func TestLunar_IsPigYear(t *testing.T) { want bool }{ { - args: args{NewGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 4, 23, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 5, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2020, 8, 5, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2021, 7, 7, 0, 0, 0, 0, time.Local))}, want: false, }, { - args: args{NewGregorian(time.Date(2031, 8, 5, 0, 0, 0, 0, time.Local))}, + args: args{FromGregorian(time.Date(2031, 8, 5, 0, 0, 0, 0, time.Local))}, want: true, }, } diff --git a/calendar_unit_test.go b/calendar_unit_test.go index b8240bd4..b31f3b73 100755 --- a/calendar_unit_test.go +++ b/calendar_unit_test.go @@ -8,12 +8,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCreateFromLunar1(t *testing.T) { - l := CreateFromLunar(2023, 12, 11, 0, 0, 0, false) - assert.Nil(t, l.Error) - assert.Equal(t, "2024-01-21 00:00:00", l.String()) -} - func TestCarbon_Lunar(t *testing.T) { type args struct { c Carbon diff --git a/comparer.go b/comparer.go index 42456704..b7130573 100755 --- a/comparer.go +++ b/comparer.go @@ -7,7 +7,7 @@ import ( // IsDST reports whether is daylight saving time. // 是否是夏令时 func (c Carbon) IsDST() bool { - return c.ToStdTime().IsDST() + return c.time.IsDST() } // IsZero reports whether is zero time. @@ -207,7 +207,7 @@ func (c Carbon) IsMonday() bool { if c.IsInvalid() { return false } - return c.ToStdTime().Weekday() == time.Monday + return c.StdTime().Weekday() == time.Monday } // IsTuesday reports whether is Tuesday. @@ -216,7 +216,7 @@ func (c Carbon) IsTuesday() bool { if c.IsInvalid() { return false } - return c.ToStdTime().Weekday() == time.Tuesday + return c.StdTime().Weekday() == time.Tuesday } // IsWednesday reports whether is Wednesday. @@ -225,7 +225,7 @@ func (c Carbon) IsWednesday() bool { if c.IsInvalid() { return false } - return c.ToStdTime().Weekday() == time.Wednesday + return c.StdTime().Weekday() == time.Wednesday } // IsThursday reports whether is Thursday. @@ -234,7 +234,7 @@ func (c Carbon) IsThursday() bool { if c.IsInvalid() { return false } - return c.ToStdTime().Weekday() == time.Thursday + return c.StdTime().Weekday() == time.Thursday } // IsFriday reports whether is Friday. @@ -243,7 +243,7 @@ func (c Carbon) IsFriday() bool { if c.IsInvalid() { return false } - return c.ToStdTime().Weekday() == time.Friday + return c.StdTime().Weekday() == time.Friday } // IsSaturday reports whether is Saturday. @@ -252,7 +252,7 @@ func (c Carbon) IsSaturday() bool { if c.IsInvalid() { return false } - return c.ToStdTime().Weekday() == time.Saturday + return c.StdTime().Weekday() == time.Saturday } // IsSunday reports whether is Sunday. @@ -261,7 +261,7 @@ func (c Carbon) IsSunday() bool { if c.IsInvalid() { return false } - return c.ToStdTime().Weekday() == time.Sunday + return c.StdTime().Weekday() == time.Sunday } // IsWeekday reports whether is weekday. diff --git a/constellation.go b/constellation.go index 8ae49f46..80260631 100755 --- a/constellation.go +++ b/constellation.go @@ -4,6 +4,25 @@ import ( "strings" ) +var constellations = []struct { + startMonth, startDay int + endMonth, endDay int + index int +}{ + {3, 21, 4, 19, 0}, // Aries + {4, 20, 5, 20, 1}, // Taurus + {5, 21, 6, 21, 2}, // Gemini + {6, 22, 7, 22, 3}, // Cancer + {7, 23, 8, 22, 4}, // Leo + {8, 23, 9, 22, 5}, // Virgo + {9, 23, 10, 23, 6}, // Libra + {10, 24, 11, 22, 7}, // Scorpio + {11, 23, 12, 21, 8}, // Sagittarius + {12, 22, 1, 19, 9}, // Capricorn + {1, 20, 2, 18, 10}, // Aquarius + {2, 19, 3, 20, 11}, // Pisces +} + // Constellation gets constellation name like "Aries", i18n is supported. // 获取星座,支持i18n func (c Carbon) Constellation() string { @@ -15,36 +34,20 @@ func (c Carbon) Constellation() string { } index := -1 _, month, day := c.Date() - switch { - case month == 3 && day >= 21, month == 4 && day <= 19: - index = 0 // Aries - case month == 4 && day >= 20, month == 5 && day <= 20: - index = 1 // Taurus - case month == 5 && day >= 21, month == 6 && day <= 21: - index = 2 // Gemini - case month == 6 && day >= 22, month == 7 && day <= 22: - index = 3 // Cancer - case month == 7 && day >= 23, month == 8 && day <= 22: - index = 4 // Leo - case month == 8 && day >= 23, month == 9 && day <= 22: - index = 5 // Virgo - case month == 9 && day >= 23, month == 10 && day <= 23: - index = 6 // Libra - case month == 10 && day >= 24, month == 11 && day <= 22: - index = 7 // Scorpio - case month == 11 && day >= 23, month == 12 && day <= 21: - index = 8 // Sagittarius - case month == 12 && day >= 22, month == 1 && day <= 19: - index = 9 // Capricorn - case month == 1 && day >= 20, month == 2 && day <= 18: - index = 10 // Aquarius - case month == 2 && day >= 19, month == 3 && day <= 20: - index = 11 // Aquarius + for i := 0; i < len(constellations); i++ { + constellation := constellations[i] + if month == constellation.startMonth && day >= constellation.startDay { + index = constellation.index + } + if month == constellation.endMonth && day <= constellation.endDay { + index = constellation.index + } } + c.lang.rw.Lock() defer c.lang.rw.Unlock() - if constellations, ok := c.lang.resources["constellations"]; ok { - slice := strings.Split(constellations, "|") + if resources, ok := c.lang.resources["constellations"]; ok { + slice := strings.Split(resources, "|") if len(slice) == MonthsPerYear { return slice[index] } diff --git a/constellation_unit_test.go b/constellation_unit_test.go index 797b0177..4150e604 100755 --- a/constellation_unit_test.go +++ b/constellation_unit_test.go @@ -21,6 +21,7 @@ func TestCarbon_Constellation(t *testing.T) { {"0000-00-00 00:00:00", ""}, {"2020-01-05", "Capricorn"}, + {"2020-01-22", "Aquarius"}, {"2020-02-05", "Aquarius"}, {"2020-03-05", "Pisces"}, {"2020-04-05", "Aries"}, @@ -32,6 +33,7 @@ func TestCarbon_Constellation(t *testing.T) { {"2020-10-05", "Libra"}, {"2020-11-05", "Scorpio"}, {"2020-12-05", "Sagittarius"}, + {"2020-12-22", "Capricorn"}, } for index, test := range tests { diff --git a/database.go b/database.go index d138728c..05802ac8 100644 --- a/database.go +++ b/database.go @@ -25,7 +25,7 @@ func (c Carbon) Value() (driver.Value, error) { if c.IsZero() { return nil, nil } - return c.ToStdTime(), nil + return c.StdTime(), nil } // GormDataType implements the interface GormDataTypeInterface for Carbon struct. diff --git a/extremum.go b/extremum.go index 4b124ee9..8eb1a6c1 100755 --- a/extremum.go +++ b/extremum.go @@ -32,24 +32,24 @@ func (c Carbon) Farthest(c1 Carbon, c2 Carbon) Carbon { // Max returns the maximum Carbon instance from the given Carbon instance (second-precision). // 返回最大的 Carbon 实例 -func Max(c1 Carbon, c2 ...Carbon) Carbon { - max := c1 +func Max(c1 Carbon, c2 ...Carbon) (c Carbon) { + c = c1 for i := range c2 { - if c2[i].Gte(max) { - max = c2[i] + if c2[i].Gte(c) { + c = c2[i] } } - return max + return } // Min returns the minimum Carbon instance from the given Carbon instance (second-precision). // 返回最小的 Carbon 实例 -func Min(c1 Carbon, c2 ...Carbon) Carbon { - min := c1 +func Min(c1 Carbon, c2 ...Carbon) (c Carbon) { + c = c1 for i := range c2 { - if c2[i].Lte(min) { - min = c2[i] + if c2[i].Lte(c) { + c = c2[i] } } - return min + return } diff --git a/getter.go b/getter.go index b2d7a502..9e8bd4a8 100755 --- a/getter.go +++ b/getter.go @@ -4,6 +4,12 @@ import ( "time" ) +// StdTime converts Carbon to standard time.Time. +// 将 Carbon 转换成标准 time.Time +func (c Carbon) StdTime() time.Time { + return c.time.In(c.loc) +} + // DaysInYear gets total days in year like 365. // 获取本年的总天数 func (c Carbon) DaysInYear() int { @@ -31,7 +37,7 @@ func (c Carbon) MonthOfYear() int { if c.IsInvalid() { return 0 } - return int(c.ToStdTime().Month()) + return int(c.StdTime().Month()) } // DayOfYear gets day of year like 365. @@ -40,7 +46,7 @@ func (c Carbon) DayOfYear() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().YearDay() + return c.StdTime().YearDay() } // DayOfMonth gets day of month like 30. @@ -49,7 +55,7 @@ func (c Carbon) DayOfMonth() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Day() + return c.StdTime().Day() } // DayOfWeek gets day of week like 6. @@ -58,7 +64,7 @@ func (c Carbon) DayOfWeek() int { if c.IsInvalid() { return 0 } - day := int(c.ToStdTime().Weekday()) + day := int(c.StdTime().Weekday()) if day == 0 { return DaysPerWeek } @@ -71,7 +77,7 @@ func (c Carbon) WeekOfYear() int { if c.IsInvalid() { return 0 } - _, week := c.ToStdTime().ISOWeek() + _, week := c.StdTime().ISOWeek() return week } @@ -136,7 +142,7 @@ func (c Carbon) Date() (year, month, day int) { return } var tm time.Month - year, tm, day = c.ToStdTime().Date() + year, tm, day = c.StdTime().Date() return year, int(tm), day } @@ -176,7 +182,7 @@ func (c Carbon) Time() (hour, minute, second int) { if c.IsInvalid() { return } - return c.ToStdTime().Clock() + return c.StdTime().Clock() } // TimeMilli gets current hour, minute, second and millisecond like 13, 14, 15, 999. @@ -233,7 +239,7 @@ func (c Carbon) Year() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Year() + return c.StdTime().Year() } // Quarter gets current quarter like 3. @@ -283,7 +289,7 @@ func (c Carbon) Hour() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Hour() + return c.StdTime().Hour() } // Minute gets current minute like 14. @@ -292,7 +298,7 @@ func (c Carbon) Minute() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Minute() + return c.StdTime().Minute() } // Second gets current second like 15. @@ -301,7 +307,7 @@ func (c Carbon) Second() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Second() + return c.StdTime().Second() } // Millisecond gets current millisecond like 999. @@ -310,7 +316,7 @@ func (c Carbon) Millisecond() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Nanosecond() / 1e6 + return c.StdTime().Nanosecond() / 1e6 } // Microsecond gets current microsecond like 999999. @@ -319,7 +325,7 @@ func (c Carbon) Microsecond() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Nanosecond() / 1e3 + return c.StdTime().Nanosecond() / 1e3 } // Nanosecond gets current nanosecond like 999999999. @@ -328,7 +334,7 @@ func (c Carbon) Nanosecond() int { if c.IsInvalid() { return 0 } - return c.ToStdTime().Nanosecond() + return c.StdTime().Nanosecond() } // Timestamp gets timestamp with second like 1596604455. @@ -337,7 +343,7 @@ func (c Carbon) Timestamp() int64 { if c.IsInvalid() { return 0 } - return c.ToStdTime().Unix() + return c.StdTime().Unix() } // TimestampMilli gets timestamp with millisecond like 1596604455000. @@ -346,7 +352,7 @@ func (c Carbon) TimestampMilli() int64 { if c.IsInvalid() { return 0 } - t := c.ToStdTime() + t := c.StdTime() return t.Unix()*1e3 + int64(t.Nanosecond())/1e6 } @@ -356,7 +362,7 @@ func (c Carbon) TimestampMicro() int64 { if c.IsInvalid() { return 0 } - t := c.ToStdTime() + t := c.StdTime() return t.Unix()*1e6 + int64(t.Nanosecond())/1e3 } @@ -366,7 +372,7 @@ func (c Carbon) TimestampNano() int64 { if c.IsInvalid() { return 0 } - return c.ToStdTime().UnixNano() + return c.StdTime().UnixNano() } // Location gets location name like "PRC". @@ -378,14 +384,14 @@ func (c Carbon) Location() string { // Timezone gets timezone name like "CST". // 获取时区 func (c Carbon) Timezone() string { - name, _ := c.ToStdTime().Zone() + name, _ := c.StdTime().Zone() return name } // Offset gets offset seconds from the UTC timezone like 28800. // 获取距离UTC时区的偏移量,单位秒 func (c Carbon) Offset() int { - _, offset := c.ToStdTime().Zone() + _, offset := c.StdTime().Zone() return offset } diff --git a/getter_bench_test.go b/getter_bench_test.go index 6e0b0881..fa110996 100755 --- a/getter_bench_test.go +++ b/getter_bench_test.go @@ -2,6 +2,18 @@ package carbon import "testing" +func BenchmarkCarbon_StdTime(b *testing.B) { + now := Now() + for n := 0; n < b.N; n++ { + now.StdTime() + } + + c := NewCarbon() + for n := 0; n < b.N; n++ { + c.StdTime() + } +} + func BenchmarkCarbon_DaysInYear(b *testing.B) { now := Now() for n := 0; n < b.N; n++ { diff --git a/getter_unit_test.go b/getter_unit_test.go index 420e0335..00e720e8 100755 --- a/getter_unit_test.go +++ b/getter_unit_test.go @@ -3,10 +3,17 @@ package carbon import ( "strconv" "testing" + "time" "github.com/stretchr/testify/assert" ) +func TestCarbon_StdTime(t *testing.T) { + expected := time.Now().Format(DateTimeLayout) + actual := Now().StdTime().Format(DateTimeLayout) + assert.Equal(t, expected, actual) +} + func TestCarbon_DaysInYear(t *testing.T) { assert := assert.New(t) diff --git a/outputer.go b/outputer.go index 5c0c30db..62f51d34 100644 --- a/outputer.go +++ b/outputer.go @@ -30,7 +30,7 @@ func (c Carbon) ToString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().String() + return c.StdTime().String() } // ToMonthString outputs a string in month layout like "January", i18n is supported. @@ -47,8 +47,8 @@ func (c Carbon) ToMonthString(timezone ...string) string { } c.lang.rw.Lock() defer c.lang.rw.Unlock() - if months, ok := c.lang.resources["months"]; ok { - slice := strings.Split(months, "|") + if resources, ok := c.lang.resources["months"]; ok { + slice := strings.Split(resources, "|") if len(slice) == MonthsPerYear { return slice[c.Month()-1] } @@ -70,8 +70,8 @@ func (c Carbon) ToShortMonthString(timezone ...string) string { } c.lang.rw.Lock() defer c.lang.rw.Unlock() - if months, ok := c.lang.resources["short_months"]; ok { - slice := strings.Split(months, "|") + if resources, ok := c.lang.resources["short_months"]; ok { + slice := strings.Split(resources, "|") if len(slice) == MonthsPerYear { return slice[c.Month()-1] } @@ -93,8 +93,8 @@ func (c Carbon) ToWeekString(timezone ...string) string { } c.lang.rw.Lock() defer c.lang.rw.Unlock() - if months, ok := c.lang.resources["weeks"]; ok { - slice := strings.Split(months, "|") + if resources, ok := c.lang.resources["weeks"]; ok { + slice := strings.Split(resources, "|") if len(slice) == DaysPerWeek { return slice[c.DayOfWeek()%DaysPerWeek] } @@ -116,8 +116,8 @@ func (c Carbon) ToShortWeekString(timezone ...string) string { } c.lang.rw.Lock() defer c.lang.rw.Unlock() - if months, ok := c.lang.resources["short_weeks"]; ok { - slice := strings.Split(months, "|") + if resources, ok := c.lang.resources["short_weeks"]; ok { + slice := strings.Split(resources, "|") if len(slice) == DaysPerWeek { return slice[c.DayOfWeek()%DaysPerWeek] } @@ -134,7 +134,7 @@ func (c Carbon) ToDayDateTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DayDateTimeLayout) + return c.StdTime().Format(DayDateTimeLayout) } // ToDateTimeString outputs a string in "2006-01-02 15:04:05" layout. @@ -146,7 +146,7 @@ func (c Carbon) ToDateTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateTimeLayout) + return c.StdTime().Format(DateTimeLayout) } // ToDateTimeMilliString outputs a string in "2006-01-02 15:04:05.999" layout. @@ -158,7 +158,7 @@ func (c Carbon) ToDateTimeMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateTimeMilliLayout) + return c.StdTime().Format(DateTimeMilliLayout) } // ToDateTimeMicroString outputs a string in "2006-01-02 15:04:05.999999" layout. @@ -170,7 +170,7 @@ func (c Carbon) ToDateTimeMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateTimeMicroLayout) + return c.StdTime().Format(DateTimeMicroLayout) } // ToDateTimeNanoString outputs a string in "2006-01-02 15:04:05.999999999" layout. @@ -182,7 +182,7 @@ func (c Carbon) ToDateTimeNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateTimeNanoLayout) + return c.StdTime().Format(DateTimeNanoLayout) } // ToShortDateTimeString outputs a string in "20060102150405" layout. @@ -194,7 +194,7 @@ func (c Carbon) ToShortDateTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateTimeLayout) + return c.StdTime().Format(ShortDateTimeLayout) } // ToShortDateTimeMilliString outputs a string in "20060102150405.999" layout. @@ -206,7 +206,7 @@ func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateTimeMilliLayout) + return c.StdTime().Format(ShortDateTimeMilliLayout) } // ToShortDateTimeMicroString outputs a string in "20060102150405.999999" layout. @@ -218,7 +218,7 @@ func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateTimeMicroLayout) + return c.StdTime().Format(ShortDateTimeMicroLayout) } // ToShortDateTimeNanoString outputs a string in "20060102150405.999999999" layout. @@ -230,7 +230,7 @@ func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateTimeNanoLayout) + return c.StdTime().Format(ShortDateTimeNanoLayout) } // ToDateString outputs a string in "2006-01-02" layout. @@ -242,7 +242,7 @@ func (c Carbon) ToDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateLayout) + return c.StdTime().Format(DateLayout) } // ToDateMilliString outputs a string in "2006-01-02.999" layout. @@ -254,7 +254,7 @@ func (c Carbon) ToDateMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateMilliLayout) + return c.StdTime().Format(DateMilliLayout) } // ToDateMicroString outputs a string in "2006-01-02.999999" layout. @@ -266,7 +266,7 @@ func (c Carbon) ToDateMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateMicroLayout) + return c.StdTime().Format(DateMicroLayout) } // ToDateNanoString outputs a string in "2006-01-02.999999999" layout. @@ -278,7 +278,7 @@ func (c Carbon) ToDateNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(DateNanoLayout) + return c.StdTime().Format(DateNanoLayout) } // ToShortDateString outputs a string in "20060102" layout. @@ -290,7 +290,7 @@ func (c Carbon) ToShortDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateLayout) + return c.StdTime().Format(ShortDateLayout) } // ToShortDateMilliString outputs a string in "20060102.999" layout. @@ -302,7 +302,7 @@ func (c Carbon) ToShortDateMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateMilliLayout) + return c.StdTime().Format(ShortDateMilliLayout) } // ToShortDateMicroString outputs a string in "20060102.999999" layout. @@ -314,7 +314,7 @@ func (c Carbon) ToShortDateMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateMicroLayout) + return c.StdTime().Format(ShortDateMicroLayout) } // ToShortDateNanoString outputs a string in "20060102.999999999" layout. @@ -326,7 +326,7 @@ func (c Carbon) ToShortDateNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortDateNanoLayout) + return c.StdTime().Format(ShortDateNanoLayout) } // ToTimeString outputs a string in "15:04:05" layout. @@ -338,7 +338,7 @@ func (c Carbon) ToTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(TimeLayout) + return c.StdTime().Format(TimeLayout) } // ToTimeMilliString outputs a string in "15:04:05.999" layout. @@ -350,7 +350,7 @@ func (c Carbon) ToTimeMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(TimeMilliLayout) + return c.StdTime().Format(TimeMilliLayout) } // ToTimeMicroString outputs a string in "15:04:05.999999" layout. @@ -362,7 +362,7 @@ func (c Carbon) ToTimeMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(TimeMicroLayout) + return c.StdTime().Format(TimeMicroLayout) } // ToTimeNanoString outputs a string in "15:04:05.999999999" layout. @@ -374,7 +374,7 @@ func (c Carbon) ToTimeNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(TimeNanoLayout) + return c.StdTime().Format(TimeNanoLayout) } // ToShortTimeString outputs a string in "150405" layout. @@ -386,7 +386,7 @@ func (c Carbon) ToShortTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortTimeLayout) + return c.StdTime().Format(ShortTimeLayout) } // ToShortTimeMilliString outputs a string in "150405.999" layout. @@ -398,7 +398,7 @@ func (c Carbon) ToShortTimeMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortTimeMilliLayout) + return c.StdTime().Format(ShortTimeMilliLayout) } // ToShortTimeMicroString outputs a string in "150405.999999" layout. @@ -410,7 +410,7 @@ func (c Carbon) ToShortTimeMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortTimeMicroLayout) + return c.StdTime().Format(ShortTimeMicroLayout) } // ToShortTimeNanoString outputs a string in "150405.999999999" layout. @@ -422,7 +422,7 @@ func (c Carbon) ToShortTimeNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ShortTimeNanoLayout) + return c.StdTime().Format(ShortTimeNanoLayout) } // ToAtomString outputs a string in "2006-01-02T15:04:05Z07:00" layout. @@ -434,7 +434,7 @@ func (c Carbon) ToAtomString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(AtomLayout) + return c.StdTime().Format(AtomLayout) } // ToAnsicString outputs a string in "Mon Jan _2 15:04:05 2006" layout. @@ -446,7 +446,7 @@ func (c Carbon) ToAnsicString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ANSICLayout) + return c.StdTime().Format(ANSICLayout) } // ToCookieString outputs a string in "Monday, 02-Jan-2006 15:04:05 MST" layout. @@ -458,7 +458,7 @@ func (c Carbon) ToCookieString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(CookieLayout) + return c.StdTime().Format(CookieLayout) } // ToRssString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format. @@ -470,7 +470,7 @@ func (c Carbon) ToRssString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RssLayout) + return c.StdTime().Format(RssLayout) } // ToW3cString outputs a string in "2006-01-02T15:04:05Z07:00" layout. @@ -488,7 +488,7 @@ func (c Carbon) ToUnixDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(UnixDateLayout) + return c.StdTime().Format(UnixDateLayout) } // ToRubyDateString outputs a string in "Mon Jan 02 15:04:05 -0700 2006" layout. @@ -500,7 +500,7 @@ func (c Carbon) ToRubyDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RubyDateLayout) + return c.StdTime().Format(RubyDateLayout) } // ToKitchenString outputs a string in "3:04PM" layout. @@ -512,7 +512,7 @@ func (c Carbon) ToKitchenString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(KitchenLayout) + return c.StdTime().Format(KitchenLayout) } // ToIso8601String outputs a string in "2006-01-02T15:04:05-07:00" layout. @@ -524,7 +524,7 @@ func (c Carbon) ToIso8601String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ISO8601Layout) + return c.StdTime().Format(ISO8601Layout) } // ToIso8601MilliString outputs a string in "2006-01-02T15:04:05.999-07:00" layout. @@ -536,7 +536,7 @@ func (c Carbon) ToIso8601MilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ISO8601MilliLayout) + return c.StdTime().Format(ISO8601MilliLayout) } // ToIso8601MicroString outputs a string in "2006-01-02T15:04:05.999999-07:00" layout. @@ -548,7 +548,7 @@ func (c Carbon) ToIso8601MicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ISO8601MicroLayout) + return c.StdTime().Format(ISO8601MicroLayout) } // ToIso8601NanoString outputs a string in "2006-01-02T15:04:05.999999999-07:00" layout. @@ -560,7 +560,7 @@ func (c Carbon) ToIso8601NanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(ISO8601NanoLayout) + return c.StdTime().Format(ISO8601NanoLayout) } // ToRfc822String outputs a string in "02 Jan 06 15:04 MST" layout. @@ -572,7 +572,7 @@ func (c Carbon) ToRfc822String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC822Layout) + return c.StdTime().Format(RFC822Layout) } // ToRfc822zString outputs a string in "02 Jan 06 15:04 -0700" layout. @@ -584,7 +584,7 @@ func (c Carbon) ToRfc822zString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC822ZLayout) + return c.StdTime().Format(RFC822ZLayout) } // ToRfc850String outputs a string in "Monday, 02-Jan-06 15:04:05 MST" layout. @@ -596,7 +596,7 @@ func (c Carbon) ToRfc850String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC850Layout) + return c.StdTime().Format(RFC850Layout) } // ToRfc1036String outputs a string in "Mon, 02 Jan 06 15:04:05 -0700" layout. @@ -608,7 +608,7 @@ func (c Carbon) ToRfc1036String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC1036Layout) + return c.StdTime().Format(RFC1036Layout) } // ToRfc1123String outputs a string in "Mon, 02 Jan 2006 15:04:05 MST" layout. @@ -620,7 +620,7 @@ func (c Carbon) ToRfc1123String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC1123Layout) + return c.StdTime().Format(RFC1123Layout) } // ToRfc1123zString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout. @@ -632,7 +632,7 @@ func (c Carbon) ToRfc1123zString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC1123ZLayout) + return c.StdTime().Format(RFC1123ZLayout) } // ToRfc2822String outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout. @@ -644,7 +644,7 @@ func (c Carbon) ToRfc2822String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC2822Layout) + return c.StdTime().Format(RFC2822Layout) } // ToRfc3339String outputs a string in "2006-01-02T15:04:05Z07:00" layout. @@ -656,7 +656,7 @@ func (c Carbon) ToRfc3339String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC3339Layout) + return c.StdTime().Format(RFC3339Layout) } // ToRfc3339MilliString outputs a string in "2006-01-02T15:04:05.999Z07:00" layout. @@ -668,7 +668,7 @@ func (c Carbon) ToRfc3339MilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC3339MilliLayout) + return c.StdTime().Format(RFC3339MilliLayout) } // ToRfc3339MicroString outputs a string in "2006-01-02T15:04:05.999999Z07:00" layout. @@ -680,7 +680,7 @@ func (c Carbon) ToRfc3339MicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC3339MicroLayout) + return c.StdTime().Format(RFC3339MicroLayout) } // ToRfc3339NanoString outputs a string in "2006-01-02T15:04:05.999999999Z07:00" layout. @@ -692,7 +692,7 @@ func (c Carbon) ToRfc3339NanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC3339NanoLayout) + return c.StdTime().Format(RFC3339NanoLayout) } // ToRfc7231String outputs a string in "Mon, 02 Jan 2006 15:04:05 GMT" layout. @@ -704,7 +704,7 @@ func (c Carbon) ToRfc7231String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(RFC7231Layout) + return c.StdTime().Format(RFC7231Layout) } // ToLayoutString outputs a string by layout. @@ -716,7 +716,7 @@ func (c Carbon) ToLayoutString(layout string, timezone ...string) string { if c.IsInvalid() { return "" } - return c.ToStdTime().Format(layout) + return c.StdTime().Format(layout) } // Layout outputs a string by layout, it is shorthand for ToLayoutString. @@ -756,7 +756,7 @@ func (c Carbon) ToFormatString(format string, timezone ...string) string { case 'Z': // timestamp with nanoseconds, such as 1596604455000000000 buffer.WriteString(strconv.FormatInt(c.TimestampNano(), 10)) default: // common symbols - buffer.WriteString(c.ToStdTime().Format(layout)) + buffer.WriteString(c.StdTime().Format(layout)) } } else { switch format[i] { @@ -824,8 +824,10 @@ func (c Carbon) Format(format string, timezone ...string) string { return c.ToFormatString(format, timezone...) } +// Deprecated: it will be removed in the future, use StdTime instead. +// // ToStdTime converts Carbon to standard time.Time. // 将 Carbon 转换成标准 time.Time func (c Carbon) ToStdTime() time.Time { - return c.time.In(c.loc) + return c.StdTime() } diff --git a/season.go b/season.go index 7ba972a0..ceb6ede5 100755 --- a/season.go +++ b/season.go @@ -4,6 +4,23 @@ import ( "strings" ) +var seasons = []struct { + month, index int +}{ + {3, 0}, // spring + {4, 0}, // spring + {5, 0}, // spring + {6, 1}, // summer + {7, 1}, // summer + {8, 1}, // summer + {9, 2}, // autumn + {10, 2}, // autumn + {11, 2}, // autumn + {12, 3}, // winter + {1, 3}, // winter + {2, 3}, // winter +} + // Season gets season name according to the meteorological division method like "Spring", i18n is supported. // 获取当前季节(以气象划分),支持i18n func (c Carbon) Season() string { @@ -15,20 +32,16 @@ func (c Carbon) Season() string { } index := -1 month := c.Month() - switch { - case month == 3 || month == 4 || month == 5: - index = 0 - case month == 6 || month == 7 || month == 8: - index = 1 - case month == 9 || month == 10 || month == 11: - index = 2 - case month == 12 || month == 1 || month == 2: - index = 3 + for i := 0; i < len(seasons); i++ { + season := seasons[i] + if month == season.month { + index = season.index + } } c.lang.rw.Lock() defer c.lang.rw.Unlock() - if seasons, ok := c.lang.resources["seasons"]; ok { - slice := strings.Split(seasons, "|") + if resources, ok := c.lang.resources["seasons"]; ok { + slice := strings.Split(resources, "|") if len(slice) == QuartersPerYear { return slice[index] } diff --git a/tag.go b/tag.go index e8a9bba5..8ca0e9f8 100644 --- a/tag.go +++ b/tag.go @@ -7,18 +7,6 @@ import ( ) var ( - // invalid pointer error - // 无效的指针错误 - invalidPtrError = func() error { - return fmt.Errorf("invalid struct pointer, please make sure the struct is a pointer") - } - - // invalid tag error - // 无效的标签错误 - invalidTagError = func(field string) error { - return fmt.Errorf("invalid carbon tag in %s field, please make sure the tag is valid", field) - } - // supported types // 支持的类型 tagTypes = map[string]string{ @@ -81,6 +69,18 @@ var ( "timestampMicro": "format:X", "timestampNano": "format:Z", } + + // invalid pointer error + // 无效的指针错误 + invalidPtrError = func() error { + return fmt.Errorf("invalid struct pointer, please make sure the struct is a pointer") + } + + // invalid tag error + // 无效的标签错误 + invalidTagError = func(field string) error { + return fmt.Errorf("invalid carbon tag in %s field, please make sure the tag is valid", field) + } ) // tag defines a tag struct. diff --git a/traveler.go b/traveler.go index dce54510..fc1fa33d 100755 --- a/traveler.go +++ b/traveler.go @@ -77,7 +77,7 @@ func (c Carbon) AddDuration(duration string) Carbon { return c } td, err := parseByDuration(duration) - c.time, c.Error = c.ToStdTime().Add(td), err + c.time, c.Error = c.StdTime().Add(td), err return c } @@ -189,7 +189,7 @@ func (c Carbon) AddYears(years int) Carbon { if c.IsInvalid() { return c } - c.time = c.ToStdTime().AddDate(years, 0, 0) + c.time = c.StdTime().AddDate(years, 0, 0) return c } @@ -302,7 +302,7 @@ func (c Carbon) AddMonths(months int) Carbon { if c.IsInvalid() { return c } - c.time = c.ToStdTime().AddDate(0, months, 0) + c.time = c.StdTime().AddDate(0, months, 0) return c } @@ -388,7 +388,7 @@ func (c Carbon) AddDays(days int) Carbon { if c.IsInvalid() { return c } - c.time = c.ToStdTime().AddDate(0, 0, days) + c.time = c.StdTime().AddDate(0, 0, days) return c } @@ -417,7 +417,7 @@ func (c Carbon) AddHours(hours int) Carbon { return c } td := time.Duration(hours) * time.Hour - c.time = c.ToStdTime().Add(td) + c.time = c.StdTime().Add(td) return c } @@ -446,7 +446,7 @@ func (c Carbon) AddMinutes(minutes int) Carbon { return c } td := time.Duration(minutes) * time.Minute - c.time = c.ToStdTime().Add(td) + c.time = c.StdTime().Add(td) return c } @@ -475,7 +475,7 @@ func (c Carbon) AddSeconds(seconds int) Carbon { return c } td := time.Duration(seconds) * time.Second - c.time = c.ToStdTime().Add(td) + c.time = c.StdTime().Add(td) return c } @@ -504,7 +504,7 @@ func (c Carbon) AddMilliseconds(milliseconds int) Carbon { return c } td := time.Duration(milliseconds) * time.Millisecond - c.time = c.ToStdTime().Add(td) + c.time = c.StdTime().Add(td) return c } @@ -533,7 +533,7 @@ func (c Carbon) AddMicroseconds(microseconds int) Carbon { return c } td := time.Duration(microseconds) * time.Microsecond - c.time = c.ToStdTime().Add(td) + c.time = c.StdTime().Add(td) return c } @@ -562,7 +562,7 @@ func (c Carbon) AddNanoseconds(nanoseconds int) Carbon { return c } td := time.Duration(nanoseconds) * time.Nanosecond - c.time = c.ToStdTime().Add(td) + c.time = c.StdTime().Add(td) return c }