add gtime.TimeWrapper

This commit is contained in:
John
2020-02-16 18:07:05 +08:00
parent f9e7823c14
commit 7443246e05
5 changed files with 99 additions and 13 deletions

View File

@ -1,11 +1,19 @@
package main
import "github.com/gogf/gf/debug/gdebug"
func Test() {
gdebug.PrintStack()
}
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
)
func main() {
Test()
type T struct {
UpdateTime gtime.Time
}
t := new(T)
gconv.Struct(g.Map{
"UpdateTime": gtime.Now(),
}, t)
fmt.Println(t.UpdateTime)
}

View File

@ -14,7 +14,7 @@ import (
// Time is a wrapper for time.Time for additional features.
type Time struct {
time.Time
TimeWrapper
}
// New creates and returns a Time object with given time.Time object.
@ -24,21 +24,21 @@ func New(t ...time.Time) *Time {
return NewFromTime(t[0])
}
return &Time{
time.Time{},
TimeWrapper{time.Time{}},
}
}
// Now creates and returns a time object of now.
func Now() *Time {
return &Time{
time.Now(),
TimeWrapper{time.Now()},
}
}
// NewFromTime creates and returns a Time object with given time.Time object.
func NewFromTime(t time.Time) *Time {
return &Time{
t,
TimeWrapper{t},
}
}
@ -85,7 +85,7 @@ func NewFromTimeStamp(timestamp int64) *Time {
sec = timestamp
}
return &Time{
time.Unix(sec, nano),
TimeWrapper{time.Unix(sec, nano)},
}
}
@ -157,14 +157,24 @@ func (t *Time) Nanosecond() int {
return t.Time.Nanosecond()
}
// String returns current time object as string.
func (t *Time) String() string {
if t == nil {
return ""
}
if t.IsZero() {
return ""
}
return t.Format("Y-m-d H:i:s")
}
// String returns current time object as string.
//func (t Time) String() string {
// if t.IsZero() {
// return ""
// }
// return t.Format("Y-m-d H:i:s")
//}
// Clone returns a new Time object which is a clone of current time object.
func (t *Time) Clone() *Time {
return New(t.Time)

View File

@ -0,0 +1,25 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gtime
import (
"time"
)
// TimeWrapper is a wrapper for stdlib struct time.Time.
// It's used for overwriting some functions of time.Time, for example: String.
type TimeWrapper struct {
time.Time
}
// String overwrites the String function of time.Time.
func (t TimeWrapper) String() string {
if t.IsZero() {
return ""
}
return t.Format("2006-01-02 15:04:05")
}

View File

@ -13,7 +13,7 @@ import (
"testing"
)
func Test_Json(t *testing.T) {
func Test_Json_Pointer(t *testing.T) {
// Marshal
gtest.Case(t, func() {
type T struct {
@ -55,3 +55,39 @@ func Test_Json(t *testing.T) {
gtest.Assert(t.String(), "2006-01-02 15:04:05")
})
}
func Test_Json_Struct(t *testing.T) {
// Marshal
gtest.Case(t, func() {
type T struct {
Time gtime.Time
}
t := new(T)
s := "2006-01-02 15:04:05"
t.Time = *gtime.NewFromStr(s)
j, err := json.Marshal(t)
gtest.Assert(err, nil)
gtest.Assert(j, `{"Time":"2006-01-02 15:04:05"}`)
})
// Marshal nil
gtest.Case(t, func() {
type T struct {
Time gtime.Time
}
t := new(T)
j, err := json.Marshal(t)
gtest.Assert(err, nil)
gtest.Assert(j, `{"Time":""}`)
})
// Marshal nil omitempty
gtest.Case(t, func() {
type T struct {
Time gtime.Time `json:"time,omitempty"`
}
t := new(T)
j, err := json.Marshal(t)
gtest.Assert(err, nil)
gtest.Assert(j, `{"time":""}`)
})
}

View File

@ -7,6 +7,7 @@
package gtime_test
import (
"fmt"
"testing"
"time"
@ -30,6 +31,10 @@ func Test_Nil(t *testing.T) {
var t *gtime.Time
gtest.Assert(t.String(), "")
})
gtest.Case(t, func() {
var t gtime.Time
gtest.Assert(t.String(), "")
})
}
func Test_NewFromStr(t *testing.T) {
@ -48,9 +53,11 @@ func Test_String(t *testing.T) {
gtest.Case(t, func() {
t1 := gtime.NewFromStr("2006-01-02 15:04:05")
gtest.Assert(t1.String(), "2006-01-02 15:04:05")
gtest.Assert(fmt.Sprintf("%s", t1), "2006-01-02 15:04:05")
t2 := *t1
gtest.Assert(t2.String(), "2006-01-02 15:04:05")
gtest.Assert(fmt.Sprintf("%s", t2), "{2006-01-02 15:04:05}")
})
}