From 3348258e4af357d51c2abbc511ddc43d91943526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 29 Apr 2022 10:04:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B9=E6=B3=95=EF=BC=9A?= =?UTF-8?q?=201=E3=80=81=E8=8E=B7=E5=8F=96n=E5=88=86=E9=92=9F=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E9=97=B4=202=E3=80=81=E8=8E=B7=E5=8F=96n?= =?UTF-8?q?=E5=88=86=E9=92=9F=E5=90=8E=E7=9A=84=E6=97=B6=E9=97=B4=203?= =?UTF-8?q?=E3=80=81=E6=AF=94=E8=BE=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 +++++++++++++++-------- app.go | 14 ++++++++++++ app_test.go | 58 ++++++++++++++++++++++++++++------------------- compare.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++-- 5 files changed, 135 insertions(+), 34 deletions(-) create mode 100644 compare.go diff --git a/README.md b/README.md index d3d2e9d..bcbaa9a 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,25 @@ -# README +

+Golang Time +

-[comment]: <> (go) -[![godoc](https://pkg.go.dev/badge/gitee.com/dtapps/gotime?status.svg)](https://pkg.go.dev/gitee.com/dtapps/gotime) -[![goproxy.cn](https://goproxy.cn/stats/gitee.com/dtapps/gotime/badges/download-count.svg)](https://goproxy.cn/stats/gitee.com/dtapps/gotime) -[![goreportcard.com](https://goreportcard.com/badge/gitee.com/dtapps/gotime)](https://goreportcard.com/report/gitee.com/dtapps/gotime) -[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/gitee.com%2Fdtapps%2Fgotime) +📦 Golang 时间组件 -`go` time +[comment]: <> (go) +[![godoc](https://pkg.go.dev/badge/github.com/dtapps/gotime?status.svg)](https://pkg.go.dev/github.com/dtapps/gotime) +[![goproxy.cn](https://goproxy.cn/stats/github.com/dtapps/gotime/badges/download-count.svg)](https://goproxy.cn/stats/github.com/dtapps/gotime) +[![goreportcard.com](https://goreportcard.com/badge/github.com/dtapps/gotime)](https://goreportcard.com/report/github.com/dtapps/gotime) +[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/github.com%2Fdtapps%2Fgotime) -`go get gitee.com/dtapps/gotime` +#### 安装使用 ```go -import gitee.com/dtapps/gotime +go get -v -u github.com/dtapps/gotime ``` + +#### 导入 + +```go +import ( + "github.com/dtapps/gotime" +) +``` \ No newline at end of file diff --git a/app.go b/app.go index b10633b..3bd7dfe 100644 --- a/app.go +++ b/app.go @@ -49,6 +49,20 @@ func (p Pro) AfterSeconds(seconds int) Pro { return p } +// BeforeMinute 获取n分钟前的时间 +func (p Pro) BeforeMinute(seconds int) Pro { + st, _ := time.ParseDuration(fmt.Sprintf("-%dm", seconds)) + p.Time = p.Time.Add(st) + return p +} + +// AfterMinute 获取n分钟后的时间 +func (p Pro) AfterMinute(seconds int) Pro { + st, _ := time.ParseDuration(fmt.Sprintf("+%dm", seconds)) + p.Time = p.Time.Add(st) + return p +} + // BeforeHour 获取n小时前的时间 func (p Pro) BeforeHour(hour int) Pro { st, _ := time.ParseDuration(fmt.Sprintf("-%dh", hour)) diff --git a/app_test.go b/app_test.go index 5fb7ea8..c49b795 100644 --- a/app_test.go +++ b/app_test.go @@ -46,34 +46,37 @@ func TestTime(t *testing.T) { t.Log("本月开始时间:", Current().StartOfMonth().Format()) t.Log("本月结束时间:", Current().EndOfMonth().Format()) - //t.Log("7100秒前的时间:", Current().BeforeSeconds(7100).Format()) - //t.Log("2小时前的时间:", Current().BeforeHour(2).Format()) - //t.Log("7100秒后的时间:", Current().AfterSeconds(7100).Format()) - //t.Log("2小时后的时间:", Current().AfterHour(2).Format()) + t.Log("7100秒前的时间:", Current().BeforeSeconds(7100).Format()) + t.Log("2小时前的时间:", Current().BeforeHour(2).Format()) + t.Log("7100秒后的时间:", Current().AfterSeconds(7100).Format()) + t.Log("2小时后的时间:", Current().AfterHour(2).Format()) + + t.Log("相差多少小时(绝对值):", SetCurrentParse("2022-03-01T10:03:39+08:00").Format()) + t.Log("相差多少小时(绝对值):", SetCurrentParse("2022-03-04T11:12:47+08:00").Format()) } -func TestStartOfDay(t *testing.T) { - t.Log(Current().Format()) - t.Log(Current().StartOfDay().Format()) - t.Log(Current().EndOfDay().Format()) - t.Log(Current().Timestamp()) - t.Log(Current().StartOfDay().Timestamp()) - t.Log(Current().EndOfDay().Timestamp()) - t.Log(Current().BeforeDay(1).Format()) - t.Log(Current().BeforeDay(1).StartOfDay().Format()) - t.Log(Current().BeforeDay(1).EndOfDay().Format()) - t.Log(Current().AfterDay(1).Format()) - t.Log(Current().AfterDay(1).StartOfDay().Format()) - t.Log(Current().AfterDay(1).EndOfDay().Format()) +func TestDayStartOfEnd(t *testing.T) { + t.Log("当前时间:", Current().Format()) + t.Log("今天开始时间:", Current().StartOfDay().Format()) + t.Log("今天结束时间:", Current().EndOfDay().Format()) + t.Log("当前时间戳:", Current().Timestamp()) + t.Log("今天开始时间戳:", Current().StartOfDay().Timestamp()) + t.Log("今天结束时间戳:", Current().EndOfDay().Timestamp()) + t.Log("n天前时间:", Current().BeforeDay(1).Format()) + t.Log("n天前开始时间戳:", Current().BeforeDay(1).StartOfDay().Format()) + t.Log("n天前结束时间戳:", Current().BeforeDay(1).EndOfDay().Format()) + t.Log("n天后时间:", Current().AfterDay(1).Format()) + t.Log("n天后开始时间戳:", Current().AfterDay(1).StartOfDay().Format()) + t.Log("n天后结束时间戳:", Current().AfterDay(1).EndOfDay().Format()) } func TestDiff(t *testing.T) { - t.Log(Current().DiffInHourWithAbs(SetCurrentParse("2021-11-26 14:50:00").Time)) - t.Log(Current().DiffInHour(SetCurrentParse("2021-11-26 14:50:00").Time)) - t.Log(Current().DiffInMinutesWithAbs(SetCurrentParse("2021-11-26 14:50:00").Time)) - t.Log(Current().DiffInMinutes(SetCurrentParse("2021-11-26 14:50:00").Time)) - t.Log(SetCurrentParse("2022-03-01T10:03:39+08:00").Format()) - t.Log(SetCurrentParse("2022-03-04T11:12:47+08:00").Format()) + t.Log("相差多少分秒:", Current().DiffInSecond(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("相差多少分秒(绝对值):", Current().DiffInSecondWithAbs(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("相差多少分钟:", Current().DiffInMinutes(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("相差多少分钟(绝对值):", Current().DiffInMinutesWithAbs(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("相差多少小时:", Current().DiffInHour(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("相差多少小时(绝对值):", Current().DiffInHourWithAbs(SetCurrentParse("2021-11-26 14:50:00").Time)) } func TestUnix(t *testing.T) { @@ -122,3 +125,12 @@ func TestMT(t *testing.T) { t.Log(Current().BeforeHour(24 * (day - 1)).Format()) } + +func TestCompare(t *testing.T) { + t.Log("是否大于:", Current().Gt(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("是否小于:", Current().Lt(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("是否等于:", Current().Eq(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("是否不等于:", Current().Ne(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("是否大于等于:", Current().Gte(SetCurrentParse("2021-11-26 14:50:00").Time)) + t.Log("是否小于等于:", Current().Lte(SetCurrentParse("2021-11-26 14:50:00").Time)) +} diff --git a/compare.go b/compare.go new file mode 100644 index 0000000..bc89964 --- /dev/null +++ b/compare.go @@ -0,0 +1,65 @@ +package gotime + +import "time" + +// Gt 是否大于 +func (p Pro) Gt(t2 time.Time) bool { + return p.Time.After(t2) +} + +// Lt 是否小于 +func (p Pro) Lt(t2 time.Time) bool { + return p.Time.Before(t2) +} + +// Eq 是否等于 +func (p Pro) Eq(t2 time.Time) bool { + return p.Time.Equal(t2) +} + +// Ne 是否不等于 +func (p Pro) Ne(t2 time.Time) bool { + return !p.Eq(t2) +} + +// Gte 是否大于等于 +func (p Pro) Gte(t2 time.Time) bool { + return p.Gt(t2) || p.Eq(t2) +} + +// Lte 是否小于等于 +func (p Pro) Lte(t2 time.Time) bool { + return p.Lt(t2) || p.Eq(t2) +} + +// Between 是否在两个时间之间(不包括这两个时间) +func (p Pro) Between(start time.Time, end time.Time) bool { + if p.Gt(start) && p.Lt(end) { + return true + } + return false +} + +// BetweenIncludedStart 是否在两个时间之间(包括开始时间) +func (p Pro) BetweenIncludedStart(start time.Time, end time.Time) bool { + if p.Gte(start) && p.Lt(end) { + return true + } + return false +} + +// BetweenIncludedEnd 是否在两个时间之间(包括结束时间) +func (p Pro) BetweenIncludedEnd(start time.Time, end time.Time) bool { + if p.Gt(start) && p.Lte(end) { + return true + } + return false +} + +// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间) +func (p Pro) BetweenIncludedBoth(start time.Time, end time.Time) bool { + if p.Gte(start) && p.Lte(end) { + return true + } + return false +} diff --git a/go.mod b/go.mod index 4fb04b3..54fc872 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module gitee.com/dtapps/gotime +module github.com/dtapps/gotime -go 1.17 +go 1.18