1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

feat: add IsLeapYear

This commit is contained in:
dudaodong
2023-04-28 14:42:25 +08:00
parent 219e31d929
commit 945c59896b
5 changed files with 263 additions and 161 deletions

View File

@@ -218,3 +218,9 @@ func BeginOfYear(t time.Time) time.Time {
func EndOfYear(t time.Time) time.Time { func EndOfYear(t time.Time) time.Time {
return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond) return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
} }
// IsLeapYear check if param year is leap year or not.
// Play: todo
func IsLeapYear(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}

View File

@@ -321,3 +321,15 @@ func ExampleNewUnixNow() {
// // Output: // // Output:
// // 2006-01-02T23:04:05+08:00 // // 2006-01-02T23:04:05+08:00
// } // }
func ExampleIsLeapYear() {
result1 := IsLeapYear(2000)
result2 := IsLeapYear(2001)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}

View File

@@ -230,3 +230,13 @@ func TestEndOfYear(t *testing.T) {
assert.Equal(expected, actual) assert.Equal(expected, actual)
} }
func TestIsLeapYear(t *testing.T) {
assert := internal.NewAssert(t, "TestEndOfYear")
result1 := IsLeapYear(2000)
result2 := IsLeapYear(2001)
assert.Equal(true, result1)
assert.Equal(false, result2)
}

View File

