diff --git a/README.cn.md b/README.cn.md index b1504c4..a8f56dc 100644 --- a/README.cn.md +++ b/README.cn.md @@ -44,17 +44,19 @@ import ( go get -u github.com/golang-module/carbon import ( - "github.com/golang-module/carbon" +"github.com/golang-module/carbon" ) // 使用 gitee 库 go get -u gitee.com/golang-module/carbon import ( - "gitee.com/golang-module/carbon" +"gitee.com/golang-module/carbon" ) ``` +> v1 和 v2 版本区别请查阅 常见问题 + #### 用法示例 > 默认时区为 Local,即服务器所在时区,假设当前时间为 2020-08-05 13:14:15 @@ -625,6 +627,34 @@ carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true carbon.Parse("2020-08-06").IsTomorrow() // true +// 是否是同一世纪 +carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("3020-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("2099-08-05 13:14:15")) // true +// 是否是同一年代 +carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2030-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2120-08-05 13:14:15")) // true +// 是否是同一年 +carbon.Parse("2020-08-05 00:00:00").IsSameYear(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameYear(carbon.Parse("2020-12-31 13:14:15")) // true +// 是否是同一季节 +carbon.Parse("2020-08-05 00:00:00").IsSameQuarter(carbon.Parse("2020-09-05 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameQuarter(carbon.Parse("2021-01-31 13:14:15")) // true +// 是否是同一月 +carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2021-01-31 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2020-01-31 13:14:15")) // true +// 是否是同一天 +carbon.Parse("2020-08-05 13:14:15").IsSameDay(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 00:00:00").IsSameDay(carbon.Parse("2020-08-05 13:14:15")) // true +// 是否是同一小时 +carbon.Parse("2020-08-05 13:14:15").IsSameHour(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:00:00").IsSameHour(carbon.Parse("2020-08-05 13:14:15")) // true +// 是否是同一分钟 +carbon.Parse("2020-08-05 13:14:15").IsSameMinute(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:00").IsSameMinute(carbon.Parse("2020-08-05 13:14:15")) // true +// 是否是同一秒 +carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2020-08-05 13:14:15")) // true + // 是否大于 carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-04 13:14:15")) // true carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-05 13:14:15")) // false @@ -1205,22 +1235,21 @@ type Person struct { ###### 实例化模型 ```go -now := carbon.Now() person := Person { Name: "gouguoyin", Age: 18, - Birthday1: carbon.DateTime{now.SubYears(18)}, - Birthday2: carbon.DateTime{now.SubYears(18)}, - Birthday3: carbon.DateTime{now.SubYears(18)}, - Birthday4: carbon.DateTime{now.SubYears(18)}, - GraduatedAt1: carbon.Date{now}, - GraduatedAt2: carbon.Date{now}, - GraduatedAt3: carbon.Date{now}, - GraduatedAt4: carbon.Date{now}, - CreatedAt1: carbon.Timestamp{now}, - CreatedAt2: carbon.TimestampMilli{now}, - CreatedAt3: carbon.TimestampMicro{now}, - CreatedAt4: carbon.TimestampNano{now}, + Birthday1: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday2: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday3: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday4: carbon.DateTime{carbon.Now().SubYears(18)}, + GraduatedAt1: carbon.Date{carbon.Now()}, + GraduatedAt2: carbon.Date{carbon.Now()}, + GraduatedAt3: carbon.Date{carbon.Now()}, + GraduatedAt4: carbon.Date{carbon.Now()}, + CreatedAt1: carbon.Timestamp{carbon.Now()}, + CreatedAt2: carbon.TimestampMilli{carbon.Now()}, + CreatedAt3: carbon.TimestampMicro{carbon.Now()}, + CreatedAt4: carbon.TimestampNano{carbon.Now()}, } ``` @@ -1245,10 +1274,10 @@ fmt.Printf("%s", data) "graduated_at2": "2020-08-05.999", "graduated_at3": "2020-08-05.999999", "graduated_at4": "2020-08-05.999999999", - "created_at1": 1596604455, - "created_at2": 1596604455999, - "created_at3": 1596604455999999, - "created_at4": 1596604455999999999 + "created_at1": 1596604455, + "created_at2": 1596604455999, + "created_at3": 1596604455999999, + "created_at4": 1596604455999999999 } ``` @@ -1297,6 +1326,7 @@ fmt.Printf("%+v", *person) * [葡萄牙语(pt)](./lang/pt.json "葡萄牙语"): 由 [felipear89](https://github.com/felipear89 "felipear89") 翻译 * [俄罗斯语(ru)](./lang/ru.json "俄罗斯语"): 由 [zemlyak](https://github.com/zemlyak "zemlyak") 翻译 * [乌克兰语(uk)](./lang/uk.json "乌克兰语"): 由 [open-git](https://github.com/open-git "open-git") 翻译 +* [罗马尼亚语(ro)](./lang/ro.json "罗马尼亚语"): 由 [DrOctavius](https://github.com/DrOctavius "DrOctavius") 翻译 目前支持的方法有 @@ -1450,6 +1480,13 @@ invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for | Q | 当前季节 | 1 | 1-4 | 1 | | C | 当前世纪数 | - | 0-99 | 21 | +#### 常见问题 + +1、v1 和 v2 版本有什么区别? +> v1 和 v2 版本的 API 没有任何区别,只是 `language.go` 里翻译资源文件内嵌的实现方式不同,v1 版本是由第三方扩展库 [packr](https://github.com/gobuffalo/packr) +> 实现的,v2 +> 版本是由 `golang1.16` 后内置标准库 [embed](https://pkg.go.dev/embed) 实现的,推荐使用 v2 版本。 + #### 参考项目 * [briannesbitt/carbon](https://github.com/briannesbitt/Carbon) @@ -1460,9 +1497,10 @@ invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for * [iamkun/dayjs](https://github.com/iamkun/dayjs) #### 贡献者 + 感谢以下所有为 `Carbon` 做出贡献的人: - + #### 赞助 diff --git a/README.jp.md b/README.jp.md index 3bcc6c4..354eae5 100644 --- a/README.jp.md +++ b/README.jp.md @@ -54,7 +54,9 @@ import ( "gitee.com/golang-module/carbon" ) ``` +> v1とv2の違いについては、人気のある問題をご覧ください。 +人気のある問題 #### 使い方の例 > デフォルトのタイムゾーンはLocalです。つまりサーバのタイムゾーンです, 現在の時間は2020-08-05 13:14:15と仮定します @@ -622,6 +624,34 @@ carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true carbon.Parse("2020-08-06").IsTomorrow() // true +// 同じ世紀ですか +carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("3020-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("2099-08-05 13:14:15")) // true +// 同じ時代ですか +carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2030-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2120-08-05 13:14:15")) // true +// 同じ年ですか +carbon.Parse("2020-08-05 00:00:00").IsSameYear(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameYear(carbon.Parse("2020-12-31 13:14:15")) // true +// 同じ季節ですか +carbon.Parse("2020-08-05 00:00:00").IsSameQuarter(carbon.Parse("2020-09-05 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameQuarter(carbon.Parse("2021-01-31 13:14:15")) // true +// 同じ月ですか +carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2021-01-31 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2020-01-31 13:14:15")) // true +// 同じ日ですか +carbon.Parse("2020-08-05 13:14:15").IsSameDay(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 00:00:00").IsSameDay(carbon.Parse("2020-08-05 13:14:15")) // true +// 同じ時間ですか +carbon.Parse("2020-08-05 13:14:15").IsSameHour(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:00:00").IsSameHour(carbon.Parse("2020-08-05 13:14:15")) // true +// 同じ分ですか +carbon.Parse("2020-08-05 13:14:15").IsSameMinute(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:00").IsSameMinute(carbon.Parse("2020-08-05 13:14:15")) // true +// 同じ秒ですか +carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2020-08-05 13:14:15")) // true + // 大きいかどうか carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-04 13:14:15")) // true carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-05 13:14:15")) // false @@ -1201,22 +1231,21 @@ type Person struct { ###### 初期化モデル ```go -now := carbon.Now() person := Person { Name: "gouguoyin", Age: 18, - Birthday1: carbon.DateTime{now.SubYears(18)}, - Birthday2: carbon.DateTime{now.SubYears(18)}, - Birthday3: carbon.DateTime{now.SubYears(18)}, - Birthday4: carbon.DateTime{now.SubYears(18)}, - GraduatedAt1: carbon.Date{now}, - GraduatedAt2: carbon.Date{now}, - GraduatedAt3: carbon.Date{now}, - GraduatedAt4: carbon.Date{now}, - CreatedAt1: carbon.Timestamp{now}, - CreatedAt2: carbon.TimestampMilli{now}, - CreatedAt3: carbon.TimestampMicro{now}, - CreatedAt4: carbon.TimestampNano{now}, + Birthday1: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday2: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday3: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday4: carbon.DateTime{carbon.Now().SubYears(18)}, + GraduatedAt1: carbon.Date{carbon.Now()}, + GraduatedAt2: carbon.Date{carbon.Now()}, + GraduatedAt3: carbon.Date{carbon.Now()}, + GraduatedAt4: carbon.Date{carbon.Now()}, + CreatedAt1: carbon.Timestamp{carbon.Now()}, + CreatedAt2: carbon.TimestampMilli{carbon.Now()}, + CreatedAt3: carbon.TimestampMicro{carbon.Now()}, + CreatedAt4: carbon.TimestampNano{carbon.Now()}, } ``` @@ -1225,8 +1254,8 @@ person := Person { ```go data, err := json.Marshal(&person) if err != nil { - // エラー処理... - log.Fatal(c.Error) + // エラー処理... + log.Fatal(c.Error) } fmt.Printf("%s", data) // 出力 @@ -1270,8 +1299,8 @@ str := `{ person := new(Person) err := json.Unmarshal([]byte(str), &person) if err != nil { - // エラー処理... - log.Fatal(c.Error) + // エラー処理... + log.Fatal(c.Error) } fmt.Printf("%+v", *person) // 出力 @@ -1293,6 +1322,7 @@ fmt.Printf("%+v", *person) * [ポルトガル語(pt)](./lang/pt.json "ポルトガル語"):[felipear89](https://github.com/felipear89 "felipear89") から翻訳されます * [ロシア語(ru)](./lang/ru.json "ロシア語"):[zemlyak](https://github.com/zemlyak "zemlyak") から翻訳されます * [ウクライナ語(uk)](./lang/uk.json "ウクライナ語"):[open-git](https://github.com/open-git "open-git") から翻訳されます +* [ルーマニア語(ro)](./lang/ro.json "ルーマニア語"): [DrOctavius](https://github.com/DrOctavius "DrOctavius") から翻訳されます 現在サポートされている方法 @@ -1446,6 +1476,13 @@ invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for | Q | 季節 | 1 | 1-4 | 1 | | C | 世紀 | - | 0-99 | 21 | +#### 人気のある問題 + +1、v1とv2のバージョンの違いは何ですか? +> APIのv1バージョンとv2バージョンに違いはありませんが `language.go` +> での翻訳リソースファイルの実装は異なります。v1は、サードパーティの拡張ライブラリ [packr](https://github.com/gobuffalo/packr) によって実装されています,v2は、 `golang1.16` +> の後に組み込みの標準ライブラリ [embed](https://pkg.go.dev/embed) によって実装されています。v2バージョンをお勧めします。 + #### 参考文献 * [briannesbitt/carbon](https://github.com/briannesbitt/Carbon) @@ -1456,9 +1493,10 @@ invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for * [iamkun/dayjs](https://github.com/iamkun/dayjs) #### 寄稿者 + `Carbon` に貢献してくれた以下のすべてに感謝します: - + #### スポンサー diff --git a/README.md b/README.md index 30c30ac..a06909b 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ import ( "gitee.com/golang-module/carbon" ) ``` +> Please refer to FAQ for the difference between v1 and v2 #### Usage and example @@ -622,6 +623,34 @@ carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true carbon.Parse("2020-08-06").IsTomorrow() // true +// Whether is same century +carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("3020-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameCentury(carbon.Parse("2099-08-05 13:14:15")) // true +// Whether is same decade +carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2030-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameDecade(carbon.Parse("2120-08-05 13:14:15")) // true +// Whether is same year +carbon.Parse("2020-08-05 00:00:00").IsSameYear(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameYear(carbon.Parse("2020-12-31 13:14:15")) // true +// Whether is same quarter +carbon.Parse("2020-08-05 00:00:00").IsSameQuarter(carbon.Parse("2020-09-05 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameQuarter(carbon.Parse("2021-01-31 13:14:15")) // true +// Whether is same month +carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2021-01-31 13:14:15")) // false +carbon.Parse("2020-01-01 00:00:00").IsSameMonth(carbon.Parse("2020-01-31 13:14:15")) // true +// Whether is same day +carbon.Parse("2020-08-05 13:14:15").IsSameDay(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 00:00:00").IsSameDay(carbon.Parse("2020-08-05 13:14:15")) // true +// Whether is same hour +carbon.Parse("2020-08-05 13:14:15").IsSameHour(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:00:00").IsSameHour(carbon.Parse("2020-08-05 13:14:15")) // true +// Whether is same minute +carbon.Parse("2020-08-05 13:14:15").IsSameMinute(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:00").IsSameMinute(carbon.Parse("2020-08-05 13:14:15")) // true +// Whether is same second +carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2021-08-05 13:14:15")) // false +carbon.Parse("2020-08-05 13:14:15").IsSameSecond(carbon.Parse("2020-08-05 13:14:15")) // true + // Whether greater than carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-04 13:14:15")) // true carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-05 13:14:15")) // false @@ -1200,22 +1229,21 @@ type Person struct { ###### Instantiate model ```go -now := carbon.Now() person := Person { Name: "gouguoyin", Age: 18, - Birthday1: carbon.DateTime{now.SubYears(18)}, - Birthday2: carbon.DateTime{now.SubYears(18)}, - Birthday3: carbon.DateTime{now.SubYears(18)}, - Birthday4: carbon.DateTime{now.SubYears(18)}, - GraduatedAt1: carbon.Date{now}, - GraduatedAt2: carbon.Date{now}, - GraduatedAt3: carbon.Date{now}, - GraduatedAt4: carbon.Date{now}, - CreatedAt1: carbon.Timestamp{now}, - CreatedAt2: carbon.TimestampMilli{now}, - CreatedAt3: carbon.TimestampMicro{now}, - CreatedAt4: carbon.TimestampNano{now}, + Birthday1: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday2: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday3: carbon.DateTime{carbon.Now().SubYears(18)}, + Birthday4: carbon.DateTime{carbon.Now().SubYears(18)}, + GraduatedAt1: carbon.Date{carbon.Now()}, + GraduatedAt2: carbon.Date{carbon.Now()}, + GraduatedAt3: carbon.Date{carbon.Now()}, + GraduatedAt4: carbon.Date{carbon.Now()}, + CreatedAt1: carbon.Timestamp{carbon.Now()}, + CreatedAt2: carbon.TimestampMilli{carbon.Now()}, + CreatedAt3: carbon.TimestampMicro{carbon.Now()}, + CreatedAt4: carbon.TimestampNano{carbon.Now()}, } ``` @@ -1224,8 +1252,8 @@ person := Person { ```go data, err := json.Marshal(&person) if err != nil { - // Error handle... - log.Fatal(err) + // Error handle... + log.Fatal(err) } fmt.Printf("%s", data) // Output @@ -1269,8 +1297,8 @@ str := `{ person := new(Person) err := json.Unmarshal([]byte(str), &person) if err != nil { - // Error handle... - log.Fatal(err) + // Error handle... + log.Fatal(err) } fmt.Printf("%+v", *person) // Output @@ -1294,6 +1322,7 @@ The following languages are supported * [Portuguese(pt)](./lang/pt.json "Portuguese"): translated by [felipear89](https://github.com/felipear89 "felipear89") * [Russian(ru)](./lang/ru.json "Russian"): translated by [zemlyak](https://github.com/zemlyak "zemlyak") * [Ukrainian(uk)](./lang/uk.json "Ukrainian"): translated by [open-git](https://github.com/open-git "open-git") +* [Romanian(ro)](./lang/ro.json "Romanian"): translated by [DrOctavius](https://github.com/DrOctavius "DrOctavius") The following methods are supported @@ -1447,6 +1476,13 @@ invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for | Q | Quarter | 1 | 1-4 | 1 | | C | Century | - | 0-99 | 21 | +#### FAQ + +1、What is the difference between v1 and v2? +> There is no difference between v1 and v2 of the API, but the implementation of the translation resource files +> in `language.go` is different. The v1 is implemented by the third-party extension +> library [packr](https://github.com/gobuffalo/packr), and the v2 is implemented by the standard +> library [embed](https://pkg.go.dev/embed) after `golang1.16`. The v2 is recommended. #### References @@ -1458,9 +1494,10 @@ invalid timezone "xxx", please see the file "$GOROOT/lib/time/zoneinfo.zip" for * [iamkun/dayjs](https://github.com/iamkun/dayjs) #### Contributors + Thanks to all of the following who contributed to `Carbon`: - + #### Sponsors diff --git a/comparer.go b/comparer.go index 932df35..3489271 100755 --- a/comparer.go +++ b/comparer.go @@ -291,6 +291,87 @@ func (c Carbon) IsTomorrow() bool { return c.ToDateString() == Tomorrow().ToDateString() } +// IsSameCentury reports whether is same century. +// 是否是同一世纪 +func (c Carbon) IsSameCentury(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.Century() == t.Century() +} + +// IsSameDecade reports whether is same decade. +// 是否是同一年代 +func (c Carbon) IsSameDecade(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.Decade() == t.Decade() +} + +// IsSameYear reports whether is same year. +// 是否是同一年 +func (c Carbon) IsSameYear(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.Year() == t.Year() +} + +// IsSameQuarter reports whether is same quarter. +// 是否是同一季节 +func (c Carbon) IsSameQuarter(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.Quarter() == t.Quarter() +} + +// IsSameMonth reports whether is same month. +// 是否是同一月 +func (c Carbon) IsSameMonth(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.Year() == t.Year() && c.Month() == t.Month() +} + +// IsSameDay reports whether is same day. +// 是否是同一天 +func (c Carbon) IsSameDay(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.ToDateString() == t.ToDateString() +} + +// IsSameHour reports whether is same hour. +// 是否是同一小时 +func (c Carbon) IsSameHour(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.ToDateString() == t.ToDateString() && c.Hour() == t.Hour() +} + +// IsSameMinute reports whether is same minute. +// 是否是同一分钟 +func (c Carbon) IsSameMinute(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.ToDateString() == t.ToDateString() && c.Hour() == t.Hour() && c.Minute() == t.Minute() +} + +// IsSameSecond reports whether is same second. +// 是否是同一秒 +func (c Carbon) IsSameSecond(t Carbon) bool { + if c.IsInvalid() || t.IsInvalid() { + return false + } + return c.ToDateTimeString() == t.ToDateTimeString() +} + // Compare compares by an operator. // 时间比较 func (c Carbon) Compare(operator string, t Carbon) bool { diff --git a/comparer_test.go b/comparer_test.go index 4d80883..c7325eb 100755 --- a/comparer_test.go +++ b/comparer_test.go @@ -938,6 +938,187 @@ func TestCarbon_IsTomorrow(t *testing.T) { } } +func TestCarbon_IsSameCentury(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05"), Parse("3020-08-05"), false}, + {Parse("2020-08-05"), Parse("2099-08-05"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameCentury(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameDecade(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05"), Parse("2030-08-05"), false}, + {Parse("2020-08-05"), Parse("2021-08-05"), true}, + {Parse("2020-01-01"), Parse("2120-01-31"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameDecade(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameYear(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05"), Parse("2021-08-05"), false}, + {Parse("2020-01-01"), Parse("2020-12-31"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameYear(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameQuarter(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05"), Parse("2020-01-05"), false}, + {Parse("2020-01-01"), Parse("2020-01-31"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameQuarter(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameMonth(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05"), Parse("2021-08-05"), false}, + {Parse("2020-01-01"), Parse("2020-01-31"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameMonth(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameDay(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05 13:14:15"), Parse("2021-08-05 13:14:15"), false}, + {Parse("2020-08-05 00:00:00"), Parse("2020-08-05 13:14:15"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameDay(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameHour(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05 13:14:15"), Parse("2021-08-05 13:14:15"), false}, + {Parse("2020-08-05 13:00:00"), Parse("2020-08-05 13:14:15"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameHour(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameMinute(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05 13:14:15"), Parse("2021-08-05 13:14:15"), false}, + {Parse("2020-08-05 13:14:00"), Parse("2020-08-05 13:14:15"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameMinute(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_IsSameSecond(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + input1 Carbon // 输入值 + input2 Carbon // 输入值 + expected bool // 期望值 + }{ + {Parse(""), Parse(""), false}, + {Parse("2020-08-05 13:14:15"), Parse("2021-08-05 13:14:15"), false}, + {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), true}, + } + + for index, test := range tests { + assert.Nil(test.input1.Error) + assert.Nil(test.input2.Error) + assert.Equal(test.expected, test.input1.IsSameSecond(test.input2), "Current test index is "+strconv.Itoa(index)) + } +} + func TestCarbon_Compare(t *testing.T) { assert := assert.New(t)