mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
改进gconv.Time方法,增加对标准日期时间字符串的参数支持;修改gtime.StrToTime方法返回值为time.Time对象类型
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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()
|
||||
|
||||
15
geg/util/gconv/time1.go
Normal file
15
geg/util/gconv/time1.go
Normal file
@ -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())
|
||||
}
|
||||
13
geg/util/gconv/time2.go
Normal file
13
geg/util/gconv/time2.go
Normal file
@ -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())
|
||||
}
|
||||
Reference in New Issue
Block a user