Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	calendar/lunar/lunar.go
  • Loading branch information
gouguoyin committed Jan 30, 2024
2 parents 6593369 + b1bea4f commit 1686d61
Show file tree
Hide file tree
Showing 23 changed files with 614 additions and 509 deletions.
22 changes: 11 additions & 11 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions README.jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 12 additions & 14 deletions calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 13 additions & 0 deletions calendar/gregorian.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
72 changes: 62 additions & 10 deletions calendar/gregorian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
})
}
}
Loading

0 comments on commit 1686d61

Please sign in to comment.