-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_localdate.go
53 lines (43 loc) · 1.04 KB
/
data_localdate.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 LocalDate time.Time
const localDateFormat = "2006-01-02"
func (t *LocalDate) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+localDateFormat+`"`, string(data), time.Local)
*t = LocalDate(now)
return
}
func (t LocalDate) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(localDateFormat)+2)
b = append(b, '"')
b = append(b, []byte(t.String())...)
b = append(b, '"')
return b, nil
}
func (t LocalDate) String() string {
if time.Time(t).IsZero() {
return "0000-00-00"
}
return time.Time(t).Format(localDateFormat)
}
func (t LocalDate) Value() (driver.Value, error) {
if time.Time(t).IsZero() {
return time.Now(), nil
}
return time.Time(t), nil
}
func (t *LocalDate) Scan(v interface{}) error {
switch vt := v.(type) {
case time.Time:
*t = LocalDate(vt)
case string:
tTime, _ := time.Parse("2006/01/02 15:04:05", vt)
*t = LocalDate(tTime)
default:
return nil
}
return nil
}