增加方法:

1、获取n分钟前的时间
2、获取n分钟后的时间
3、比较
master v1.0.0
李光春 2 years ago
parent 48efcb04a9
commit 3348258e4a

@ -1,15 +1,25 @@
# README
<h1>
<a href="https://www.dtapp.net/">Golang Time</a>
</h1>
[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"
)
```

@ -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))

@ -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))
}

@ -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
}

@ -1,3 +1,3 @@
module gitee.com/dtapps/gotime
module github.com/dtapps/gotime
go 1.17
go 1.18

Loading…
Cancel
Save