first commit
This commit is contained in:
37
pkg/utils/time_tool.go
Normal file
37
pkg/utils/time_tool.go
Normal file
@ -0,0 +1,37 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"github.com/anxpp/common-utils/str"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Time struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t *Time) UnmarshalJSON(data []byte) (e error) {
|
||||
t.Time, e = time.ParseInLocation(str.TimeLayout, string(data), time.Local)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
return ([]byte)(fmt.Sprintf("\"%s\"", t.Time.Format(str.TimeLayout))), nil
|
||||
}
|
||||
func (t *Time) Scan(value interface{}) error {
|
||||
bytes, _ := value.([]byte)
|
||||
tt, _ := time.ParseInLocation(str.TimeLayout, string(bytes), time.Local)
|
||||
*t = Time{
|
||||
Time: tt,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Time) Value() (driver.Value, error) {
|
||||
return t.Time.Format(str.TimeLayout), nil
|
||||
}
|
||||
|
||||
func (t Time) String() string {
|
||||
return str.TimeToString(t.Time)
|
||||
}
|
||||
Reference in New Issue
Block a user