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