From d269b45582ece8f72fe5581932b8011096ffff8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 4 Aug 2022 14:08:36 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0[=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=98=AF=E5=90=A6=E4=B8=BA=E6=97=B6?= =?UTF-8?q?=E9=97=B4]=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 ++++++++++++++++ gotime_test.go | 11 ++++++++--- verification.go | 16 ++++++++++++++++ verification_test.go | 7 +++++++ version.go | 2 +- version_test.go | 13 +++++++++++++ 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 verification.go create mode 100644 verification_test.go create mode 100644 version_test.go diff --git a/README.md b/README.md index 82111e6..0b24b79 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,20 @@ ```go go get -v -u go.dtapp.net/gotime +``` + +#### 使用 + +```go +package main + +import ( + "go.dtapp.net/gotime" + "testing" +) + +// TestVerification 验证字符串是否为时间 +func TestVerification(t *testing.T) { + t.Log(gotime.Verification("2022-02-05 00:00:00", gotime.DateTimeFormat)) +} ``` \ No newline at end of file diff --git a/gotime_test.go b/gotime_test.go index a77d361..5665f08 100644 --- a/gotime_test.go +++ b/gotime_test.go @@ -91,12 +91,17 @@ func Test2(t *testing.T) { } func TestGt(t *testing.T) { - t.Log(SetCurrentParse("2022-04-06 15:05:24").Time) + t.Log(SetCurrentParse("2022-07-29 15:05:24").Time) t.Log(Current().Lte(SetCurrentParse("2022-07-18 17:05:24").Time)) } +func TestCompare(t *testing.T) { + t.Log("是否大于", Current().Gt(SetCurrentParse("2022-07-29 14:35:24").Time)) + t.Log("是否小于", Current().Lt(SetCurrentParse("2022-07-29 14:35:24").Time)) +} + func TestTaoBao(t *testing.T) { - i := 1 + var i int64 = 1 for { if i > 3 { break @@ -112,7 +117,7 @@ func TestTaoBao(t *testing.T) { func TestMT(t *testing.T) { - day := 1 + var day int64 = 1 t.Log(day) t.Log(Current().BeforeHour(24 * day).Format()) t.Log(Current().BeforeHour(24 * (day - 1)).Format()) diff --git a/verification.go b/verification.go new file mode 100644 index 0000000..ad80006 --- /dev/null +++ b/verification.go @@ -0,0 +1,16 @@ +package gotime + +import "time" + +// Verification 验证字符串是否为时间 +func Verification(str, layout string) (resp time.Time, err error) { + loc, err := time.LoadLocation("Asia/Shanghai") + if err != nil { + return time.Time{}, err + } + location, err := time.ParseInLocation(layout, str, loc) + if err != nil { + return time.Time{}, err + } + return location, nil +} diff --git a/verification_test.go b/verification_test.go new file mode 100644 index 0000000..c747a45 --- /dev/null +++ b/verification_test.go @@ -0,0 +1,7 @@ +package gotime + +import "testing" + +func TestVerification(t *testing.T) { + t.Log(Verification("2022-02-05 00:00:00", DateTimeFormat)) +} diff --git a/version.go b/version.go index 15654f7..d7c9b0b 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package gotime -const Version = "1.0.4" +const Version = "1.0.5" diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..4b5c059 --- /dev/null +++ b/version_test.go @@ -0,0 +1,13 @@ +package gotime + +import "testing" + +func TestVersion(t *testing.T) { + t.Log(Version) +} + +func BenchmarkVersion(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Log(Version) + } +}