-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
time.go
123 lines (104 loc) · 2.9 KB
/
time.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package datatypes
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// Time is time data type.
type Time time.Duration
// NewTime is a constructor for Time and returns new Time.
func NewTime(hour, min, sec, nsec int) Time {
return newTime(hour, min, sec, nsec)
}
func newTime(hour, min, sec, nsec int) Time {
return Time(
time.Duration(hour)*time.Hour +
time.Duration(min)*time.Minute +
time.Duration(sec)*time.Second +
time.Duration(nsec)*time.Nanosecond,
)
}
// GormDataType returns gorm common data type. This type is used for the field's column type.
func (Time) GormDataType() string {
return "time"
}
// GormDBDataType returns gorm DB data type based on the current using database.
func (Time) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "mysql":
return "TIME"
case "postgres":
return "TIME"
case "sqlserver":
return "TIME"
case "sqlite":
return "TEXT"
default:
return ""
}
}
// Scan implements sql.Scanner interface and scans value into Time,
func (t *Time) Scan(src interface{}) error {
switch v := src.(type) {
case []byte:
t.setFromString(string(v))
case string:
t.setFromString(v)
case time.Time:
t.setFromTime(v)
default:
return errors.New(fmt.Sprintf("failed to scan value: %v", v))
}
return nil
}
func (t *Time) setFromString(str string) {
var h, m, s, n int
fmt.Sscanf(str, "%02d:%02d:%02d.%09d", &h, &m, &s, &n)
*t = newTime(h, m, s, n)
}
func (t *Time) setFromTime(src time.Time) {
*t = newTime(src.Hour(), src.Minute(), src.Second(), src.Nanosecond())
}
// Value implements driver.Valuer interface and returns string format of Time.
func (t Time) Value() (driver.Value, error) {
return t.String(), nil
}
// String implements fmt.Stringer interface.
func (t Time) String() string {
if nsec := t.nanoseconds(); nsec > 0 {
return fmt.Sprintf("%02d:%02d:%02d.%09d", t.hours(), t.minutes(), t.seconds(), nsec)
} else {
// omit nanoseconds unless any value is specified
return fmt.Sprintf("%02d:%02d:%02d", t.hours(), t.minutes(), t.seconds())
}
}
func (t Time) hours() int {
return int(time.Duration(t).Truncate(time.Hour).Hours())
}
func (t Time) minutes() int {
return int((time.Duration(t) % time.Hour).Truncate(time.Minute).Minutes())
}
func (t Time) seconds() int {
return int((time.Duration(t) % time.Minute).Truncate(time.Second).Seconds())
}
func (t Time) nanoseconds() int {
return int((time.Duration(t) % time.Second).Nanoseconds())
}
// MarshalJSON implements json.Marshaler to convert Time to json serialization.
func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
// UnmarshalJSON implements json.Unmarshaler to deserialize json data.
func (t *Time) UnmarshalJSON(data []byte) error {
// ignore null
if string(data) == "null" {
return nil
}
t.setFromString(strings.Trim(string(data), `"`))
return nil
}