diff --git a/g/os/gtime/time.go b/g/os/gtime/time.go index 5973e35fa..c19ca1acb 100644 --- a/g/os/gtime/time.go +++ b/g/os/gtime/time.go @@ -72,11 +72,11 @@ func Format(format string, timestamps...int64) string { return time.Unix(timestamp, 0).Format(format) } -// 字符串转换为时间戳,需要给定字符串时间格式,format格式形如:2006-01-02 03:04:05 PM -func StrToTime(format string, timestr string) (int64, error) { +// 字符串转换为时间对象,需要给定字符串时间格式,format格式形如:2006-01-02 15:04:05 +func StrToTime(format string, timestr string) (time.Time, error) { t, err := time.Parse(format, timestr) if err != nil { - return 0, err + return time.Time{}, err } - return t.Unix(), nil + return t, nil } diff --git a/g/util/gconv/gconv.go b/g/util/gconv/gconv.go index 041ad0992..d41675818 100644 --- a/g/util/gconv/gconv.go +++ b/g/util/gconv/gconv.go @@ -13,8 +13,19 @@ import ( "time" "strconv" "gitee.com/johng/gf/g/encoding/gbinary" + "gitee.com/johng/gf/g/util/gstr" + "regexp" ) +var ( + // 用于time.Time转换使用,防止多次Compile + timeRegex *regexp.Regexp +) + +func init() { + timeRegex, _ = regexp.Compile(`(\d{4}-\d{2}-\d{2})\s{0,1}(\d{2}:\d{2}:\d{2}){0,1}`) +} + // 将变量i转换为字符串指定的类型t func Convert(i interface{}, t string) interface{} { switch t { @@ -45,10 +56,34 @@ func Time(i interface{}) time.Time { s := String(i) t := int64(0) n := int64(0) - if len(s) > 9 { - t = Int64(s[0 : 10]) - if len(s) > 10 { - n = Int64(s[11 : ]) + if gstr.IsNumeric(s) { + // 纯数字 + if len(s) > 9 { + // 前面10位为时间戳秒,后面转纳秒 + t = Int64(s[0 : 10]) + if len(s) > 10 { + n = Int64(s[10 : ]) + // 如果按照纳秒计算时间则完整字符串长度为19位,这里要将纳秒字段补齐 + if len(s) < 19 { + for i := 0; i < 19 - len(s); i++ { + n *= 10 + } + } + } + } + } else { + // 标准日期时间格式 + if match := timeRegex.FindStringSubmatch(s); len(match) > 0 { + if match[2] != "" { + if t, err := time.Parse("2006-01-02 15:04:05", s); err == nil { + return t + } + } + if match[1] != "" { + if t, err := time.Parse("2006-01-02", s); err == nil { + return t + } + } } } return time.Unix(t, n) diff --git a/geg/database/mysql/gdb.go b/geg/database/mysql/gdb.go index c4dadc5cf..420f03265 100644 --- a/geg/database/mysql/gdb.go +++ b/geg/database/mysql/gdb.go @@ -5,7 +5,6 @@ import ( "time" "gitee.com/johng/gf/g/database/gdb" "gitee.com/johng/gf/g" - "gitee.com/johng/gf/g/frame/gins" ) // 本文件用于gf框架的mysql数据库操作示例,不作为单元测试使用 @@ -14,20 +13,20 @@ var db *gdb.Db // 初始化配置及创建数据库 func init () { - //gdb.AddDefaultConfigNode(gdb.ConfigNode { - // Host : "127.0.0.1", - // Port : "3306", - // User : "root", - // Pass : "8692651", - // Name : "test", - // Type : "mysql", - // Role : "master", - // Charset : "utf8", - //}) - //db, _ = gdb.New() + gdb.AddDefaultConfigNode(gdb.ConfigNode { + Host : "127.0.0.1", + Port : "3306", + User : "root", + Pass : "123456", + Name : "test", + Type : "mysql", + Role : "master", + Charset : "utf8", + }) + db, _ = gdb.New() - gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame") - db = g.Database() + //gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame") + //db = g.Database() //gdb.SetConfig(gdb.ConfigNode { // Host : "127.0.0.1", @@ -477,8 +476,9 @@ func mapToStruct() { } func main() { - r, err := db.Table("user").Data(g.Map{"name" : "john11111"}).Insert() - fmt.Println(r) + r, err := db.Table("test").Where("id=1").One() + fmt.Println(r["datetime"]) + fmt.Println(r["datetime"].Time().Date()) fmt.Println(err) //create() //create() diff --git a/geg/util/gconv.go b/geg/util/gconv/gconv.go similarity index 100% rename from geg/util/gconv.go rename to geg/util/gconv/gconv.go diff --git a/geg/util/gconv/time1.go b/geg/util/gconv/time1.go new file mode 100644 index 000000000..27fcd8f57 --- /dev/null +++ b/geg/util/gconv/time1.go @@ -0,0 +1,15 @@ +package main + +import ( + "gitee.com/johng/gf/g/util/gconv" + "fmt" + "time" +) + +func main() { + now := time.Now() + t := gconv.Time(now.UnixNano()/100) + fmt.Println(now.UnixNano()) + fmt.Println(t.Nanosecond()) + fmt.Println(now.Nanosecond()) +} \ No newline at end of file diff --git a/geg/util/gconv/time2.go b/geg/util/gconv/time2.go new file mode 100644 index 000000000..ef60834c2 --- /dev/null +++ b/geg/util/gconv/time2.go @@ -0,0 +1,13 @@ +package main + +import ( + "gitee.com/johng/gf/g/util/gconv" + "fmt" +) + +func main() { + fmt.Println(gconv.Time("2018-06-07").Date()) + fmt.Println(gconv.Time("2018-06-07").Clock()) + fmt.Println(gconv.Time("2018-06-07 13:01:02").Date()) + fmt.Println(gconv.Time("2018-06-07 13:01:02").Clock()) +} \ No newline at end of file