-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_localtimezone.go
53 lines (43 loc) · 1.07 KB
/
data_localtimezone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package data
import (
"database/sql/driver"
"time"
)
type LocalTimeZone time.Time
const localTimeZoneFormat = "2006-01-02 15:04:05"
func (t *LocalTimeZone) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+localTimeZoneFormat+`"`, string(data), time.Local)
*t = LocalTimeZone(now)
return
}
func (t LocalTimeZone) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(localTimeZoneFormat)+2)
b = append(b, '"')
b = append(b, []byte(t.String())...)
b = append(b, '"')
return b, nil
}
func (t LocalTimeZone) String() string {
if time.Time(t).IsZero() {
return "0000-00-00 00:00:00"
}
return time.Time(t).Format(localTimeZoneFormat)
}
func (t LocalTimeZone) Value() (driver.Value, error) {
if time.Time(t).IsZero() {
return time.Now().Local(), nil
}
return time.Time(t).Local(), nil
}
func (t *LocalTimeZone) Scan(v interface{}) error {
switch vt := v.(type) {
case time.Time:
*t = LocalTimeZone(vt)
case string:
tTime, _ := time.Parse("2006-01-02 15:04:05", vt)
*t = LocalTimeZone(tTime)
default:
return nil
}
return nil
}