Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["time.go"],
importpath = "go-common/app/admin/main/videoup/model/utils",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["time_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)

View File

@@ -0,0 +1,58 @@
package utils
import (
"database/sql/driver"
"time"
)
// FormatTime format time
type FormatTime string
// NewFormatTime net formatTime
func NewFormatTime(t time.Time) FormatTime {
ft := new(FormatTime)
ft.Scan(t)
return *ft
}
// Scan scan time.
func (jt *FormatTime) Scan(src interface{}) (err error) {
switch sc := src.(type) {
case time.Time:
*jt = FormatTime(sc.Format("2006-01-02 15:04:05"))
case string:
*jt = FormatTime(sc)
}
return
}
// Value get string value.
func (jt FormatTime) Value() (driver.Value, error) {
return string(jt), nil
}
// TimeValue get time value.
func (jt FormatTime) TimeValue() time.Time {
t, _ := time.ParseInLocation("2006-01-02 15:04:05", string(jt), time.Local)
if t.Unix() <= 0 {
t, _ = time.ParseInLocation("2006-01-02 15:04:05", "0000-00-00 00:00:00", time.Local)
}
return t
}
// UnmarshalJSON implement Unmarshaler
func (jt *FormatTime) UnmarshalJSON(data []byte) error {
if data == nil || len(data) <= 1 {
*jt = FormatTime("0000-00-00 00:00:00")
return nil
}
str := string(data[1 : len(data)-1])
st, err := time.Parse(time.RFC3339, str)
if err == nil {
*jt = FormatTime(st.Format("2006-01-02 15:04:05"))
} else {
*jt = FormatTime(str)
}
return nil
}

View File

@@ -0,0 +1,55 @@
package utils
import (
"encoding/json"
"fmt"
"testing"
"time"
)
func Test_format(t *testing.T) {
type A struct {
Ct FormatTime `json:"ct"`
}
a := &A{Ct: ""}
if !a.Ct.TimeValue().IsZero() {
t.Fatal("1")
}
a = &A{Ct: "0000-00-00 00:00:00"}
fmt.Println("ct:", a.Ct)
fmt.Println("value:", a.Ct.TimeValue())
fmt.Println("zero:", a.Ct.TimeValue().IsZero())
if !a.Ct.TimeValue().IsZero() {
t.Fatal("2")
}
a = &A{Ct: "0001-01-01 00:00:00"}
fmt.Println("ct:", a.Ct)
fmt.Println("value:", a.Ct.TimeValue())
fmt.Println("zero:", a.Ct.TimeValue().IsZero())
if !a.Ct.TimeValue().IsZero() {
t.Fatal("3")
}
Tt, _ := time.ParseInLocation("2006-01-02 15:04:05", "2018-06-01 22:59:16", time.Local)
a = &A{}
J := `{"ct":"2018-06-01 22:59:16"}`
err := json.Unmarshal([]byte(J), a)
if err != nil || string(a.Ct) != "2018-06-01 22:59:16" || a.Ct.TimeValue() != Tt {
t.Fatal("4")
}
fmt.Println("ct:", a.Ct)
fmt.Println("value:", a.Ct.TimeValue())
fmt.Println("zero:", a.Ct.TimeValue().IsZero())
J = `{"ct":"2018-06-01T22:59:16.437367789+08:00"}`
err = json.Unmarshal([]byte(J), a)
if err != nil || string(a.Ct) != "2018-06-01 22:59:16" || a.Ct.TimeValue() != Tt {
t.Fatal("5")
}
fmt.Println("ct:", a.Ct)
fmt.Println("value:", a.Ct.TimeValue())
fmt.Println("zero:", a.Ct.TimeValue().IsZero())
}