forked from iktakahiro/revcatgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
milliseconds_value.go
59 lines (47 loc) · 1.2 KB
/
milliseconds_value.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
package revcatgo
import (
"fmt"
"time"
"gopkg.in/guregu/null.v4"
)
type milliseconds struct {
value null.Int
}
const (
// 1973-03-03T09:46:40
millisecondsThreshold = 100000000
)
func newMilliseconds(v null.Int) (*milliseconds, error) {
if v.ValueOrZero() != 0 && v.ValueOrZero() < millisecondsThreshold {
return &milliseconds{}, fmt.Errorf("milliseconds should be grater than %v", millisecondsThreshold)
}
return &milliseconds{value: v}, nil
}
func (m *milliseconds) Int64() int64 {
return m.value.ValueOrZero()
}
func (m *milliseconds) NullInt() null.Int {
return m.value
}
func (m *milliseconds) String() string {
return fmt.Sprint(m.Int64())
}
func (m *milliseconds) DateTime() time.Time {
return time.Unix(m.value.ValueOrZero()/1000, 0)
}
func (m milliseconds) MarshalJSON() ([]byte, error) {
return m.value.MarshalJSON()
}
func (m *milliseconds) UnmarshalJSON(b []byte) error {
v := &milliseconds{}
err := v.value.UnmarshalJSON(b)
if err != nil {
return fmt.Errorf("failed to unmarshal the value of milliseconds: %w", err)
}
_m, err := newMilliseconds(v.value)
if err != nil {
return fmt.Errorf("failed to unmarshal the value of milliseconds: %w", err)
}
m.value = _m.value
return nil
}