From 82597c25340030d4df80cb650f677fc82e3cf049 Mon Sep 17 00:00:00 2001 From: jason Date: Tue, 29 Oct 2024 10:58:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/time/quick.go | 21 ++++++++++++++++++++ pkg/time/time.go | 45 ++++++++++++++----------------------------- pkg/time/time_test.go | 17 ++++++++++++++++ 3 files changed, 52 insertions(+), 31 deletions(-) create mode 100644 pkg/time/quick.go create mode 100644 pkg/time/time_test.go diff --git a/pkg/time/quick.go b/pkg/time/quick.go new file mode 100644 index 0000000..848c87a --- /dev/null +++ b/pkg/time/quick.go @@ -0,0 +1,21 @@ +package time + +import ( + "sync" + "time" +) + +var quick Time +var once sync.Once + +func Quick() Time { + once.Do(func() { + go func() { + for { + quick = Now() + time.Sleep(time.Second) + } + }() + }) + return quick +} diff --git a/pkg/time/time.go b/pkg/time/time.go index 0b2ed79..d480279 100644 --- a/pkg/time/time.go +++ b/pkg/time/time.go @@ -1,42 +1,35 @@ package time import ( - "sync" "time" ) const localDateTimeFormat string = "2006-01-02 15:04:05" -type Time time.Time +type Time struct { + time.Time +} func (t *Time) MarshalJSON() ([]byte, error) { b := make([]byte, 0, len(localDateTimeFormat)+2) b = append(b, '"') - b = time.Time(*t).AppendFormat(b, localDateTimeFormat) + b = t.AppendFormat(b, localDateTimeFormat) b = append(b, '"') return b, nil } func (t *Time) UnmarshalJSON(b []byte) error { - now, err := time.ParseInLocation(`"`+localDateTimeFormat+`"`, string(b), time.Local) - *t = Time(now) + tm, err := time.ParseInLocation(`"`+localDateTimeFormat+`"`, string(b), time.Local) + *t = Time{tm} return err } func (t *Time) String() string { - return time.Time(*t).Format(localDateTimeFormat) -} - -func (t *Time) Now() Time { - return Time(time.Now()) -} - -func (t *Time) ParseTime(tm time.Time) Time { - return Time(tm) + return t.Format(localDateTimeFormat) } func (t *Time) format() string { - return time.Time(*t).Format(localDateTimeFormat) + return t.Format(localDateTimeFormat) } func (t *Time) MarshalText() ([]byte, error) { @@ -44,7 +37,12 @@ func (t *Time) MarshalText() ([]byte, error) { } func Now() Time { - return Time(time.Now()) + return Time{time.Now()} +} + +func Parse(str string) (Time, error) { + tm, err := time.Parse(str, localDateTimeFormat) + return Time{tm}, err } func After(ms int64, fn func()) { @@ -54,18 +52,3 @@ func After(ms int64, fn func()) { func Sleep(ms int64, fn func()) { time.Sleep(time.Duration(ms) * time.Millisecond) } - -var quick Time -var once sync.Once - -func Quick() Time { - once.Do(func() { - go func() { - for { - quick = Now() - time.Sleep(time.Second) - } - }() - }) - return quick -} diff --git a/pkg/time/time_test.go b/pkg/time/time_test.go new file mode 100644 index 0000000..82b1720 --- /dev/null +++ b/pkg/time/time_test.go @@ -0,0 +1,17 @@ +package time + +import ( + "encoding/json" + "testing" +) + +func TestTime_MarshalJSON(t *testing.T) { + var abc = struct { + Tm Time + }{ + Tm: Now(), + } + + buf, err := json.Marshal(&abc) + println(string(buf), err) +}