@@ -53,6 +53,7 @@ import (
- [ToFormat](#ToFormat) - [ToFormat](#ToFormat)
- [ToFormatForTpl](#ToFormatForTpl) - [ToFormatForTpl](#ToFormatForTpl)
- [ToIso8601](#ToIso8601) - [ToIso8601](#ToIso8601)
- [IsLeapYear](#IsLeapYear)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1062,3 +1063,37 @@ func main() {
// 2006-01-02T23:04:05+08:00 // 2006-01-02T23:04:05+08:00
} }
``` ```
### <span id="IsLeapYear">IsLeapYear</span>
<p>check if param `year` is leap year or not.</p>
<b>Signature:</b>
```go
func IsLeapYear(year int) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/datetime"
)
func main() {
result1 := datetime.IsLeapYear(2000)
result2 := datetime.IsLeapYear(2001)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```

View File

@@ -1,15 +1,17 @@
# Datetime # Datetime
datetime日期时间处理包格式化日期比较日期。
datetime 日期时间处理包,格式化日期,比较日期。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go) - [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 用法: ## 用法:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/datetime" "github.com/duke-git/lancet/v2/datetime"
@@ -19,74 +21,77 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [AddDay](#AddDay)
- [AddHour](#AddHour) - [AddDay](#AddDay)
- [AddMinute](#AddMinute) - [AddHour](#AddHour)
- [BeginOfMinute](#BeginOfMinute) - [AddMinute](#AddMinute)
- [BeginOfHour](#BeginOfHour) - [BeginOfMinute](#BeginOfMinute)
- [BeginOfDay](#BeginOfDay) - [BeginOfHour](#BeginOfHour)
- [BeginOfWeek](#BeginOfWeek) - [BeginOfDay](#BeginOfDay)
- [BeginOfMonth](#BeginOfMonth) - [BeginOfWeek](#BeginOfWeek)
- [BeginOfYear](#BeginOfYear) - [BeginOfMonth](#BeginOfMonth)
- [EndOfMinute](#EndOfMinute) - [BeginOfYear](#BeginOfYear)
- [EndOfHour](#EndOfHour) - [EndOfMinute](#EndOfMinute)
- [EndOfDay](#EndOfDay) - [EndOfHour](#EndOfHour)
- [EndOfWeek](#EndOfWeek) - [EndOfDay](#EndOfDay)
- [EndOfMonth](#EndOfMonth) - [EndOfWeek](#EndOfWeek)
- [EndOfYear](#EndOfYear) - [EndOfMonth](#EndOfMonth)
- [GetNowDate](#GetNowDate) - [EndOfYear](#EndOfYear)
- [GetNowTime](#GetNowTime) - [GetNowDate](#GetNowDate)
- [GetNowDateTime](#GetNowDateTime) - [GetNowTime](#GetNowTime)
- [GetZeroHourTimestamp](#GetZeroHourTimestamp) - [GetNowDateTime](#GetNowDateTime)
- [GetNightTimestamp](#GetNightTimestamp) - [GetZeroHourTimestamp](#GetZeroHourTimestamp)
- [FormatTimeToStr](#FormatTimeToStr) - [GetNightTimestamp](#GetNightTimestamp)
- [FormatStrToTime](#FormatStrToTime) - [FormatTimeToStr](#FormatTimeToStr)
- [NewUnixNow](#NewUnixNow) - [FormatStrToTime](#FormatStrToTime)
- [NewUnix](#NewUnix) - [NewUnixNow](#NewUnixNow)
- [NewFormat](#NewFormat) - [NewUnix](#NewUnix)
- [NewISO8601](#NewISO8601) - [NewFormat](#NewFormat)
- [ToUnix](#ToUnix) - [NewISO8601](#NewISO8601)
- [ToFormat](#ToFormat) - [ToUnix](#ToUnix)
- [ToFormatForTpl](#ToFormatForTpl) - [ToFormat](#ToFormat)
- [ToIso8601](#ToIso8601) - [ToFormatForTpl](#ToFormatForTpl)
- [ToIso8601](#ToIso8601)
- [IsLeapYear](#IsLeapYear)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
## 注: ## 注:
1. 方法FormatTimeToStr和FormatStrToTime中的format参数值需要传以下类型之一
- yyyy-mm-dd hh:mm:ss
- yyyy-mm-dd hh:mm
- yyyy-mm-dd hh
- yyyy-mm-dd
- yyyy-mm
- mm-dd
- dd-mm-yy hh:mm:ss
- yyyy/mm/dd hh:mm:ss
- yyyy/mm/dd hh:mm
- yyyy-mm-dd hh
- yyyy/mm/dd
- yyyy/mm
- mm/dd
- dd/mm/yy hh:mm:ss
- yyyy
- mm
- hh:mm:ss
- mm:ss
1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一:
- yyyy-mm-dd hh:mm:ss
- yyyy-mm-dd hh:mm
- yyyy-mm-dd hh
- yyyy-mm-dd
- yyyy-mm
- mm-dd
- dd-mm-yy hh:mm:ss
- yyyy/mm/dd hh:mm:ss
- yyyy/mm/dd hh:mm
- yyyy-mm-dd hh
- yyyy/mm/dd
- yyyy/mm
- mm/dd
- dd/mm/yy hh:mm:ss
- yyyy
- mm
- hh:mm:ss
- mm:ss
### <span id="AddDay">AddDay</span> ### <span id="AddDay">AddDay</span>
<p>将日期加/减天数。</p> <p>将日期加/减天数。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func AddDay(t time.Time, day int64) time.Time func AddDay(t time.Time, day int64) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -119,13 +124,13 @@ func main() {
<p>将日期加/减小时数。</p> <p>将日期加/减小时数。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func AddHour(t time.Time, hour int64) time.Time func AddHour(t time.Time, hour int64) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -158,13 +163,13 @@ func main() {
<p>将日期加/减分钟数。</p> <p>将日期加/减分钟数。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func AddMinute(t time.Time, minute int64) time.Time func AddMinute(t time.Time, minute int64) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -197,13 +202,13 @@ func main() {
<p>返回指定时间的分钟开始时间。</p> <p>返回指定时间的分钟开始时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func BeginOfMinute(t time.Time) time.Time func BeginOfMinute(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -229,13 +234,13 @@ func main() {
<p>返回指定时间的小时开始时间。</p> <p>返回指定时间的小时开始时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func BeginOfHour(t time.Time) time.Time func BeginOfHour(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -261,13 +266,13 @@ func main() {
<p>返回指定时间的当天开始时间。</p> <p>返回指定时间的当天开始时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func BeginOfDay(t time.Time) time.Time func BeginOfDay(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -293,13 +298,13 @@ func main() {
<p>返回指定时间的每周开始时间,默认开始时间星期日。</p> <p>返回指定时间的每周开始时间,默认开始时间星期日。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -325,13 +330,13 @@ func main() {
<p>返回指定时间的当月开始时间。</p> <p>返回指定时间的当月开始时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func BeginOfMonth(t time.Time) time.Time func BeginOfMonth(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -357,13 +362,13 @@ func main() {
<p>返回指定时间的当年开始时间</p> <p>返回指定时间的当年开始时间</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func BeginOfYear(t time.Time) time.Time func BeginOfYear(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -389,13 +394,13 @@ func main() {
<p>返回指定时间的分钟结束时间。</p> <p>返回指定时间的分钟结束时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func EndOfMinute(t time.Time) time.Time func EndOfMinute(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -421,13 +426,13 @@ func main() {
<p>返回指定时间的小时结束时间。</p> <p>返回指定时间的小时结束时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func EndOfHour(t time.Time) time.Time func EndOfHour(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -453,13 +458,13 @@ func main() {
<p>返回指定时间的当天结束时间。</p> <p>返回指定时间的当天结束时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func EndOfDay(t time.Time) time.Time func EndOfDay(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -485,13 +490,13 @@ func main() {
<p>返回指定时间的星期结束时间,默认结束时间星期六。</p> <p>返回指定时间的星期结束时间,默认结束时间星期六。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -517,13 +522,13 @@ func main() {
<p>返回指定时间的当月结束时间。</p> <p>返回指定时间的当月结束时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func EndOfMonth(t time.Time) time.Time func EndOfMonth(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -549,13 +554,13 @@ func main() {
<p>返回指定时间的当年结束时间。</p> <p>返回指定时间的当年结束时间。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func EndOfYear(t time.Time) time.Time func EndOfYear(t time.Time) time.Time
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -581,13 +586,13 @@ func main() {
<p>获取当天日期返回格式yyyy-mm-dd。</p> <p>获取当天日期返回格式yyyy-mm-dd。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func GetNowDate() string func GetNowDate() string
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -613,13 +618,13 @@ func main() {
<p>获取当时时间返回格式hh:mm:ss</p> <p>获取当时时间返回格式hh:mm:ss</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func GetNowTime() string func GetNowTime() string
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -645,13 +650,13 @@ func main() {
<p>获取当时日期和时间返回格式yyyy-mm-dd hh:mm:ss。</p> <p>获取当时日期和时间返回格式yyyy-mm-dd hh:mm:ss。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func GetNowDateTime() string func GetNowDateTime() string
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -677,13 +682,13 @@ func main() {
<p>获取零点时间戳(timestamp of 00:00)</p> <p>获取零点时间戳(timestamp of 00:00)</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func GetZeroHourTimestamp() int64 func GetZeroHourTimestamp() int64
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -709,13 +714,13 @@ func main() {
<p>获取午夜时间戳(timestamp of 23:59)。</p> <p>获取午夜时间戳(timestamp of 23:59)。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func GetNightTimestamp() int64 func GetNightTimestamp() int64
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -741,13 +746,13 @@ func main() {
<p>将日期格式化成字符串,`format` 参数格式参考注1。</p> <p>将日期格式化成字符串,`format` 参数格式参考注1。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func FormatTimeToStr(t time.Time, format string) string func FormatTimeToStr(t time.Time, format string) string
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -780,13 +785,13 @@ func main() {
<p>将字符串格式化成时间,`format` 参数格式参考注1。</p> <p>将字符串格式化成时间,`format` 参数格式参考注1。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func FormatStrToTime(str, format string) (time.Time, error) func FormatStrToTime(str, format string) (time.Time, error)
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -816,7 +821,7 @@ func main() {
<p>创建一个当前时间的unix时间戳。</p> <p>创建一个当前时间的unix时间戳。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
type theTime struct { type theTime struct {
@@ -825,7 +830,7 @@ type theTime struct {
func NewUnixNow() *theTime func NewUnixNow() *theTime
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -848,7 +853,7 @@ func main() {
<p>创建一个unix时间戳。</p> <p>创建一个unix时间戳。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
type theTime struct { type theTime struct {
@@ -857,7 +862,7 @@ type theTime struct {
func NewUnix(unix int64) *theTime func NewUnix(unix int64) *theTime
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -880,7 +885,7 @@ func main() {
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳。</p> <p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
type theTime struct { type theTime struct {
@@ -889,7 +894,7 @@ type theTime struct {
func NewFormat(t string) (*theTime, error) func NewFormat(t string) (*theTime, error)
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -912,7 +917,7 @@ func main() {
<p>创建一个iso8601格式时间字符串的unix时间戳。</p> <p>创建一个iso8601格式时间字符串的unix时间戳。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
type theTime struct { type theTime struct {
@@ -921,7 +926,7 @@ type theTime struct {
func NewISO8601(iso8601 string) (*theTime, error) func NewISO8601(iso8601 string) (*theTime, error)
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -944,13 +949,13 @@ func main() {
<p>返回unix时间戳。</p> <p>返回unix时间戳。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func (t *theTime) ToUnix() int64 func (t *theTime) ToUnix() int64
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -973,13 +978,13 @@ func main() {
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。</p> <p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func (t *theTime) ToFormat() string func (t *theTime) ToFormat() string
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -1002,13 +1007,13 @@ func main() {
<p>返回tpl格式指定的日期字符串。</p> <p>返回tpl格式指定的日期字符串。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func (t *theTime) ToFormatForTpl(tpl string) string func (t *theTime) ToFormatForTpl(tpl string) string
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -1032,13 +1037,13 @@ func main() {
<p>返回iso8601日期字符串。</p> <p>返回iso8601日期字符串。</p>
<b>Signature:</b> <b>函数签名:</b>
```go ```go
func (t *theTime) ToIso8601() string func (t *theTime) ToIso8601() string
``` ```
<b>Example:</b> <b>示例:</b>
```go ```go
package main package main
@@ -1057,3 +1062,37 @@ func main() {
// 2006-01-02T23:04:05+08:00 // 2006-01-02T23:04:05+08:00
} }
``` ```
### <span id="IsLeapYear">IsLeapYear</span>
<p>验证是否是闰年。</p>
<b>函数签名:</b>
```go
func IsLeapYear(year int) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/datetime"
)
func main() {
result1 := datetime.IsLeapYear(2000)
result2 := datetime.IsLeapYear(2001)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```