mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve time converting for package gconv
This commit is contained in:
@ -8,6 +8,7 @@ package gdb_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
@ -15,6 +16,7 @@ import (
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
// All types testing.
|
||||
func Test_Types(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
@ -72,6 +74,29 @@ func Test_Types(t *testing.T) {
|
||||
t.Assert(one["double"].String(), data["double"])
|
||||
t.Assert(one["bit"].Int(), data["bit"])
|
||||
t.Assert(one["tinyint"].Bool(), data["tinyint"])
|
||||
t.Assert(one["tinyint"].Bool(), data["tinyint"])
|
||||
|
||||
type T struct {
|
||||
Id int
|
||||
Blob []byte
|
||||
Binary []byte
|
||||
Date *gtime.Time
|
||||
Time *gtime.Time
|
||||
Decimal float64
|
||||
Double float64
|
||||
Bit int8
|
||||
TinyInt bool
|
||||
}
|
||||
var obj *T
|
||||
err = db.Table("types").Struct(&obj)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(obj.Id, 1)
|
||||
t.Assert(obj.Blob, data["blob"])
|
||||
t.Assert(obj.Binary, data["binary"])
|
||||
t.Assert(obj.Date.Format("Y-m-d"), data["date"])
|
||||
t.Assert(obj.Time.String(), `0000-01-01 10:00:01`)
|
||||
t.Assert(obj.Decimal, -123.46)
|
||||
t.Assert(obj.Double, data["double"])
|
||||
t.Assert(obj.Bit, data["bit"])
|
||||
t.Assert(obj.TinyInt, data["tinyint"])
|
||||
})
|
||||
}
|
||||
|
||||
@ -350,9 +350,9 @@ func StrToTime(str string, format ...string) (*Time, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if year <= 0 {
|
||||
return nil, errors.New("invalid time string:" + str)
|
||||
}
|
||||
//if year <= 0 {
|
||||
// return nil, errors.New("invalid time string:" + str)
|
||||
//}
|
||||
return NewFromTime(time.Date(year, time.Month(month), day, hour, min, sec, nsec, local)), nil
|
||||
}
|
||||
|
||||
@ -367,7 +367,7 @@ func ConvertZone(strTime string, toZone string, fromZone ...string) (*Time, erro
|
||||
if l, err := time.LoadLocation(fromZone[0]); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
t.Time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Time.Second(), t.Time.Nanosecond(), l)
|
||||
t.Time = time.Date(t.Year(), time.Month(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Time.Second(), t.Time.Nanosecond(), l)
|
||||
}
|
||||
}
|
||||
if l, err := time.LoadLocation(toZone); err != nil {
|
||||
|
||||
@ -179,6 +179,11 @@ func (t *Time) TimestampNanoStr() string {
|
||||
return strconv.FormatInt(t.TimestampNano(), 10)
|
||||
}
|
||||
|
||||
// Month returns the month of the year specified by t.
|
||||
func (t *Time) Month() int {
|
||||
return int(t.Time.Month())
|
||||
}
|
||||
|
||||
// Second returns the second offset within the minute specified by t,
|
||||
// in the range [0, 59].
|
||||
func (t *Time) Second() int {
|
||||
|
||||
@ -24,6 +24,20 @@ func Test_Time(t *testing.T) {
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := "01:02:03.456"
|
||||
t.AssertEQ(gconv.GTime(s).Hour(), 1)
|
||||
t.AssertEQ(gconv.GTime(s).Minute(), 2)
|
||||
t.AssertEQ(gconv.GTime(s).Second(), 3)
|
||||
t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
|
||||
t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := "0000-01-01 01:02:03"
|
||||
t.AssertEQ(gconv.GTime(s).Year(), 0)
|
||||
t.AssertEQ(gconv.GTime(s).Month(), 1)
|
||||
t.AssertEQ(gconv.GTime(s).Day(), 1)
|
||||
t.AssertEQ(gconv.GTime(s).Hour(), 1)
|
||||
t.AssertEQ(gconv.GTime(s).Minute(), 2)
|
||||
t.AssertEQ(gconv.GTime(s).Second(), 3)
|
||||
t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
|
||||
t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user