improve date type support for package gdb; improve datetime string for gtime.StrToTime; improve function gtime.New for more types

This commit is contained in:
John 2020-08-21 23:41:12 +08:00
parent 676e904ec6
commit bea451c9d6
6 changed files with 77 additions and 19 deletions

View File

@ -8,6 +8,7 @@ package gdb
import (
"strings"
"time"
"github.com/gogf/gf/text/gstr"
@ -88,12 +89,18 @@ func (c *Core) convertValue(fieldValue interface{}, fieldType string) interface{
return gconv.Bool(fieldValue)
case "date":
if t, ok := fieldValue.(time.Time); ok {
return gtime.NewFromTime(t).Format("Y-m-d")
}
t, _ := gtime.StrToTime(gconv.String(fieldValue))
return t.Format("Y-m-d")
case
"datetime",
"timestamp":
if t, ok := fieldValue.(time.Time); ok {
return gtime.NewFromTime(t)
}
t, _ := gtime.StrToTime(gconv.String(fieldValue))
return t.String()

View File

@ -16,7 +16,6 @@ import (
)
func Test_Types(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS types (
@ -32,8 +31,16 @@ func Test_Types(t *testing.T) {
%s bool NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, "`blob`", "`binary`", "`date`", "`time`",
"`decimal`", "`double`", "`bit`", "`tinyint`", "`bool`")); err != nil {
`,
"`blob`",
"`binary`",
"`date`",
"`time`",
"`decimal`",
"`double`",
"`bit`",
"`tinyint`",
"`bool`")); err != nil {
gtest.Error(err)
}
defer dropTable("types")
@ -41,7 +48,7 @@ func Test_Types(t *testing.T) {
"id": 1,
"blob": "i love gf",
"binary": []byte("abcdefgh"),
"date": "2018-10-24",
"date": "1880-10-24",
"time": "10:00:01",
"decimal": -123.456,
"double": -123.456,

View File

@ -227,7 +227,7 @@ func parseDateStr(s string) (year, month, day int) {
return
}
// StrToTime converts string to *Time object.
// StrToTime converts string to *Time object. It also supports timestamp string.
// The parameter <format> is unnecessary, which specifies the format for converting like "Y-m-d H:i:s".
// If <format> is given, it acts as same as function StrToTimeFormat.
// If <format> is not given, it converts string as a "standard" datetime string.
@ -236,6 +236,10 @@ func StrToTime(str string, format ...string) (*Time, error) {
if len(format) > 0 {
return StrToTimeFormat(str, format[0])
}
if isTimestampStr(str) {
timestamp, _ := strconv.ParseInt(str, 10, 64)
return NewFromTimeStamp(timestamp), nil
}
var (
year, month, day int
hour, min, sec, nsec int
@ -290,8 +294,8 @@ func StrToTime(str string, format ...string) (*Time, error) {
if h > 24 || m > 59 || s > 59 {
return nil, gerror.Newf("invalid zone string: %s", match[6])
}
// Comparing the given time zone whether equals to current tine zone,
// it converts it to UTC if they does not.
// Comparing the given time zone whether equals to current time zone,
// it converts it to UTC if they does not equal.
_, localOffset := time.Now().Zone()
// Comparing in seconds.
if (h*3600 + m*60 + s) != localOffset {
@ -326,7 +330,7 @@ func StrToTime(str string, format ...string) (*Time, error) {
}
}
}
if year <= 0 || month <= 0 || day <= 0 || hour < 0 || min < 0 || sec < 0 || nsec < 0 {
if year <= 0 {
return nil, errors.New("invalid time string:" + str)
}
// It finally converts all time to UTC time zone.
@ -424,3 +428,17 @@ func FuncCost(f func()) int64 {
f()
return TimestampNano() - t
}
// isTimestampStr checks and returns whether given string a timestamp string.
func isTimestampStr(s string) bool {
length := len(s)
if length == 0 {
return false
}
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return false
}
}
return true
}

View File

@ -17,11 +17,22 @@ type Time struct {
TimeWrapper
}
// New creates and returns a Time object with given time.Time object.
// The parameter <t> is optional.
func New(t ...time.Time) *Time {
if len(t) > 0 {
return NewFromTime(t[0])
// New creates and returns a Time object with given parameter.
// The optional parameter can be type of: time.Time, string or integer.
func New(param ...interface{}) *Time {
if len(param) > 0 {
switch r := param[0].(type) {
case time.Time:
return NewFromTime(r)
case string:
return NewFromStr(r)
case []byte:
return NewFromStr(string(r))
case int:
return NewFromTimeStamp(int64(r))
case int64:
return NewFromTimeStamp(r)
}
}
return &Time{
TimeWrapper{time.Time{}},

View File

@ -86,9 +86,7 @@ func Test_RFC822(t *testing.T) {
func Test_StrToTime(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
//正常日期列表
//正则的原因,日期"06.01.02""2006.01""2006..01"无法覆盖gtime.go的百分百
var testDatetimes = []string{
var testDateTimes = []string{
"2006-01-02 15:04:05",
"2006/01/02 15:04:05",
"2006.01.02 15:04:05.000",
@ -103,7 +101,7 @@ func Test_StrToTime(t *testing.T) {
"02.jan.2006:15:04:05",
}
for _, item := range testDatetimes {
for _, item := range testDateTimes {
timeTemp, err := gtime.StrToTime(item)
if err != nil {
t.Error("test fail")
@ -155,7 +153,6 @@ func Test_StrToTime(t *testing.T) {
var testDatesFail = []string{
"2006.01",
"06..02",
"20060102",
}
for _, item := range testDatesFail {

View File

@ -16,6 +16,7 @@ import (
)
func Test_New(t *testing.T) {
// time.Time
gtest.C(t, func(t *gtest.T) {
timeNow := time.Now()
timeTemp := gtime.New(timeNow)
@ -24,6 +25,23 @@ func Test_New(t *testing.T) {
timeTemp1 := gtime.New()
t.Assert(timeTemp1.Time, time.Time{})
})
// string
gtest.C(t, func(t *gtest.T) {
timeNow := gtime.Now()
timeTemp := gtime.New(timeNow.String())
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
})
gtest.C(t, func(t *gtest.T) {
timeNow := gtime.Now()
timeTemp := gtime.New(timeNow.TimestampMicroStr())
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
})
// int64
gtest.C(t, func(t *gtest.T) {
timeNow := gtime.Now()
timeTemp := gtime.New(timeNow.TimestampMicro())
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
})
}
func Test_Nil(t *testing.T) {
@ -42,7 +60,7 @@ func Test_NewFromStr(t *testing.T) {
timeTemp := gtime.NewFromStr("2006-01-02 15:04:05")
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05")
timeTemp1 := gtime.NewFromStr("20060102")
timeTemp1 := gtime.NewFromStr("2006.0102")
if timeTemp1 != nil {
t.Error("test fail")
}